datatable-tree 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +251 -2
- package/dist/DataTable.d.ts +583 -0
- package/dist/DataTable.js +620 -0
- package/dist/Draggable.d.ts +35 -0
- package/dist/Draggable.js +91 -0
- package/dist/ListBox.d.ts +132 -0
- package/dist/ListBox.js +243 -0
- package/dist/ListVirtualizer.d.ts +59 -0
- package/dist/ListVirtualizer.js +180 -0
- package/dist/TreeList.d.ts +196 -0
- package/dist/TreeList.js +300 -0
- package/dist/config.d.ts +55 -0
- package/dist/config.js +33 -0
- package/dist/datatable.css +161 -0
- package/dist/hooks.d.ts +27 -0
- package/dist/hooks.js +115 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +15 -0
- package/dist/interfaces.d.ts +269 -0
- package/dist/interfaces.js +3 -0
- package/dist/mobx-adapter.d.ts +12 -0
- package/dist/mobx-adapter.js +21 -0
- package/dist/reactivity.d.ts +112 -0
- package/dist/reactivity.js +155 -0
- package/dist/styles.d.ts +64 -0
- package/dist/styles.js +86 -0
- package/dist/theme.d.ts +160 -0
- package/dist/theme.js +239 -0
- package/dist/util.d.ts +28 -0
- package/dist/util.js +45 -0
- package/package.json +143 -4
- package/src/DataTable.tsx +1332 -0
- package/src/Draggable.tsx +133 -0
- package/src/ListBox.tsx +423 -0
- package/src/ListVirtualizer.ts +234 -0
- package/src/TreeList.tsx +590 -0
- package/src/config.tsx +96 -0
- package/src/css.d.ts +1 -0
- package/src/datatable.css +161 -0
- package/src/hooks.ts +138 -0
- package/src/index.ts +15 -0
- package/src/interfaces.ts +309 -0
- package/src/mobx-adapter.ts +24 -0
- package/src/reactivity.ts +222 -0
- package/src/styles.tsx +102 -0
- package/src/theme.ts +346 -0
- package/src/util.ts +60 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/** Read-only set interface (subinterface of IMapSource<K,any>).
|
|
2
|
+
* The word "set" usually means that each item in the collection is unique
|
|
3
|
+
* (appears only once, based on a definition of equality used by the
|
|
4
|
+
* collection.) Objects conforming to this interface aren't guaranteed not
|
|
5
|
+
* to contain duplicates, but as an example, BTree<K,V> implements this
|
|
6
|
+
* interface and does not allow duplicates. */
|
|
7
|
+
export interface ISetSource<K = any> {
|
|
8
|
+
/** Returns the number of key/value pairs in the map object. */
|
|
9
|
+
size: number;
|
|
10
|
+
/** Returns a boolean asserting whether the key exists in the map object or not. */
|
|
11
|
+
has(key: K): boolean;
|
|
12
|
+
/** Returns a new iterator for iterating the items in the set (the order is implementation-dependent). */
|
|
13
|
+
keys(): IterableIterator<K>;
|
|
14
|
+
}
|
|
15
|
+
/** Read-only map interface (i.e. a source of key-value pairs). */
|
|
16
|
+
export interface IMapSource<K = any, V = any> extends ISetSource<K> {
|
|
17
|
+
/** Returns the number of key/value pairs in the map object. */
|
|
18
|
+
size: number;
|
|
19
|
+
/** Returns the value associated to the key, or undefined if there is none. */
|
|
20
|
+
get(key: K): V | undefined;
|
|
21
|
+
/** Returns a boolean asserting whether the key exists in the map object or not. */
|
|
22
|
+
has(key: K): boolean;
|
|
23
|
+
/** Calls callbackFn once for each key-value pair present in the map object.
|
|
24
|
+
* The ES6 Map class sends the value to the callback before the key, so
|
|
25
|
+
* this interface must do likewise. */
|
|
26
|
+
forEach(callbackFn: (v: V, k: K, map: IMapSource<K, V>) => void, thisArg?: any): void;
|
|
27
|
+
/** Returns an iterator that provides all key-value pairs from the collection (as arrays of length 2). */
|
|
28
|
+
entries(): IterableIterator<[K, V]>;
|
|
29
|
+
/** Returns a new iterator for iterating the keys of each pair. */
|
|
30
|
+
keys(): IterableIterator<K>;
|
|
31
|
+
/** Returns a new iterator for iterating the values of each pair. */
|
|
32
|
+
values(): IterableIterator<V>;
|
|
33
|
+
}
|
|
34
|
+
/** Write-only set interface (the set cannot be queried, but items can be added to it.)
|
|
35
|
+
* Note: BTree<K,V> does not officially implement this interface, but BTree<K>
|
|
36
|
+
* can be used as an instance of ISetSink<K>. */
|
|
37
|
+
export interface ISetSink<K = any> {
|
|
38
|
+
/** Adds the specified item to the set, if it was not in the set already. */
|
|
39
|
+
add(key: K): any;
|
|
40
|
+
/** Returns true if an element in the map object existed and has been
|
|
41
|
+
* removed, or false if the element did not exist. */
|
|
42
|
+
delete(key: K): boolean;
|
|
43
|
+
/** Removes everything so that the set is empty. */
|
|
44
|
+
clear(): void;
|
|
45
|
+
}
|
|
46
|
+
/** Write-only map interface (i.e. a drain into which key-value pairs can be "sunk") */
|
|
47
|
+
export interface IMapSink<K = any, V = any> {
|
|
48
|
+
/** Returns true if an element in the map object existed and has been
|
|
49
|
+
* removed, or false if the element did not exist. */
|
|
50
|
+
delete(key: K): boolean;
|
|
51
|
+
/** Sets the value for the key in the map object (the return value is
|
|
52
|
+
* boolean in BTree but Map returns the Map itself.) */
|
|
53
|
+
set(key: K, value: V): any;
|
|
54
|
+
/** Removes all key/value pairs from the IMap object. */
|
|
55
|
+
clear(): void;
|
|
56
|
+
}
|
|
57
|
+
/** Set interface.
|
|
58
|
+
* Note: BTree<K,V> does not officially implement this interface, but BTree<K>
|
|
59
|
+
* can be used as an instance of ISet<K>. */
|
|
60
|
+
export interface ISet<K = any> extends ISetSource<K>, ISetSink<K> {
|
|
61
|
+
}
|
|
62
|
+
/** An interface compatible with ES6 Map and BTree. This interface does not
|
|
63
|
+
* describe the complete interface of either class, but merely the common
|
|
64
|
+
* interface shared by both. */
|
|
65
|
+
export interface IMap<K = any, V = any> extends IMapSource<K, V>, IMapSink<K, V> {
|
|
66
|
+
}
|
|
67
|
+
/** An data source that provides read-only access to a set of items called
|
|
68
|
+
* "keys" in sorted order. This is a subinterface of ISortedMapSource. */
|
|
69
|
+
export interface ISortedSetSource<K = any> extends ISetSource<K> {
|
|
70
|
+
/** Gets the lowest key in the collection. */
|
|
71
|
+
minKey(): K | undefined;
|
|
72
|
+
/** Gets the highest key in the collection. */
|
|
73
|
+
maxKey(): K | undefined;
|
|
74
|
+
/** Returns the next key larger than the specified key (or undefined if there is none).
|
|
75
|
+
* Also, nextHigherKey(undefined) returns the lowest key. */
|
|
76
|
+
nextHigherKey(key?: K): K | undefined;
|
|
77
|
+
/** Returns the next key smaller than the specified key (or undefined if there is none).
|
|
78
|
+
* Also, nextLowerKey(undefined) returns the highest key. */
|
|
79
|
+
nextLowerKey(key?: K): K | undefined;
|
|
80
|
+
/** Calls `callback` on the specified range of keys, in ascending order by key.
|
|
81
|
+
* @param low The first key scanned will be greater than or equal to `low`.
|
|
82
|
+
* @param high Scanning stops when a key larger than this is reached.
|
|
83
|
+
* @param includeHigh If the `high` key is present in the map, `onFound` is called
|
|
84
|
+
* for that final pair if and only if this parameter is true.
|
|
85
|
+
* @param onFound A function that is called for each key pair. Because this
|
|
86
|
+
* is a subinterface of ISortedMapSource, if there is a value
|
|
87
|
+
* associated with the key, it is passed as the second parameter.
|
|
88
|
+
* @param initialCounter Initial third argument of `onFound`. This value
|
|
89
|
+
* increases by one each time `onFound` is called. Default: 0
|
|
90
|
+
* @returns Number of pairs found and the number of times `onFound` was called.
|
|
91
|
+
*/
|
|
92
|
+
forRange(low: K, high: K, includeHigh: boolean, onFound?: (k: K, v: any, counter: number) => void, initialCounter?: number): number;
|
|
93
|
+
/** Returns a new iterator for iterating the keys of each pair in ascending order.
|
|
94
|
+
* @param firstKey Minimum key to include in the output. */
|
|
95
|
+
keys(firstKey?: K): IterableIterator<K>;
|
|
96
|
+
}
|
|
97
|
+
/** An data source that provides read-only access to items in sorted order. */
|
|
98
|
+
export interface ISortedMapSource<K = any, V = any> extends IMapSource<K, V>, ISortedSetSource<K> {
|
|
99
|
+
/** Returns the next pair whose key is larger than the specified key (or undefined
|
|
100
|
+
* if there is none). If key === undefined, this function returns the lowest pair. */
|
|
101
|
+
nextHigherPair(key?: K): [K, V] | undefined;
|
|
102
|
+
/** Returns the next pair whose key is smaller than the specified key (or undefined
|
|
103
|
+
* if there is none). If key === undefined, this function returns the highest pair. */
|
|
104
|
+
nextLowerPair(key?: K): [K, V] | undefined;
|
|
105
|
+
/** Builds an array of pairs from the specified range of keys, sorted by key.
|
|
106
|
+
* Each returned pair is also an array: pair[0] is the key, pair[1] is the value.
|
|
107
|
+
* @param low The first key in the array will be greater than or equal to `low`.
|
|
108
|
+
* @param high This method returns when a key larger than this is reached.
|
|
109
|
+
* @param includeHigh If the `high` key is present in the map, its pair will be
|
|
110
|
+
* included in the output if and only if this parameter is true. Note:
|
|
111
|
+
* if the `low` key is present, it is always included in the output.
|
|
112
|
+
* @param maxLength Maximum length of the returned array (default: unlimited)
|
|
113
|
+
* @remarks Computational complexity: O(result.length + log size)
|
|
114
|
+
*/
|
|
115
|
+
getRange(low: K, high: K, includeHigh?: boolean, maxLength?: number): [K, V][];
|
|
116
|
+
/** Calls `callback` on the specified range of keys, in ascending order by key.
|
|
117
|
+
* @param low The first key scanned will be greater than or equal to `low`.
|
|
118
|
+
* @param high Scanning stops when a key larger than this is reached.
|
|
119
|
+
* @param includeHigh If the `high` key is present in the map, `onFound` is called
|
|
120
|
+
* for that final pair if and only if this parameter is true.
|
|
121
|
+
* @param onFound A function that is called for each key-value pair.
|
|
122
|
+
* @param initialCounter Initial third argument of onFound. This value
|
|
123
|
+
* increases by one each time `onFound` is called. Default: 0
|
|
124
|
+
* @returns Number of pairs found and the number of times `callback` was called.
|
|
125
|
+
*/
|
|
126
|
+
forRange(low: K, high: K, includeHigh: boolean, onFound?: (k: K, v: V, counter: number) => void, initialCounter?: number): number;
|
|
127
|
+
/** Returns an iterator that provides items in order by key.
|
|
128
|
+
* @param firstKey Minimum key to include in the output. */
|
|
129
|
+
entries(firstKey?: K): IterableIterator<[K, V]>;
|
|
130
|
+
/** Returns a new iterator for iterating the keys of each pair in ascending order.
|
|
131
|
+
* @param firstKey Minimum key to include in the output. */
|
|
132
|
+
keys(firstKey?: K): IterableIterator<K>;
|
|
133
|
+
/** Returns a new iterator for iterating the values of each pair in order by key.
|
|
134
|
+
* @param firstKey Minimum key whose associated value is included in the output. */
|
|
135
|
+
values(firstKey?: K): IterableIterator<V>;
|
|
136
|
+
/** Performs a reduce operation like the `reduce` method of `Array`.
|
|
137
|
+
* It is used to combine all pairs into a single value, or perform conversions. */
|
|
138
|
+
reduce<R>(callback: (previous: R, currentPair: [K, V], counter: number, tree: IMapF<K, V>) => R, initialValue: R): R;
|
|
139
|
+
/** Performs a reduce operation like the `reduce` method of `Array`.
|
|
140
|
+
* It is used to combine all pairs into a single value, or perform conversions. */
|
|
141
|
+
reduce<R>(callback: (previous: R | undefined, currentPair: [K, V], counter: number, tree: IMapF<K, V>) => R): R | undefined;
|
|
142
|
+
}
|
|
143
|
+
/** An interface for a set of keys (the combination of ISortedSetSource<K> and ISetSink<K>) */
|
|
144
|
+
export interface ISortedSet<K = any> extends ISortedSetSource<K>, ISetSink<K> {
|
|
145
|
+
}
|
|
146
|
+
/** An interface for a sorted map (dictionary),
|
|
147
|
+
* not including functional/persistent methods. */
|
|
148
|
+
export interface ISortedMap<K = any, V = any> extends IMap<K, V>, ISortedMapSource<K, V> {
|
|
149
|
+
/** Adds or overwrites a key-value pair in the sorted map.
|
|
150
|
+
* @param key the key is used to determine the sort order of data in the tree.
|
|
151
|
+
* @param value data to associate with the key
|
|
152
|
+
* @param overwrite Whether to overwrite an existing key-value pair
|
|
153
|
+
* (default: true). If this is false and there is an existing
|
|
154
|
+
* key-value pair then the call to this method has no effect.
|
|
155
|
+
* @returns true if a new key-value pair was added, false if the key
|
|
156
|
+
* already existed. */
|
|
157
|
+
set(key: K, value: V, overwrite?: boolean): boolean;
|
|
158
|
+
/** Adds all pairs from a list of key-value pairs.
|
|
159
|
+
* @param pairs Pairs to add to this tree. If there are duplicate keys,
|
|
160
|
+
* later pairs currently overwrite earlier ones (e.g. [[0,1],[0,7]]
|
|
161
|
+
* associates 0 with 7.)
|
|
162
|
+
* @param overwrite Whether to overwrite pairs that already exist (if false,
|
|
163
|
+
* pairs[i] is ignored when the key pairs[i][0] already exists.)
|
|
164
|
+
* @returns The number of pairs added to the collection.
|
|
165
|
+
*/
|
|
166
|
+
setPairs(pairs: [K, V][], overwrite?: boolean): number;
|
|
167
|
+
/** Deletes a series of keys from the collection. */
|
|
168
|
+
deleteKeys(keys: K[]): number;
|
|
169
|
+
/** Removes a range of key-value pairs from the B+ tree.
|
|
170
|
+
* @param low The first key deleted will be greater than or equal to `low`.
|
|
171
|
+
* @param high Deleting stops when a key larger than this is reached.
|
|
172
|
+
* @param includeHigh Specifies whether the `high` key, if present, is deleted.
|
|
173
|
+
* @returns The number of key-value pairs that were deleted. */
|
|
174
|
+
deleteRange(low: K, high: K, includeHigh: boolean): number;
|
|
175
|
+
entries(firstKey?: K): IterableIterator<[K, V]>;
|
|
176
|
+
keys(firstKey?: K): IterableIterator<K>;
|
|
177
|
+
values(firstKey?: K): IterableIterator<V>;
|
|
178
|
+
}
|
|
179
|
+
/** An interface for a functional set, in which the set object could be read-only
|
|
180
|
+
* but new versions of the set can be created by calling "with" or "without"
|
|
181
|
+
* methods to add or remove keys. This is a subinterface of IMapF<K,V>,
|
|
182
|
+
* so the items in the set may be referred to as "keys". */
|
|
183
|
+
export interface ISetF<K = any> extends ISetSource<K> {
|
|
184
|
+
/** Returns a copy of the set with the specified key included.
|
|
185
|
+
* @remarks You might wonder why this method accepts only one key
|
|
186
|
+
* instead of `...keys: K[]`. The reason is that the derived interface
|
|
187
|
+
* IMapF expects the second parameter to be a value. Therefore
|
|
188
|
+
* withKeys() is provided to set multiple keys at once. */
|
|
189
|
+
with(key: K): ISetF<K>;
|
|
190
|
+
/** Returns a copy of the set with the specified key removed. */
|
|
191
|
+
without(key: K): ISetF<K>;
|
|
192
|
+
/** Returns a copy of the tree with all the keys in the specified array present.
|
|
193
|
+
* @param keys The keys to add.
|
|
194
|
+
* @param returnThisIfUnchanged If true, the method returns `this` when
|
|
195
|
+
* all of the keys are already present in the collection. The
|
|
196
|
+
* default value may be true or false depending on the concrete
|
|
197
|
+
* implementation of the interface (in BTree, the default is false.) */
|
|
198
|
+
withKeys(keys: K[], returnThisIfUnchanged?: boolean): ISetF<K>;
|
|
199
|
+
/** Returns a copy of the tree with all the keys in the specified array removed. */
|
|
200
|
+
withoutKeys(keys: K[], returnThisIfUnchanged?: boolean): ISetF<K>;
|
|
201
|
+
/** Returns a copy of the tree with items removed whenever the callback
|
|
202
|
+
* function returns false.
|
|
203
|
+
* @param callback A function to call for each item in the set.
|
|
204
|
+
* The second parameter to `callback` exists because ISetF
|
|
205
|
+
* is a subinterface of IMapF. If the object is a map, v
|
|
206
|
+
* is the value associated with the key, otherwise v could be
|
|
207
|
+
* undefined or another copy of the third parameter (counter). */
|
|
208
|
+
filter(callback: (k: K, v: any, counter: number) => boolean, returnThisIfUnchanged?: boolean): ISetF<K>;
|
|
209
|
+
}
|
|
210
|
+
/** An interface for a functional map, in which the map object could be read-only
|
|
211
|
+
* but new versions of the map can be created by calling "with" or "without"
|
|
212
|
+
* methods to add or remove keys or key-value pairs.
|
|
213
|
+
*/
|
|
214
|
+
export interface IMapF<K = any, V = any> extends IMapSource<K, V>, ISetF<K> {
|
|
215
|
+
/** Returns a copy of the tree with the specified key set (the value is undefined). */
|
|
216
|
+
with(key: K): IMapF<K, V | undefined>;
|
|
217
|
+
/** Returns a copy of the tree with the specified key-value pair set. */
|
|
218
|
+
with<V2>(key: K, value: V2, overwrite?: boolean): IMapF<K, V | V2>;
|
|
219
|
+
/** Returns a copy of the tree with the specified key-value pairs set. */
|
|
220
|
+
withPairs<V2>(pairs: [K, V | V2][], overwrite: boolean): IMapF<K, V | V2>;
|
|
221
|
+
/** Returns a copy of the tree with all the keys in the specified array present.
|
|
222
|
+
* @param keys The keys to add. If a key is already present in the tree,
|
|
223
|
+
* neither the existing key nor the existing value is modified.
|
|
224
|
+
* @param returnThisIfUnchanged If true, the method returns `this` when
|
|
225
|
+
* all of the keys are already present in the collection. The
|
|
226
|
+
* default value may be true or false depending on the concrete
|
|
227
|
+
* implementation of the interface (in BTree, the default is false.) */
|
|
228
|
+
withKeys(keys: K[], returnThisIfUnchanged?: boolean): IMapF<K, V | undefined>;
|
|
229
|
+
/** Returns a copy of the tree with all values altered by a callback function. */
|
|
230
|
+
mapValues<R>(callback: (v: V, k: K, counter: number) => R): IMapF<K, R>;
|
|
231
|
+
/** Performs a reduce operation like the `reduce` method of `Array`.
|
|
232
|
+
* It is used to combine all pairs into a single value, or perform conversions. */
|
|
233
|
+
reduce<R>(callback: (previous: R, currentPair: [K, V], counter: number, tree: IMapF<K, V>) => R, initialValue: R): R;
|
|
234
|
+
/** Performs a reduce operation like the `reduce` method of `Array`.
|
|
235
|
+
* It is used to combine all pairs into a single value, or perform conversions. */
|
|
236
|
+
reduce<R>(callback: (previous: R | undefined, currentPair: [K, V], counter: number, tree: IMapF<K, V>) => R): R | undefined;
|
|
237
|
+
without(key: K): IMapF<K, V>;
|
|
238
|
+
withoutKeys(keys: K[], returnThisIfUnchanged?: boolean): IMapF<K, V>;
|
|
239
|
+
/** Returns a copy of the tree with pairs removed whenever the callback
|
|
240
|
+
* function returns false. */
|
|
241
|
+
filter(callback: (k: K, v: V, counter: number) => boolean, returnThisIfUnchanged?: boolean): IMapF<K, V>;
|
|
242
|
+
}
|
|
243
|
+
/** An interface for a functional sorted set: a functional set in which the
|
|
244
|
+
* keys (items) are sorted. This is a subinterface of ISortedMapF. */
|
|
245
|
+
export interface ISortedSetF<K = any> extends ISetF<K>, ISortedSetSource<K> {
|
|
246
|
+
keys(firstKey?: K): IterableIterator<K>;
|
|
247
|
+
}
|
|
248
|
+
export interface ISortedMapF<K = any, V = any> extends ISortedSetF<K>, IMapF<K, V>, ISortedMapSource<K, V> {
|
|
249
|
+
/** Returns a copy of the tree with the specified range of keys removed. */
|
|
250
|
+
withoutRange(low: K, high: K, includeHigh: boolean, returnThisIfUnchanged?: boolean): ISortedMapF<K, V>;
|
|
251
|
+
entries(firstKey?: K): IterableIterator<[K, V]>;
|
|
252
|
+
keys(firstKey?: K): IterableIterator<K>;
|
|
253
|
+
values(firstKey?: K): IterableIterator<V>;
|
|
254
|
+
forRange(low: K, high: K, includeHigh: boolean, onFound?: (k: K, v: V, counter: number) => void, initialCounter?: number): number;
|
|
255
|
+
with(key: K): ISortedMapF<K, V | undefined>;
|
|
256
|
+
with<V2>(key: K, value: V2, overwrite?: boolean): ISortedMapF<K, V | V2>;
|
|
257
|
+
withKeys(keys: K[], returnThisIfUnchanged?: boolean): ISortedMapF<K, V | undefined>;
|
|
258
|
+
withPairs<V2>(pairs: [K, V | V2][], overwrite: boolean): ISortedMapF<K, V | V2>;
|
|
259
|
+
mapValues<R>(callback: (v: V, k: K, counter: number) => R): ISortedMapF<K, R>;
|
|
260
|
+
without(key: K): ISortedMapF<K, V>;
|
|
261
|
+
withoutKeys(keys: K[], returnThisIfUnchanged?: boolean): ISortedMapF<K, V>;
|
|
262
|
+
filter(callback: (k: K, v: any, counter: number) => boolean, returnThisIfUnchanged?: boolean): ISortedMapF<K, V>;
|
|
263
|
+
}
|
|
264
|
+
export interface ISortedMapConstructor<K, V> {
|
|
265
|
+
new (entries?: [K, V][], compare?: (a: K, b: K) => number): ISortedMap<K, V>;
|
|
266
|
+
}
|
|
267
|
+
export interface ISortedMapFConstructor<K, V> {
|
|
268
|
+
new (entries?: [K, V][], compare?: (a: K, b: K) => number): ISortedMapF<K, V>;
|
|
269
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ReactivityAdapter } from './reactivity.js';
|
|
2
|
+
/** A `ReactivityAdapter` backed by MobX. Install it once at app startup, before rendering:
|
|
3
|
+
*
|
|
4
|
+
* import { setReactivityAdapter } from 'datatable-tree';
|
|
5
|
+
* import { mobxReactivityAdapter } from 'datatable-tree/mobx';
|
|
6
|
+
*
|
|
7
|
+
* setReactivityAdapter(mobxReactivityAdapter);
|
|
8
|
+
*
|
|
9
|
+
* With this adapter, all list/tree/table components behave as mobx-react `observer`s:
|
|
10
|
+
* rows re-render when observable row data mutates, and props like `selection`,
|
|
11
|
+
* `expandedParents` and `columnsToSortBy` can be MobX observable collections. */
|
|
12
|
+
export declare const mobxReactivityAdapter: ReactivityAdapter;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Optional entry point (`datatable-tree/mobx`): a ReactivityAdapter backed by MobX.
|
|
2
|
+
// Requires the optional peer dependencies `mobx` and `mobx-react` to be installed.
|
|
3
|
+
import { computed, observable, runInAction } from 'mobx';
|
|
4
|
+
import { observer } from 'mobx-react';
|
|
5
|
+
/** A `ReactivityAdapter` backed by MobX. Install it once at app startup, before rendering:
|
|
6
|
+
*
|
|
7
|
+
* import { setReactivityAdapter } from 'datatable-tree';
|
|
8
|
+
* import { mobxReactivityAdapter } from 'datatable-tree/mobx';
|
|
9
|
+
*
|
|
10
|
+
* setReactivityAdapter(mobxReactivityAdapter);
|
|
11
|
+
*
|
|
12
|
+
* With this adapter, all list/tree/table components behave as mobx-react `observer`s:
|
|
13
|
+
* rows re-render when observable row data mutates, and props like `selection`,
|
|
14
|
+
* `expandedParents` and `columnsToSortBy` can be MobX observable collections. */
|
|
15
|
+
export const mobxReactivityAdapter = {
|
|
16
|
+
observer: observer,
|
|
17
|
+
runInAction: f => runInAction(f),
|
|
18
|
+
observableSet: () => observable.set(),
|
|
19
|
+
observableArray: () => observable.array(),
|
|
20
|
+
computed: (f) => computed(f),
|
|
21
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ISet } from './interfaces.js';
|
|
3
|
+
/**
|
|
4
|
+
* Reactivity adapter
|
|
5
|
+
* ------------------
|
|
6
|
+
*
|
|
7
|
+
* The list/tree/table components in this package re-render in response to state changes:
|
|
8
|
+
* changes to the selection, to the set of expanded parents, to the sort order, and (in apps
|
|
9
|
+
* built on an observability library) changes to the row data itself. Rather than depend on a
|
|
10
|
+
* particular state-management library, the components route all such behavior through a
|
|
11
|
+
* `ReactivityAdapter`.
|
|
12
|
+
*
|
|
13
|
+
* The default adapter (`plainReactivityAdapter`) has no dependencies: components re-render
|
|
14
|
+
* via ordinary React state updates, and collections created by `observableSet`/
|
|
15
|
+
* `observableArray` are `WatchableSet`/`WatchableArray` instances that notify the components
|
|
16
|
+
* using them. What the default adapter cannot do is detect mutations of *plain* Sets/arrays
|
|
17
|
+
* you pass as props, or mutations of row data; pass new object/collection instances (or
|
|
18
|
+
* `WatchableSet`/`WatchableArray`) when changing them from outside, as is normal in React.
|
|
19
|
+
*
|
|
20
|
+
* If your app uses MobX, call `setReactivityAdapter(mobxReactivityAdapter)` (see the
|
|
21
|
+
* `datatable-tree/mobx` entry point) once at startup, before rendering. Then all components
|
|
22
|
+
* behave as `observer`s: rows re-render when observable row data mutates, and you can pass
|
|
23
|
+
* `observable.set`/`observable.array` for selection/expansion/sort state.
|
|
24
|
+
*
|
|
25
|
+
* Each adapter member is named after the MobX API it abstracts.
|
|
26
|
+
*/
|
|
27
|
+
export interface ReactivityAdapter {
|
|
28
|
+
/** Wraps a component so that it re-renders when observable values read during its last
|
|
29
|
+
* render change (mobx-react `observer`). Default: returns the component unchanged. */
|
|
30
|
+
observer<C extends React.ComponentType<any>>(component: C): C;
|
|
31
|
+
/** Runs a group of state mutations as a single atomic action, so observers see one
|
|
32
|
+
* change notification rather than several (mobx `runInAction`). Default: `f()`. */
|
|
33
|
+
runInAction(f: () => void): void;
|
|
34
|
+
/** Creates the Set used for default selection/expandedParents state when the caller
|
|
35
|
+
* doesn't provide one (mobx `observable.set`). Default: a `WatchableSet`. */
|
|
36
|
+
observableSet<K>(): ISet<K>;
|
|
37
|
+
/** Creates the array used for default `columnsToSortBy` state when the caller doesn't
|
|
38
|
+
* provide one (mobx `observable.array`). Default: a `WatchableArray`. */
|
|
39
|
+
observableArray<T>(): WatchableArrayLike<T>;
|
|
40
|
+
/** Creates a lazily-(re)computed derived value (mobx `computed`). The default adapter
|
|
41
|
+
* does not cache: `get()` simply calls `f()`, which is correct but repeats the
|
|
42
|
+
* computation once per render. */
|
|
43
|
+
computed<T>(f: () => T): {
|
|
44
|
+
get(): T;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** The array type accepted for `columnsToSortBy`: a mutable array with a `clear()` method.
|
|
48
|
+
* Satisfied by MobX's `IObservableArray` and by this package's `WatchableArray`. */
|
|
49
|
+
export type WatchableArrayLike<T> = Array<T> & {
|
|
50
|
+
clear(): void;
|
|
51
|
+
};
|
|
52
|
+
/** Interface implemented by collections that can notify components of their mutations.
|
|
53
|
+
* The list components subscribe (via `useWatched`) to any prop collection that has a
|
|
54
|
+
* `subscribe` method, so they re-render when it changes. */
|
|
55
|
+
export interface Watchable {
|
|
56
|
+
/** Registers a mutation listener and returns a function that unregisters it. */
|
|
57
|
+
subscribe(onChange: () => void): () => void;
|
|
58
|
+
}
|
|
59
|
+
/** A `Set` that implements `Watchable`, notifying subscribers after add/delete/clear. */
|
|
60
|
+
export declare class WatchableSet<K> extends Set<K> implements Watchable {
|
|
61
|
+
private listeners;
|
|
62
|
+
subscribe(onChange: () => void): () => void;
|
|
63
|
+
protected notify(): void;
|
|
64
|
+
add(key: K): this;
|
|
65
|
+
delete(key: K): boolean;
|
|
66
|
+
clear(): void;
|
|
67
|
+
}
|
|
68
|
+
/** An `Array` that implements `Watchable`, notifying subscribers after mutations made
|
|
69
|
+
* through the standard mutator methods (push/pop/shift/unshift/splice/sort/reverse) or
|
|
70
|
+
* `clear`. Note: direct writes like `a[0] = x` or `a.length = 0` do NOT notify. */
|
|
71
|
+
export declare class WatchableArray<T> extends Array<T> implements Watchable {
|
|
72
|
+
private listeners?;
|
|
73
|
+
subscribe(onChange: () => void): () => void;
|
|
74
|
+
protected notify(): void;
|
|
75
|
+
clear(): void;
|
|
76
|
+
push(...items: T[]): number;
|
|
77
|
+
pop(): T | undefined;
|
|
78
|
+
shift(): T | undefined;
|
|
79
|
+
unshift(...items: T[]): number;
|
|
80
|
+
splice(start: number, deleteCount?: number, ...items: T[]): T[];
|
|
81
|
+
sort(compareFn?: (a: T, b: T) => number): this;
|
|
82
|
+
reverse(): this;
|
|
83
|
+
}
|
|
84
|
+
/** The default adapter: plain React, no external dependencies. */
|
|
85
|
+
export declare const plainReactivityAdapter: ReactivityAdapter;
|
|
86
|
+
/** Installs a `ReactivityAdapter`. Call once at app startup, before rendering any of this
|
|
87
|
+
* package's components (components adopt the adapter on their next render, but state
|
|
88
|
+
* created with a previous adapter is not converted). */
|
|
89
|
+
export declare function setReactivityAdapter(newAdapter: ReactivityAdapter): void;
|
|
90
|
+
export declare function getReactivityAdapter(): ReactivityAdapter;
|
|
91
|
+
/** Wraps a component with `adapter.observer` lazily, at first render rather than at module
|
|
92
|
+
* load time, so that `setReactivityAdapter` works no matter when the app calls it during
|
|
93
|
+
* startup. (A module-scope `observer(...)` call would capture the default adapter, because
|
|
94
|
+
* imported modules run before the importing module's own code.) */
|
|
95
|
+
export declare function reactive<C extends React.ComponentType<any>>(component: C): C;
|
|
96
|
+
/** Wraps a function so that its body runs inside `adapter.runInAction`, preserving `this`
|
|
97
|
+
* and arguments (replaces mobx `action(fn)` for event handlers). */
|
|
98
|
+
export declare function asAction<F extends (...args: any[]) => void>(fn: F): F;
|
|
99
|
+
/** Re-renders the calling component when `collection` reports a mutation. Works with any
|
|
100
|
+
* prop value: subscribes only if the object implements `Watchable` (e.g. `WatchableSet`,
|
|
101
|
+
* `WatchableArray`); plain Sets/arrays and MobX collections are left to their own devices
|
|
102
|
+
* (MobX collections notify through `observer` instead). */
|
|
103
|
+
export declare function useWatched<T>(collection: T): T;
|
|
104
|
+
/** Combines useMemo() with the adapter's computed() so that a computation is not repeated
|
|
105
|
+
* every render. Keep in mind that local variables captured in the `computation` lambda will
|
|
106
|
+
* often be outdated since the same lambda is re-used across multiple renders.
|
|
107
|
+
*
|
|
108
|
+
* NOTE: with the MobX adapter, `computed()` can only cache inside an `observer()` component;
|
|
109
|
+
* with the default adapter it recomputes on every `get()`. */
|
|
110
|
+
export declare function useComputed<T>(computation: () => T, deps?: ReadonlyArray<any>): {
|
|
111
|
+
get(): T;
|
|
112
|
+
};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import React, { useEffect, useMemo } from 'react';
|
|
2
|
+
import { useForceUpdate } from './hooks.js';
|
|
3
|
+
/** A `Set` that implements `Watchable`, notifying subscribers after add/delete/clear. */
|
|
4
|
+
export class WatchableSet extends Set {
|
|
5
|
+
constructor() {
|
|
6
|
+
super(...arguments);
|
|
7
|
+
this.listeners = new Set();
|
|
8
|
+
}
|
|
9
|
+
subscribe(onChange) {
|
|
10
|
+
this.listeners.add(onChange);
|
|
11
|
+
return () => this.listeners.delete(onChange);
|
|
12
|
+
}
|
|
13
|
+
notify() {
|
|
14
|
+
this.listeners.forEach(f => f());
|
|
15
|
+
}
|
|
16
|
+
add(key) {
|
|
17
|
+
if (!super.has(key)) {
|
|
18
|
+
super.add(key);
|
|
19
|
+
this.notify();
|
|
20
|
+
}
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
delete(key) {
|
|
24
|
+
const deleted = super.delete(key);
|
|
25
|
+
if (deleted) {
|
|
26
|
+
this.notify();
|
|
27
|
+
}
|
|
28
|
+
return deleted;
|
|
29
|
+
}
|
|
30
|
+
clear() {
|
|
31
|
+
if (super.size > 0) {
|
|
32
|
+
super.clear();
|
|
33
|
+
this.notify();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** An `Array` that implements `Watchable`, notifying subscribers after mutations made
|
|
38
|
+
* through the standard mutator methods (push/pop/shift/unshift/splice/sort/reverse) or
|
|
39
|
+
* `clear`. Note: direct writes like `a[0] = x` or `a.length = 0` do NOT notify. */
|
|
40
|
+
export class WatchableArray extends Array {
|
|
41
|
+
subscribe(onChange) {
|
|
42
|
+
var _a;
|
|
43
|
+
((_a = this.listeners) !== null && _a !== void 0 ? _a : (this.listeners = new Set())).add(onChange);
|
|
44
|
+
return () => this.listeners.delete(onChange);
|
|
45
|
+
}
|
|
46
|
+
notify() {
|
|
47
|
+
var _a;
|
|
48
|
+
(_a = this.listeners) === null || _a === void 0 ? void 0 : _a.forEach(f => f());
|
|
49
|
+
}
|
|
50
|
+
clear() {
|
|
51
|
+
this.length = 0;
|
|
52
|
+
this.notify();
|
|
53
|
+
}
|
|
54
|
+
push(...items) {
|
|
55
|
+
const result = super.push(...items);
|
|
56
|
+
this.notify();
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
pop() {
|
|
60
|
+
const result = super.pop();
|
|
61
|
+
this.notify();
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
shift() {
|
|
65
|
+
const result = super.shift();
|
|
66
|
+
this.notify();
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
unshift(...items) {
|
|
70
|
+
const result = super.unshift(...items);
|
|
71
|
+
this.notify();
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
splice(start, deleteCount, ...items) {
|
|
75
|
+
const result = deleteCount === undefined && items.length === 0
|
|
76
|
+
? super.splice(start)
|
|
77
|
+
: super.splice(start, deleteCount, ...items);
|
|
78
|
+
this.notify();
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
sort(compareFn) {
|
|
82
|
+
super.sort(compareFn);
|
|
83
|
+
this.notify();
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
reverse() {
|
|
87
|
+
super.reverse();
|
|
88
|
+
this.notify();
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** The default adapter: plain React, no external dependencies. */
|
|
93
|
+
export const plainReactivityAdapter = {
|
|
94
|
+
observer: component => component,
|
|
95
|
+
runInAction: f => f(),
|
|
96
|
+
observableSet: () => new WatchableSet(),
|
|
97
|
+
observableArray: () => new WatchableArray(),
|
|
98
|
+
computed: f => ({ get: f }),
|
|
99
|
+
};
|
|
100
|
+
let adapter = plainReactivityAdapter;
|
|
101
|
+
/** Installs a `ReactivityAdapter`. Call once at app startup, before rendering any of this
|
|
102
|
+
* package's components (components adopt the adapter on their next render, but state
|
|
103
|
+
* created with a previous adapter is not converted). */
|
|
104
|
+
export function setReactivityAdapter(newAdapter) {
|
|
105
|
+
adapter = newAdapter;
|
|
106
|
+
}
|
|
107
|
+
export function getReactivityAdapter() {
|
|
108
|
+
return adapter;
|
|
109
|
+
}
|
|
110
|
+
/** Wraps a component with `adapter.observer` lazily, at first render rather than at module
|
|
111
|
+
* load time, so that `setReactivityAdapter` works no matter when the app calls it during
|
|
112
|
+
* startup. (A module-scope `observer(...)` call would capture the default adapter, because
|
|
113
|
+
* imported modules run before the importing module's own code.) */
|
|
114
|
+
export function reactive(component) {
|
|
115
|
+
var _a;
|
|
116
|
+
let wrapped;
|
|
117
|
+
let wrappedWith;
|
|
118
|
+
const Reactive = (props) => {
|
|
119
|
+
if (wrappedWith !== adapter) {
|
|
120
|
+
wrapped = adapter.observer(component);
|
|
121
|
+
wrappedWith = adapter;
|
|
122
|
+
}
|
|
123
|
+
return React.createElement(wrapped, props);
|
|
124
|
+
};
|
|
125
|
+
Reactive.displayName = (_a = component.displayName) !== null && _a !== void 0 ? _a : component.name;
|
|
126
|
+
return Reactive;
|
|
127
|
+
}
|
|
128
|
+
/** Wraps a function so that its body runs inside `adapter.runInAction`, preserving `this`
|
|
129
|
+
* and arguments (replaces mobx `action(fn)` for event handlers). */
|
|
130
|
+
export function asAction(fn) {
|
|
131
|
+
return function (...args) {
|
|
132
|
+
adapter.runInAction(() => fn.apply(this, args));
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/** Re-renders the calling component when `collection` reports a mutation. Works with any
|
|
136
|
+
* prop value: subscribes only if the object implements `Watchable` (e.g. `WatchableSet`,
|
|
137
|
+
* `WatchableArray`); plain Sets/arrays and MobX collections are left to their own devices
|
|
138
|
+
* (MobX collections notify through `observer` instead). */
|
|
139
|
+
export function useWatched(collection) {
|
|
140
|
+
const forceUpdate = useForceUpdate();
|
|
141
|
+
const watchable = (collection === null || collection === void 0 ? void 0 : collection.subscribe)
|
|
142
|
+
? collection : undefined;
|
|
143
|
+
useEffect(() => watchable === null || watchable === void 0 ? void 0 : watchable.subscribe(forceUpdate), [watchable, forceUpdate]);
|
|
144
|
+
return collection;
|
|
145
|
+
}
|
|
146
|
+
/** Combines useMemo() with the adapter's computed() so that a computation is not repeated
|
|
147
|
+
* every render. Keep in mind that local variables captured in the `computation` lambda will
|
|
148
|
+
* often be outdated since the same lambda is re-used across multiple renders.
|
|
149
|
+
*
|
|
150
|
+
* NOTE: with the MobX adapter, `computed()` can only cache inside an `observer()` component;
|
|
151
|
+
* with the default adapter it recomputes on every `get()`. */
|
|
152
|
+
export function useComputed(computation, deps = []) {
|
|
153
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
154
|
+
return useMemo(() => adapter.computed(computation), deps);
|
|
155
|
+
}
|
package/dist/styles.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { AriaRole } from 'react';
|
|
2
|
+
/** `style` and `class` properties. The type has a short name, deliberately, so
|
|
3
|
+
that it is convenient to use often. Note: `class` is a synonym for `className`;
|
|
4
|
+
you can't use both properties at once (or rather, className takes precedence). */
|
|
5
|
+
export interface Styles {
|
|
6
|
+
style?: React.CSSProperties;
|
|
7
|
+
class?: string;
|
|
8
|
+
className?: string;
|
|
9
|
+
useDefaultClass?: boolean;
|
|
10
|
+
role?: AriaRole;
|
|
11
|
+
}
|
|
12
|
+
/** Combines a Styles object with an optional default class and default CSSProperties
|
|
13
|
+
* to produce a pair { style, className }. A user-defined class name in `s` replaces
|
|
14
|
+
* the default class. Each individual style in `s` can replace individual styles in
|
|
15
|
+
* `defaultStyle`, but non-conflicting styles are merged.
|
|
16
|
+
*
|
|
17
|
+
* This function can also be used to simply convert { style, className, ... } to
|
|
18
|
+
* { style, className }, removing additional properties. Components that accept
|
|
19
|
+
* Styles can use this method to easily set their class/style, e.g.
|
|
20
|
+
*
|
|
21
|
+
* export function MyComponent(p: Styles & { children: React.ReactNode; }) {
|
|
22
|
+
* const styles = useStyles();
|
|
23
|
+
* return (<div {...classAndStyle(p, styles.mycomp)}>{p.children}</div>);
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* Using the Styles type, it is straightforward to allow components to change styles in
|
|
27
|
+
* grandchildren elements, e.g. the `onTable` and `onTableBody` props in this example:
|
|
28
|
+
*
|
|
29
|
+
* export function WrappedTable(p: Styles & {
|
|
30
|
+
* children: React.ReactNode;
|
|
31
|
+
* onTable?: Styles;
|
|
32
|
+
* onTableBody?: Styles;
|
|
33
|
+
* }) {
|
|
34
|
+
* const styles = useStyles();
|
|
35
|
+
* return (
|
|
36
|
+
* <div {...classAndStyle(p, styles.container)}>
|
|
37
|
+
* <Table {...classAndStyle(p.onTable, styles.table)}>
|
|
38
|
+
* <TableBody {...classAndStyle(p.onTableBody)}>
|
|
39
|
+
* {p.children}
|
|
40
|
+
* </TableBody>
|
|
41
|
+
* </Table>
|
|
42
|
+
* </div>
|
|
43
|
+
* );
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* In this example, TableBody is a MUI component that accepts className and style
|
|
47
|
+
* (but not `class`). Given a child component that accepts `Styles`, you can
|
|
48
|
+
* write <Child {...p.onChild}/> rather than <Child {...classAndStyle(p.onChild)}/>. */
|
|
49
|
+
export declare function classAndStyle(s?: Styles | null, defaultClass?: string, defaultStyle?: React.CSSProperties): {
|
|
50
|
+
style?: React.CSSProperties;
|
|
51
|
+
className?: string;
|
|
52
|
+
role?: AriaRole;
|
|
53
|
+
} | undefined;
|
|
54
|
+
/** Same as `classAndStyle` except that other properties from `s` are also copied to the output. */
|
|
55
|
+
export declare function classAndStyleEtc(s: Styles, defaultClass?: string, defaultStyle?: React.CSSProperties): {
|
|
56
|
+
style: import("react").CSSProperties | undefined;
|
|
57
|
+
className: string | undefined;
|
|
58
|
+
class?: string;
|
|
59
|
+
useDefaultClass?: boolean;
|
|
60
|
+
role?: AriaRole;
|
|
61
|
+
};
|
|
62
|
+
/** Combines two `CSSProperties|undefined` values, with properties in the first
|
|
63
|
+
* overriding properties in the second. */
|
|
64
|
+
export declare function combineStyle(style?: React.CSSProperties, defaultStyle?: React.CSSProperties): import("react").CSSProperties | undefined;
|