UNPKG

6.71 kBTypeScriptView Raw
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13import {Key, ReactElement, ReactNode} from 'react';
14
15export interface ItemProps<T> {
16 /** Rendered contents of the item or child items. */
17 children: ReactNode,
18 /** Rendered contents of the item if `children` contains child items. */
19 title?: ReactNode, // label?? contents?
20 /** A string representation of the item's contents, used for features like typeahead. */
21 textValue?: string,
22 /** An accessibility label for this item. */
23 'aria-label'?: string,
24 /** A list of child item objects. Used for dynamic collections. */
25 childItems?: Iterable<T>,
26 /** Whether this item has children, even if not loaded yet. */
27 hasChildItems?: boolean
28}
29
30export type ItemElement<T> = ReactElement<ItemProps<T>>;
31export type ItemRenderer<T> = (item: T) => ItemElement<T>;
32export type LoadingState = 'loading' | 'sorting' | 'loadingMore' | 'error' | 'idle' | 'filtering';
33
34export interface AsyncLoadable {
35 /** Whether the items are currently loading. */
36 isLoading?: boolean, // possibly isLoadingMore
37 /** Handler that is called when more items should be loaded, e.g. while scrolling near the bottom. */
38 onLoadMore?: () => any
39}
40
41export interface SectionProps<T> {
42 /** Rendered contents of the section, e.g. a header. */
43 title?: ReactNode,
44 /** An accessibility label for the section. */
45 'aria-label'?: string,
46 /** Static child items or a function to render children. */
47 children: ItemElement<T> | ItemElement<T>[] | ItemRenderer<T>,
48 /** Item objects in the section. */
49 items?: Iterable<T>
50}
51
52export type SectionElement<T> = ReactElement<SectionProps<T>>;
53
54export type CollectionElement<T> = SectionElement<T> | ItemElement<T>;
55export type CollectionChildren<T> = CollectionElement<T> | CollectionElement<T>[] | ((item: T) => CollectionElement<T>);
56export interface CollectionBase<T> {
57 /** The contents of the collection. */
58 children: CollectionChildren<T>,
59 /** Item objects in the collection. */
60 items?: Iterable<T>,
61 /** The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. */
62 disabledKeys?: Iterable<Key>
63}
64
65export interface Expandable {
66 /** The currently expanded keys in the collection (controlled). */
67 expandedKeys?: Iterable<Key>,
68 /** The initial expanded keys in the collection (uncontrolled). */
69 defaultExpandedKeys?: Iterable<Key>,
70 /** Handler that is called when items are expanded or collapsed. */
71 onExpandedChange?: (keys: Set<Key>) => any
72}
73
74export interface Sortable {
75 /** The current sorted column and direction. */
76 sortDescriptor?: SortDescriptor,
77 /** Handler that is called when the sorted column or direction changes. */
78 onSortChange?: (descriptor: SortDescriptor) => any
79}
80
81export interface SortDescriptor {
82 /** The key of the column to sort by. */
83 column?: Key,
84 /** The direction to sort by. */
85 direction?: SortDirection
86}
87
88export type SortDirection = 'ascending' | 'descending';
89
90export interface KeyboardDelegate {
91 /** Returns the key visually below the given one, or `null` for none. */
92 getKeyBelow?(key: Key): Key | null,
93
94 /** Returns the key visually above the given one, or `null` for none. */
95 getKeyAbove?(key: Key): Key | null,
96
97 /** Returns the key visually to the left of the given one, or `null` for none. */
98 getKeyLeftOf?(key: Key): Key | null,
99
100 /** Returns the key visually to the right of the given one, or `null` for none. */
101 getKeyRightOf?(key: Key): Key | null,
102
103 /** Returns the key visually one page below the given one, or `null` for none. */
104 getKeyPageBelow?(key: Key): Key | null,
105
106 /** Returns the key visually one page above the given one, or `null` for none. */
107 getKeyPageAbove?(key: Key): Key | null,
108
109 /** Returns the first key, or `null` for none. */
110 getFirstKey?(key?: Key, global?: boolean): Key | null,
111
112 /** Returns the last key, or `null` for none. */
113 getLastKey?(key?: Key, global?: boolean): Key | null,
114
115 /** Returns the next key after `fromKey` that matches the given search string, or `null` for none. */
116 getKeyForSearch?(search: string, fromKey?: Key): Key | null
117}
118
119/**
120 * A generic interface to access a readonly sequential
121 * collection of unique keyed items.
122 */
123export interface Collection<T> extends Iterable<T> {
124 /** The number of items in the collection. */
125 readonly size: number,
126
127 /** Iterate over all keys in the collection. */
128 getKeys(): Iterable<Key>,
129
130 /** Get an item by its key. */
131 getItem(key: Key): T,
132
133 /** Get an item by the index of its key. */
134 at(idx: number): T,
135
136 /** Get the key that comes before the given key in the collection. */
137 getKeyBefore(key: Key): Key | null,
138
139 /** Get the key that comes after the given key in the collection. */
140 getKeyAfter(key: Key): Key | null,
141
142 /** Get the first key in the collection. */
143 getFirstKey(): Key | null,
144
145 /** Get the last key in the collection. */
146 getLastKey(): Key | null
147}
148
149export interface Node<T> {
150 /** The type of item this node represents. */
151 type: string,
152 /** A unique key for the node. */
153 key: Key,
154 /** The object value the node was created from. */
155 value: T,
156 /** The level of depth this node is at in the heirarchy. */
157 level: number,
158 /** Whether this item has children, even if not loaded yet. */
159 hasChildNodes: boolean,
160 /** The loaded children of this node. */
161 childNodes: Iterable<Node<T>>,
162 /** The rendered contents of this node (e.g. JSX). */
163 rendered: ReactNode,
164 /** A string value for this node, used for features like typeahead. */
165 textValue: string,
166 /** An accessibility label for this node. */
167 'aria-label'?: string,
168 /** The index of this node within its parent. */
169 index?: number,
170 /** A function that should be called to wrap the rendered node. */
171 wrapper?: (element: ReactElement) => ReactElement,
172 /** The key of the parent node. */
173 parentKey?: Key,
174 /** The key of the node before this node. */
175 prevKey?: Key,
176 /** The key of the node after this node. */
177 nextKey?: Key,
178 /** Additional properties specific to a particular node type. */
179 props?: any,
180 /** @private */
181 shouldInvalidate?: (context: unknown) => boolean
182}