UNPKG

7.27 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 CollectionStateBase<T, C extends Collection<Node<T>> = Collection<Node<T>>> extends Partial<CollectionBase<T>> {
66 /** A pre-constructed collection to use instead of building one from items and children. */
67 collection?: C
68}
69
70export interface Expandable {
71 /** The currently expanded keys in the collection (controlled). */
72 expandedKeys?: Iterable<Key>,
73 /** The initial expanded keys in the collection (uncontrolled). */
74 defaultExpandedKeys?: Iterable<Key>,
75 /** Handler that is called when items are expanded or collapsed. */
76 onExpandedChange?: (keys: Set<Key>) => any
77}
78
79export interface Sortable {
80 /** The current sorted column and direction. */
81 sortDescriptor?: SortDescriptor,
82 /** Handler that is called when the sorted column or direction changes. */
83 onSortChange?: (descriptor: SortDescriptor) => any
84}
85
86export interface SortDescriptor {
87 /** The key of the column to sort by. */
88 column?: Key,
89 /** The direction to sort by. */
90 direction?: SortDirection
91}
92
93export type SortDirection = 'ascending' | 'descending';
94
95export interface KeyboardDelegate {
96 /** Returns the key visually below the given one, or `null` for none. */
97 getKeyBelow?(key: Key): Key | null,
98
99 /** Returns the key visually above the given one, or `null` for none. */
100 getKeyAbove?(key: Key): Key | null,
101
102 /** Returns the key visually to the left of the given one, or `null` for none. */
103 getKeyLeftOf?(key: Key): Key | null,
104
105 /** Returns the key visually to the right of the given one, or `null` for none. */
106 getKeyRightOf?(key: Key): Key | null,
107
108 /** Returns the key visually one page below the given one, or `null` for none. */
109 getKeyPageBelow?(key: Key): Key | null,
110
111 /** Returns the key visually one page above the given one, or `null` for none. */
112 getKeyPageAbove?(key: Key): Key | null,
113
114 /** Returns the first key, or `null` for none. */
115 getFirstKey?(key?: Key, global?: boolean): Key | null,
116
117 /** Returns the last key, or `null` for none. */
118 getLastKey?(key?: Key, global?: boolean): Key | null,
119
120 /** Returns the next key after `fromKey` that matches the given search string, or `null` for none. */
121 getKeyForSearch?(search: string, fromKey?: Key): Key | null
122}
123
124/**
125 * A generic interface to access a readonly sequential
126 * collection of unique keyed items.
127 */
128export interface Collection<T> extends Iterable<T> {
129 /** The number of items in the collection. */
130 readonly size: number,
131
132 /** Iterate over all keys in the collection. */
133 getKeys(): Iterable<Key>,
134
135 /** Get an item by its key. */
136 getItem(key: Key): T | null,
137
138 /** Get an item by the index of its key. */
139 at(idx: number): T | null,
140
141 /** Get the key that comes before the given key in the collection. */
142 getKeyBefore(key: Key): Key | null,
143
144 /** Get the key that comes after the given key in the collection. */
145 getKeyAfter(key: Key): Key | null,
146
147 /** Get the first key in the collection. */
148 getFirstKey(): Key | null,
149
150 /** Get the last key in the collection. */
151 getLastKey(): Key | null,
152
153 /** Iterate over the child items of the given key. */
154 getChildren?(key: Key): Iterable<T>,
155
156 /** Returns a string representation of the item's contents. */
157 getTextValue?(key: Key): string
158}
159
160export interface Node<T> {
161 /** The type of item this node represents. */
162 type: string,
163 /** A unique key for the node. */
164 key: Key,
165 /** The object value the node was created from. */
166 value: T | null,
167 /** The level of depth this node is at in the heirarchy. */
168 level: number,
169 /** Whether this item has children, even if not loaded yet. */
170 hasChildNodes: boolean,
171 /**
172 * The loaded children of this node.
173 * @deprecated Use `collection.getChildren(node.key)` instead.
174 */
175 childNodes: Iterable<Node<T>>,
176 /** The rendered contents of this node (e.g. JSX). */
177 rendered: ReactNode,
178 /** A string value for this node, used for features like typeahead. */
179 textValue: string,
180 /** An accessibility label for this node. */
181 'aria-label'?: string,
182 /** The index of this node within its parent. */
183 index?: number,
184 /** A function that should be called to wrap the rendered node. */
185 wrapper?: (element: ReactElement) => ReactElement,
186 /** The key of the parent node. */
187 parentKey?: Key | null,
188 /** The key of the node before this node. */
189 prevKey?: Key | null,
190 /** The key of the node after this node. */
191 nextKey?: Key | null,
192 /** Additional properties specific to a particular node type. */
193 props?: any,
194 /** @private */
195 shouldInvalidate?: (context: unknown) => boolean
196}