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