UNPKG

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