@react-stately/data 3.4.4-nightly.3036 → 3.4.4-nightly.3047
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/dist/main.js +629 -789
- package/dist/main.js.map +1 -1
- package/dist/module.js +617 -780
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/useAsyncList.ts +2 -1
package/dist/main.js
CHANGED
|
@@ -1,832 +1,672 @@
|
|
|
1
|
-
var
|
|
2
|
-
useMemo,
|
|
3
|
-
useState,
|
|
4
|
-
useEffect,
|
|
5
|
-
useReducer
|
|
6
|
-
} = require("react");
|
|
1
|
+
var $huzL3$react = require("react");
|
|
7
2
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
function $parcel$interopDefault(a) {
|
|
11
|
-
return a && a.__esModule ? a.default : a;
|
|
3
|
+
function $parcel$export(e, n, v, s) {
|
|
4
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
12
5
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*/
|
|
18
|
-
function useListData(options) {
|
|
19
|
-
let {
|
|
20
|
-
initialItems = [],
|
|
21
|
-
initialSelectedKeys,
|
|
22
|
-
getKey = item => item.id || item.key,
|
|
23
|
-
filter,
|
|
24
|
-
initialFilterText = ''
|
|
25
|
-
} = options; // Store both items and filteredItems in state so we can go back to the unfiltered list
|
|
26
|
-
|
|
27
|
-
let [state, setState] = useState({
|
|
28
|
-
items: initialItems,
|
|
29
|
-
selectedKeys: initialSelectedKeys === 'all' ? 'all' : new Set(initialSelectedKeys || []),
|
|
30
|
-
filterText: initialFilterText
|
|
31
|
-
});
|
|
32
|
-
let filteredItems = useMemo(() => filter ? state.items.filter(item => filter(item, state.filterText)) : state.items, [state.items, state.filterText, filter]);
|
|
33
|
-
return _babelRuntimeHelpersExtends({}, state, {
|
|
34
|
-
items: filteredItems
|
|
35
|
-
}, $f237d9b45bc5fe911ce76292c069bdd7$export$createListActions({
|
|
36
|
-
getKey
|
|
37
|
-
}, setState), {
|
|
38
|
-
getItem(key) {
|
|
39
|
-
return state.items.find(item => getKey(item) === key);
|
|
6
|
+
function $parcel$exportWildcard(dest, source) {
|
|
7
|
+
Object.keys(source).forEach(function(key) {
|
|
8
|
+
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
|
|
9
|
+
return;
|
|
40
10
|
}
|
|
41
11
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
function $f237d9b45bc5fe911ce76292c069bdd7$export$createListActions(opts, dispatch) {
|
|
48
|
-
let {
|
|
49
|
-
cursor,
|
|
50
|
-
getKey
|
|
51
|
-
} = opts;
|
|
52
|
-
return {
|
|
53
|
-
setSelectedKeys(selectedKeys) {
|
|
54
|
-
dispatch(state => _babelRuntimeHelpersExtends({}, state, {
|
|
55
|
-
selectedKeys
|
|
56
|
-
}));
|
|
57
|
-
},
|
|
58
|
-
|
|
59
|
-
setFilterText(filterText) {
|
|
60
|
-
dispatch(state => _babelRuntimeHelpersExtends({}, state, {
|
|
61
|
-
filterText
|
|
62
|
-
}));
|
|
63
|
-
},
|
|
64
|
-
|
|
65
|
-
insert(index) {
|
|
66
|
-
for (var _len = arguments.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
67
|
-
values[_key - 1] = arguments[_key];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
dispatch(state => $f237d9b45bc5fe911ce76292c069bdd7$var$insert(state, index, ...values));
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
insertBefore(key) {
|
|
74
|
-
for (var _len2 = arguments.length, values = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
75
|
-
values[_key2 - 1] = arguments[_key2];
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
dispatch(state => {
|
|
79
|
-
let index = state.items.findIndex(item => getKey(item) === key);
|
|
80
|
-
|
|
81
|
-
if (index === -1) {
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return $f237d9b45bc5fe911ce76292c069bdd7$var$insert(state, index, ...values);
|
|
86
|
-
});
|
|
87
|
-
},
|
|
88
|
-
|
|
89
|
-
insertAfter(key) {
|
|
90
|
-
for (var _len3 = arguments.length, values = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
|
91
|
-
values[_key3 - 1] = arguments[_key3];
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
dispatch(state => {
|
|
95
|
-
let index = state.items.findIndex(item => getKey(item) === key);
|
|
96
|
-
|
|
97
|
-
if (index === -1) {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return $f237d9b45bc5fe911ce76292c069bdd7$var$insert(state, index + 1, ...values);
|
|
102
|
-
});
|
|
103
|
-
},
|
|
104
|
-
|
|
105
|
-
prepend() {
|
|
106
|
-
for (var _len4 = arguments.length, values = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
|
107
|
-
values[_key4] = arguments[_key4];
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
dispatch(state => $f237d9b45bc5fe911ce76292c069bdd7$var$insert(state, 0, ...values));
|
|
111
|
-
},
|
|
112
|
-
|
|
113
|
-
append() {
|
|
114
|
-
for (var _len5 = arguments.length, values = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
|
|
115
|
-
values[_key5] = arguments[_key5];
|
|
12
|
+
Object.defineProperty(dest, key, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function get() {
|
|
15
|
+
return source[key];
|
|
116
16
|
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
117
19
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
remove() {
|
|
122
|
-
for (var _len6 = arguments.length, keys = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
|
|
123
|
-
keys[_key6] = arguments[_key6];
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
dispatch(state => {
|
|
127
|
-
let keySet = new Set(keys);
|
|
128
|
-
let items = state.items.filter(item => !keySet.has(getKey(item)));
|
|
129
|
-
let selection = 'all';
|
|
130
|
-
|
|
131
|
-
if (state.selectedKeys !== 'all') {
|
|
132
|
-
selection = new Set(state.selectedKeys);
|
|
133
|
-
|
|
134
|
-
for (let key of keys) {
|
|
135
|
-
selection.delete(key);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (cursor == null && items.length === 0) {
|
|
140
|
-
selection = new Set();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return _babelRuntimeHelpersExtends({}, state, {
|
|
144
|
-
items,
|
|
145
|
-
selectedKeys: selection
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
},
|
|
149
|
-
|
|
150
|
-
removeSelectedItems() {
|
|
151
|
-
dispatch(state => {
|
|
152
|
-
if (state.selectedKeys === 'all') {
|
|
153
|
-
return _babelRuntimeHelpersExtends({}, state, {
|
|
154
|
-
items: [],
|
|
155
|
-
selectedKeys: new Set()
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
let selectedKeys = state.selectedKeys;
|
|
160
|
-
let items = state.items.filter(item => !selectedKeys.has(getKey(item)));
|
|
161
|
-
return _babelRuntimeHelpersExtends({}, state, {
|
|
162
|
-
items,
|
|
163
|
-
selectedKeys: new Set()
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
},
|
|
167
|
-
|
|
168
|
-
move(key, toIndex) {
|
|
169
|
-
dispatch(state => {
|
|
170
|
-
let index = state.items.findIndex(item => getKey(item) === key);
|
|
171
|
-
|
|
172
|
-
if (index === -1) {
|
|
173
|
-
return state;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
let copy = state.items.slice();
|
|
177
|
-
let [item] = copy.splice(index, 1);
|
|
178
|
-
copy.splice(toIndex, 0, item);
|
|
179
|
-
return _babelRuntimeHelpersExtends({}, state, {
|
|
180
|
-
items: copy
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
},
|
|
184
|
-
|
|
185
|
-
moveBefore(key, keys) {
|
|
186
|
-
dispatch(state => {
|
|
187
|
-
let toIndex = state.items.findIndex(item => getKey(item) === key);
|
|
188
|
-
|
|
189
|
-
if (toIndex === -1) {
|
|
190
|
-
return state;
|
|
191
|
-
} // Find indices of keys to move. Sort them so that the order in the list is retained.
|
|
192
|
-
|
|
20
|
+
return dest;
|
|
21
|
+
}
|
|
193
22
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
});
|
|
197
|
-
},
|
|
23
|
+
$parcel$export(module.exports, "useListData", () => $7eaa5a0dc0e5bafc$export$762f73dccccd255d);
|
|
24
|
+
var $c690ccebce22d69f$exports = {};
|
|
198
25
|
|
|
199
|
-
|
|
200
|
-
dispatch(state => {
|
|
201
|
-
let toIndex = state.items.findIndex(item => getKey(item) === key);
|
|
26
|
+
$parcel$export($c690ccebce22d69f$exports, "useAsyncList", () => $c690ccebce22d69f$export$bc3384a35de93d66);
|
|
202
27
|
|
|
203
|
-
|
|
204
|
-
|
|
28
|
+
function $7eaa5a0dc0e5bafc$export$762f73dccccd255d(options) {
|
|
29
|
+
let { initialItems: initialItems = [] , initialSelectedKeys: initialSelectedKeys , getKey: getKey = (item)=>item.id || item.key
|
|
30
|
+
, filter: filter , initialFilterText: initialFilterText = '' } = options;
|
|
31
|
+
// Store both items and filteredItems in state so we can go back to the unfiltered list
|
|
32
|
+
let [state, setState] = $huzL3$react.useState({
|
|
33
|
+
items: initialItems,
|
|
34
|
+
selectedKeys: initialSelectedKeys === 'all' ? 'all' : new Set(initialSelectedKeys || []),
|
|
35
|
+
filterText: initialFilterText
|
|
36
|
+
});
|
|
37
|
+
let filteredItems = $huzL3$react.useMemo(()=>filter ? state.items.filter((item)=>filter(item, state.filterText)
|
|
38
|
+
) : state.items
|
|
39
|
+
, [
|
|
40
|
+
state.items,
|
|
41
|
+
state.filterText,
|
|
42
|
+
filter
|
|
43
|
+
]);
|
|
44
|
+
return {
|
|
45
|
+
...state,
|
|
46
|
+
items: filteredItems,
|
|
47
|
+
...$7eaa5a0dc0e5bafc$export$79c0c687a5963b0a({
|
|
48
|
+
getKey: getKey
|
|
49
|
+
}, setState),
|
|
50
|
+
getItem (key) {
|
|
51
|
+
return state.items.find((item)=>getKey(item) === key
|
|
52
|
+
);
|
|
205
53
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function $7eaa5a0dc0e5bafc$export$79c0c687a5963b0a(opts, dispatch) {
|
|
57
|
+
let { cursor: cursor , getKey: getKey } = opts;
|
|
58
|
+
return {
|
|
59
|
+
setSelectedKeys (selectedKeys) {
|
|
60
|
+
dispatch((state)=>({
|
|
61
|
+
...state,
|
|
62
|
+
selectedKeys: selectedKeys
|
|
63
|
+
})
|
|
64
|
+
);
|
|
65
|
+
},
|
|
66
|
+
setFilterText (filterText) {
|
|
67
|
+
dispatch((state)=>({
|
|
68
|
+
...state,
|
|
69
|
+
filterText: filterText
|
|
70
|
+
})
|
|
71
|
+
);
|
|
72
|
+
},
|
|
73
|
+
insert (index, ...values) {
|
|
74
|
+
dispatch((state)=>$7eaa5a0dc0e5bafc$var$insert(state, index, ...values)
|
|
75
|
+
);
|
|
76
|
+
},
|
|
77
|
+
insertBefore (key, ...values) {
|
|
78
|
+
dispatch((state)=>{
|
|
79
|
+
let index = state.items.findIndex((item)=>getKey(item) === key
|
|
80
|
+
);
|
|
81
|
+
if (index === -1) return;
|
|
82
|
+
return $7eaa5a0dc0e5bafc$var$insert(state, index, ...values);
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
insertAfter (key, ...values) {
|
|
86
|
+
dispatch((state)=>{
|
|
87
|
+
let index = state.items.findIndex((item)=>getKey(item) === key
|
|
88
|
+
);
|
|
89
|
+
if (index === -1) return;
|
|
90
|
+
return $7eaa5a0dc0e5bafc$var$insert(state, index + 1, ...values);
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
prepend (...values) {
|
|
94
|
+
dispatch((state)=>$7eaa5a0dc0e5bafc$var$insert(state, 0, ...values)
|
|
95
|
+
);
|
|
96
|
+
},
|
|
97
|
+
append (...values) {
|
|
98
|
+
dispatch((state)=>$7eaa5a0dc0e5bafc$var$insert(state, state.items.length, ...values)
|
|
99
|
+
);
|
|
100
|
+
},
|
|
101
|
+
remove (...keys) {
|
|
102
|
+
dispatch((state)=>{
|
|
103
|
+
let keySet = new Set(keys);
|
|
104
|
+
let items = state.items.filter((item)=>!keySet.has(getKey(item))
|
|
105
|
+
);
|
|
106
|
+
let selection = 'all';
|
|
107
|
+
if (state.selectedKeys !== 'all') {
|
|
108
|
+
selection = new Set(state.selectedKeys);
|
|
109
|
+
for (let key of keys)selection.delete(key);
|
|
110
|
+
}
|
|
111
|
+
if (cursor == null && items.length === 0) selection = new Set();
|
|
112
|
+
return {
|
|
113
|
+
...state,
|
|
114
|
+
items: items,
|
|
115
|
+
selectedKeys: selection
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
},
|
|
119
|
+
removeSelectedItems () {
|
|
120
|
+
dispatch((state)=>{
|
|
121
|
+
if (state.selectedKeys === 'all') return {
|
|
122
|
+
...state,
|
|
123
|
+
items: [],
|
|
124
|
+
selectedKeys: new Set()
|
|
125
|
+
};
|
|
126
|
+
let selectedKeys = state.selectedKeys;
|
|
127
|
+
let items = state.items.filter((item)=>!selectedKeys.has(getKey(item))
|
|
128
|
+
);
|
|
129
|
+
return {
|
|
130
|
+
...state,
|
|
131
|
+
items: items,
|
|
132
|
+
selectedKeys: new Set()
|
|
133
|
+
};
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
move (key, toIndex) {
|
|
137
|
+
dispatch((state)=>{
|
|
138
|
+
let index = state.items.findIndex((item)=>getKey(item) === key
|
|
139
|
+
);
|
|
140
|
+
if (index === -1) return state;
|
|
141
|
+
let copy = state.items.slice();
|
|
142
|
+
let [item1] = copy.splice(index, 1);
|
|
143
|
+
copy.splice(toIndex, 0, item1);
|
|
144
|
+
return {
|
|
145
|
+
...state,
|
|
146
|
+
items: copy
|
|
147
|
+
};
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
moveBefore (key1, keys) {
|
|
151
|
+
dispatch((state)=>{
|
|
152
|
+
let toIndex = state.items.findIndex((item)=>getKey(item) === key1
|
|
153
|
+
);
|
|
154
|
+
if (toIndex === -1) return state;
|
|
155
|
+
// Find indices of keys to move. Sort them so that the order in the list is retained.
|
|
156
|
+
let indices = keys.map((key)=>state.items.findIndex((item)=>getKey(item) === key
|
|
157
|
+
)
|
|
158
|
+
).sort();
|
|
159
|
+
return $7eaa5a0dc0e5bafc$var$move(state, indices, toIndex);
|
|
160
|
+
});
|
|
161
|
+
},
|
|
162
|
+
moveAfter (key2, keys) {
|
|
163
|
+
dispatch((state)=>{
|
|
164
|
+
let toIndex = state.items.findIndex((item)=>getKey(item) === key2
|
|
165
|
+
);
|
|
166
|
+
if (toIndex === -1) return state;
|
|
167
|
+
let indices = keys.map((key)=>state.items.findIndex((item)=>getKey(item) === key
|
|
168
|
+
)
|
|
169
|
+
).sort();
|
|
170
|
+
return $7eaa5a0dc0e5bafc$var$move(state, indices, toIndex + 1);
|
|
171
|
+
});
|
|
172
|
+
},
|
|
173
|
+
update (key, newValue) {
|
|
174
|
+
dispatch((state)=>{
|
|
175
|
+
let index = state.items.findIndex((item)=>getKey(item) === key
|
|
176
|
+
);
|
|
177
|
+
if (index === -1) return state;
|
|
178
|
+
return {
|
|
179
|
+
...state,
|
|
180
|
+
items: [
|
|
181
|
+
...state.items.slice(0, index),
|
|
182
|
+
newValue,
|
|
183
|
+
...state.items.slice(index + 1)
|
|
184
|
+
]
|
|
185
|
+
};
|
|
186
|
+
});
|
|
218
187
|
}
|
|
219
|
-
|
|
220
|
-
return _babelRuntimeHelpersExtends({}, state, {
|
|
221
|
-
items: [...state.items.slice(0, index), newValue, ...state.items.slice(index + 1)]
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
};
|
|
188
|
+
};
|
|
227
189
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
190
|
+
function $7eaa5a0dc0e5bafc$var$insert(state, index, ...values) {
|
|
191
|
+
return {
|
|
192
|
+
...state,
|
|
193
|
+
items: [
|
|
194
|
+
...state.items.slice(0, index),
|
|
195
|
+
...values,
|
|
196
|
+
...state.items.slice(index)
|
|
197
|
+
]
|
|
198
|
+
};
|
|
237
199
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
200
|
+
function $7eaa5a0dc0e5bafc$var$move(state, indices, toIndex) {
|
|
201
|
+
// Shift the target down by the number of items being moved from before the target
|
|
202
|
+
for (let index of indices)if (index < toIndex) toIndex--;
|
|
203
|
+
let moves = indices.map((from)=>({
|
|
204
|
+
from: from,
|
|
205
|
+
to: toIndex++
|
|
206
|
+
})
|
|
207
|
+
);
|
|
208
|
+
// Shift later from indices down if they have a larger index
|
|
209
|
+
for(let i = 0; i < moves.length; i++){
|
|
210
|
+
let a = moves[i].from;
|
|
211
|
+
for(let j = i; j < moves.length; j++){
|
|
212
|
+
let b = moves[j].from;
|
|
213
|
+
if (b > a) moves[j].from--;
|
|
214
|
+
}
|
|
244
215
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
let a = moves[i].from;
|
|
254
|
-
|
|
255
|
-
for (let j = i; j < moves.length; j++) {
|
|
256
|
-
let b = moves[j].from;
|
|
257
|
-
|
|
258
|
-
if (b > a) {
|
|
259
|
-
moves[j].from--;
|
|
260
|
-
}
|
|
216
|
+
// Interleave the moves so they can be applied one by one rather than all at once
|
|
217
|
+
for(let i1 = 0; i1 < moves.length; i1++){
|
|
218
|
+
let a = moves[i1];
|
|
219
|
+
for(let j = moves.length - 1; j > i1; j--){
|
|
220
|
+
let b = moves[j];
|
|
221
|
+
if (b.from < a.to) a.to++;
|
|
222
|
+
else b.from++;
|
|
223
|
+
}
|
|
261
224
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
let a = moves[i];
|
|
267
|
-
|
|
268
|
-
for (let j = moves.length - 1; j > i; j--) {
|
|
269
|
-
let b = moves[j];
|
|
270
|
-
|
|
271
|
-
if (b.from < a.to) {
|
|
272
|
-
a.to++;
|
|
273
|
-
} else {
|
|
274
|
-
b.from++;
|
|
275
|
-
}
|
|
225
|
+
let copy = state.items.slice();
|
|
226
|
+
for (let move of moves){
|
|
227
|
+
let [item] = copy.splice(move.from, 1);
|
|
228
|
+
copy.splice(move.to, 0, item);
|
|
276
229
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
for (let move of moves) {
|
|
282
|
-
let [item] = copy.splice(move.from, 1);
|
|
283
|
-
copy.splice(move.to, 0, item);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
return _babelRuntimeHelpersExtends({}, state, {
|
|
287
|
-
items: copy
|
|
288
|
-
});
|
|
230
|
+
return {
|
|
231
|
+
...state,
|
|
232
|
+
items: copy
|
|
233
|
+
};
|
|
289
234
|
}
|
|
290
235
|
|
|
291
|
-
function $fbe26d4ebdaa302e354dcbb53fc6447$var$reducer(data, action) {
|
|
292
|
-
var _action$filterText, _action$sortDescripto, _action$selectedKeys, _action$filterText2, _action$sortDescripto2, _action$filterText3, _action$selectedKeys2, _action$sortDescripto3, _action$filterText4;
|
|
293
|
-
|
|
294
|
-
let selectedKeys;
|
|
295
|
-
|
|
296
|
-
switch (data.state) {
|
|
297
|
-
case 'idle':
|
|
298
|
-
case 'error':
|
|
299
|
-
switch (action.type) {
|
|
300
|
-
case 'loading':
|
|
301
|
-
case 'loadingMore':
|
|
302
|
-
case 'sorting':
|
|
303
|
-
case 'filtering':
|
|
304
|
-
return _babelRuntimeHelpersExtends({}, data, {
|
|
305
|
-
filterText: (_action$filterText = action.filterText) != null ? _action$filterText : data.filterText,
|
|
306
|
-
state: action.type,
|
|
307
|
-
// Reset items to an empty list if loading, but not when sorting.
|
|
308
|
-
items: action.type === 'loading' ? [] : data.items,
|
|
309
|
-
sortDescriptor: (_action$sortDescripto = action.sortDescriptor) != null ? _action$sortDescripto : data.sortDescriptor,
|
|
310
|
-
abortController: action.abortController
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
case 'update':
|
|
314
|
-
return _babelRuntimeHelpersExtends({}, data, action.updater(data));
|
|
315
|
-
|
|
316
|
-
case 'success':
|
|
317
|
-
case 'error':
|
|
318
|
-
return data;
|
|
319
|
-
|
|
320
|
-
default:
|
|
321
|
-
throw new Error("Invalid action \"" + action.type + "\" in state \"" + data.state + "\"");
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
case 'loading':
|
|
325
|
-
case 'sorting':
|
|
326
|
-
case 'filtering':
|
|
327
|
-
switch (action.type) {
|
|
328
|
-
case 'success':
|
|
329
|
-
// Ignore if there is a newer abortcontroller in state.
|
|
330
|
-
// This means that multiple requests were going at once.
|
|
331
|
-
// We want to take only the latest result.
|
|
332
|
-
if (action.abortController !== data.abortController) {
|
|
333
|
-
return data;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
selectedKeys = (_action$selectedKeys = action.selectedKeys) != null ? _action$selectedKeys : data.selectedKeys;
|
|
337
|
-
return _babelRuntimeHelpersExtends({}, data, {
|
|
338
|
-
filterText: (_action$filterText2 = action.filterText) != null ? _action$filterText2 : data.filterText,
|
|
339
|
-
state: 'idle',
|
|
340
|
-
items: [...action.items],
|
|
341
|
-
selectedKeys: selectedKeys === 'all' ? 'all' : new Set(selectedKeys),
|
|
342
|
-
sortDescriptor: (_action$sortDescripto2 = action.sortDescriptor) != null ? _action$sortDescripto2 : data.sortDescriptor,
|
|
343
|
-
abortController: null,
|
|
344
|
-
cursor: action.cursor
|
|
345
|
-
});
|
|
346
|
-
|
|
347
|
-
case 'error':
|
|
348
|
-
if (action.abortController !== data.abortController) {
|
|
349
|
-
return data;
|
|
350
|
-
}
|
|
351
236
|
|
|
352
|
-
return _babelRuntimeHelpersExtends({}, data, {
|
|
353
|
-
state: 'error',
|
|
354
|
-
error: action.error,
|
|
355
|
-
abortController: null
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
case 'loading':
|
|
359
|
-
case 'loadingMore':
|
|
360
|
-
case 'sorting':
|
|
361
|
-
case 'filtering':
|
|
362
|
-
// We're already loading, and another load was triggered at the same time.
|
|
363
|
-
// We need to abort the previous load and start a new one.
|
|
364
|
-
data.abortController.abort();
|
|
365
|
-
return _babelRuntimeHelpersExtends({}, data, {
|
|
366
|
-
filterText: (_action$filterText3 = action.filterText) != null ? _action$filterText3 : data.filterText,
|
|
367
|
-
state: action.type,
|
|
368
|
-
// Reset items to an empty list if loading, but not when sorting.
|
|
369
|
-
items: action.type === 'loading' ? [] : data.items,
|
|
370
|
-
abortController: action.abortController
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
case 'update':
|
|
374
|
-
// We're already loading, and an update happened at the same time (e.g. selectedKey changed).
|
|
375
|
-
// Update data but don't abort previous load.
|
|
376
|
-
return _babelRuntimeHelpersExtends({}, data, action.updater(data));
|
|
377
|
-
|
|
378
|
-
default:
|
|
379
|
-
throw new Error("Invalid action \"" + action.type + "\" in state \"" + data.state + "\"");
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
case 'loadingMore':
|
|
383
|
-
switch (action.type) {
|
|
384
|
-
case 'success':
|
|
385
|
-
selectedKeys = data.selectedKeys === 'all' || action.selectedKeys === 'all' ? 'all' : new Set([...data.selectedKeys, ...((_action$selectedKeys2 = action.selectedKeys) != null ? _action$selectedKeys2 : [])]); // Append the new items
|
|
386
|
-
|
|
387
|
-
return _babelRuntimeHelpersExtends({}, data, {
|
|
388
|
-
state: 'idle',
|
|
389
|
-
items: [...data.items, ...action.items],
|
|
390
|
-
selectedKeys,
|
|
391
|
-
sortDescriptor: (_action$sortDescripto3 = action.sortDescriptor) != null ? _action$sortDescripto3 : data.sortDescriptor,
|
|
392
|
-
abortController: null,
|
|
393
|
-
cursor: action.cursor
|
|
394
|
-
});
|
|
395
237
|
|
|
238
|
+
function $c690ccebce22d69f$var$reducer(data, action) {
|
|
239
|
+
let selectedKeys;
|
|
240
|
+
switch(data.state){
|
|
241
|
+
case 'idle':
|
|
396
242
|
case 'error':
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
243
|
+
switch(action.type){
|
|
244
|
+
case 'loading':
|
|
245
|
+
case 'loadingMore':
|
|
246
|
+
case 'sorting':
|
|
247
|
+
case 'filtering':
|
|
248
|
+
return {
|
|
249
|
+
...data,
|
|
250
|
+
filterText: action.filterText ?? data.filterText,
|
|
251
|
+
state: action.type,
|
|
252
|
+
// Reset items to an empty list if loading, but not when sorting.
|
|
253
|
+
items: action.type === 'loading' ? [] : data.items,
|
|
254
|
+
sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,
|
|
255
|
+
abortController: action.abortController
|
|
256
|
+
};
|
|
257
|
+
case 'update':
|
|
258
|
+
return {
|
|
259
|
+
...data,
|
|
260
|
+
...action.updater(data)
|
|
261
|
+
};
|
|
262
|
+
case 'success':
|
|
263
|
+
case 'error':
|
|
264
|
+
return data;
|
|
265
|
+
default:
|
|
266
|
+
throw new Error(`Invalid action "${action.type}" in state "${data.state}"`);
|
|
267
|
+
}
|
|
406
268
|
case 'loading':
|
|
407
269
|
case 'sorting':
|
|
408
270
|
case 'filtering':
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
271
|
+
switch(action.type){
|
|
272
|
+
case 'success':
|
|
273
|
+
// Ignore if there is a newer abortcontroller in state.
|
|
274
|
+
// This means that multiple requests were going at once.
|
|
275
|
+
// We want to take only the latest result.
|
|
276
|
+
if (action.abortController !== data.abortController) return data;
|
|
277
|
+
selectedKeys = action.selectedKeys ?? data.selectedKeys;
|
|
278
|
+
return {
|
|
279
|
+
...data,
|
|
280
|
+
filterText: action.filterText ?? data.filterText,
|
|
281
|
+
state: 'idle',
|
|
282
|
+
items: [
|
|
283
|
+
...action.items
|
|
284
|
+
],
|
|
285
|
+
selectedKeys: selectedKeys === 'all' ? 'all' : new Set(selectedKeys),
|
|
286
|
+
sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,
|
|
287
|
+
abortController: null,
|
|
288
|
+
cursor: action.cursor
|
|
289
|
+
};
|
|
290
|
+
case 'error':
|
|
291
|
+
if (action.abortController !== data.abortController) return data;
|
|
292
|
+
return {
|
|
293
|
+
...data,
|
|
294
|
+
state: 'error',
|
|
295
|
+
error: action.error,
|
|
296
|
+
abortController: null
|
|
297
|
+
};
|
|
298
|
+
case 'loading':
|
|
299
|
+
case 'loadingMore':
|
|
300
|
+
case 'sorting':
|
|
301
|
+
case 'filtering':
|
|
302
|
+
// We're already loading, and another load was triggered at the same time.
|
|
303
|
+
// We need to abort the previous load and start a new one.
|
|
304
|
+
data.abortController.abort();
|
|
305
|
+
return {
|
|
306
|
+
...data,
|
|
307
|
+
filterText: action.filterText ?? data.filterText,
|
|
308
|
+
state: action.type,
|
|
309
|
+
// Reset items to an empty list if loading, but not when sorting.
|
|
310
|
+
items: action.type === 'loading' ? [] : data.items,
|
|
311
|
+
abortController: action.abortController
|
|
312
|
+
};
|
|
313
|
+
case 'update':
|
|
314
|
+
// We're already loading, and an update happened at the same time (e.g. selectedKey changed).
|
|
315
|
+
// Update data but don't abort previous load.
|
|
316
|
+
return {
|
|
317
|
+
...data,
|
|
318
|
+
...action.updater(data)
|
|
319
|
+
};
|
|
320
|
+
default:
|
|
321
|
+
throw new Error(`Invalid action "${action.type}" in state "${data.state}"`);
|
|
322
|
+
}
|
|
420
323
|
case 'loadingMore':
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
324
|
+
switch(action.type){
|
|
325
|
+
case 'success':
|
|
326
|
+
selectedKeys = data.selectedKeys === 'all' || action.selectedKeys === 'all' ? 'all' : new Set([
|
|
327
|
+
...data.selectedKeys,
|
|
328
|
+
...action.selectedKeys ?? []
|
|
329
|
+
]);
|
|
330
|
+
// Append the new items
|
|
331
|
+
return {
|
|
332
|
+
...data,
|
|
333
|
+
state: 'idle',
|
|
334
|
+
items: [
|
|
335
|
+
...data.items,
|
|
336
|
+
...action.items
|
|
337
|
+
],
|
|
338
|
+
selectedKeys: selectedKeys,
|
|
339
|
+
sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,
|
|
340
|
+
abortController: null,
|
|
341
|
+
cursor: action.cursor
|
|
342
|
+
};
|
|
343
|
+
case 'error':
|
|
344
|
+
if (action.abortController !== data.abortController) return data;
|
|
345
|
+
return {
|
|
346
|
+
...data,
|
|
347
|
+
state: 'error',
|
|
348
|
+
error: action.error
|
|
349
|
+
};
|
|
350
|
+
case 'loading':
|
|
351
|
+
case 'sorting':
|
|
352
|
+
case 'filtering':
|
|
353
|
+
// We're already loading more, and another load was triggered at the same time.
|
|
354
|
+
// We need to abort the previous load more and start a new one.
|
|
355
|
+
data.abortController.abort();
|
|
356
|
+
return {
|
|
357
|
+
...data,
|
|
358
|
+
filterText: action.filterText ?? data.filterText,
|
|
359
|
+
state: action.type,
|
|
360
|
+
// Reset items to an empty list if loading, but not when sorting.
|
|
361
|
+
items: action.type === 'loading' ? [] : data.items,
|
|
362
|
+
abortController: action.abortController
|
|
363
|
+
};
|
|
364
|
+
case 'loadingMore':
|
|
365
|
+
// If already loading more and another loading more is triggered, abort the new load more since
|
|
366
|
+
// it is a duplicate request since the cursor hasn't been updated.
|
|
367
|
+
// Do not overwrite the data.abortController
|
|
368
|
+
action.abortController.abort();
|
|
369
|
+
return data;
|
|
370
|
+
case 'update':
|
|
371
|
+
// We're already loading, and an update happened at the same time (e.g. selectedKey changed).
|
|
372
|
+
// Update data but don't abort previous load.
|
|
373
|
+
return {
|
|
374
|
+
...data,
|
|
375
|
+
...action.updater(data)
|
|
376
|
+
};
|
|
377
|
+
default:
|
|
378
|
+
throw new Error(`Invalid action "${action.type}" in state "${data.state}"`);
|
|
379
|
+
}
|
|
432
380
|
default:
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
default:
|
|
437
|
-
throw new Error("Invalid state \"" + data.state + "\"");
|
|
438
|
-
}
|
|
381
|
+
throw new Error(`Invalid state "${data.state}"`);
|
|
382
|
+
}
|
|
439
383
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
// Only do this if not aborted (e.g. user triggers another filter action before load completes)
|
|
489
|
-
|
|
490
|
-
if (filterText && filterText !== previousFilterText && !abortController.signal.aborted) {
|
|
384
|
+
function $c690ccebce22d69f$export$bc3384a35de93d66(options) {
|
|
385
|
+
const { load: load , sort: sort , initialSelectedKeys: initialSelectedKeys , initialSortDescriptor: initialSortDescriptor , getKey: getKey = (item)=>item.id || item.key
|
|
386
|
+
, initialFilterText: initialFilterText = '' } = options;
|
|
387
|
+
let [data, dispatch] = $huzL3$react.useReducer($c690ccebce22d69f$var$reducer, {
|
|
388
|
+
state: 'idle',
|
|
389
|
+
error: null,
|
|
390
|
+
items: [],
|
|
391
|
+
selectedKeys: initialSelectedKeys === 'all' ? 'all' : new Set(initialSelectedKeys),
|
|
392
|
+
sortDescriptor: initialSortDescriptor,
|
|
393
|
+
filterText: initialFilterText
|
|
394
|
+
});
|
|
395
|
+
const dispatchFetch = async (action, fn)=>{
|
|
396
|
+
let abortController = new AbortController();
|
|
397
|
+
try {
|
|
398
|
+
dispatch({
|
|
399
|
+
...action,
|
|
400
|
+
abortController: abortController
|
|
401
|
+
});
|
|
402
|
+
let previousFilterText = action.filterText ?? data.filterText;
|
|
403
|
+
let response = await fn({
|
|
404
|
+
items: data.items.slice(),
|
|
405
|
+
selectedKeys: data.selectedKeys,
|
|
406
|
+
sortDescriptor: action.sortDescriptor ?? data.sortDescriptor,
|
|
407
|
+
signal: abortController.signal,
|
|
408
|
+
cursor: action.type === 'loadingMore' ? data.cursor : null,
|
|
409
|
+
filterText: previousFilterText
|
|
410
|
+
});
|
|
411
|
+
let filterText = response.filterText ?? previousFilterText;
|
|
412
|
+
dispatch({
|
|
413
|
+
type: 'success',
|
|
414
|
+
...response,
|
|
415
|
+
abortController: abortController
|
|
416
|
+
});
|
|
417
|
+
// Fetch a new filtered list if filterText is updated via `load` response func rather than list.setFilterText
|
|
418
|
+
// Only do this if not aborted (e.g. user triggers another filter action before load completes)
|
|
419
|
+
if (filterText && filterText !== previousFilterText && !abortController.signal.aborted) dispatchFetch({
|
|
420
|
+
type: 'filtering',
|
|
421
|
+
filterText: filterText
|
|
422
|
+
}, load);
|
|
423
|
+
} catch (e) {
|
|
424
|
+
dispatch({
|
|
425
|
+
type: 'error',
|
|
426
|
+
error: e,
|
|
427
|
+
abortController: abortController
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
$huzL3$react.useEffect(()=>{
|
|
491
432
|
dispatchFetch({
|
|
492
|
-
|
|
493
|
-
filterText
|
|
433
|
+
type: 'loading'
|
|
494
434
|
}, load);
|
|
495
|
-
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
}, sort || load);
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
}, $f237d9b45bc5fe911ce76292c069bdd7$export$createListActions(_babelRuntimeHelpersExtends({}, options, {
|
|
548
|
-
getKey,
|
|
549
|
-
cursor: data.cursor
|
|
550
|
-
}), fn => {
|
|
551
|
-
dispatch({
|
|
552
|
-
type: 'update',
|
|
553
|
-
updater: fn
|
|
554
|
-
});
|
|
555
|
-
}), {
|
|
556
|
-
setFilterText(filterText) {
|
|
557
|
-
dispatchFetch({
|
|
558
|
-
type: 'filtering',
|
|
559
|
-
filterText
|
|
560
|
-
}, load);
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
});
|
|
435
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
436
|
+
}, []);
|
|
437
|
+
return {
|
|
438
|
+
items: data.items,
|
|
439
|
+
selectedKeys: data.selectedKeys,
|
|
440
|
+
sortDescriptor: data.sortDescriptor,
|
|
441
|
+
isLoading: data.state === 'loading' || data.state === 'loadingMore' || data.state === 'sorting' || data.state === 'filtering',
|
|
442
|
+
loadingState: data.state,
|
|
443
|
+
error: data.error,
|
|
444
|
+
filterText: data.filterText,
|
|
445
|
+
getItem (key) {
|
|
446
|
+
return data.items.find((item)=>getKey(item) === key
|
|
447
|
+
);
|
|
448
|
+
},
|
|
449
|
+
reload () {
|
|
450
|
+
dispatchFetch({
|
|
451
|
+
type: 'loading'
|
|
452
|
+
}, load);
|
|
453
|
+
},
|
|
454
|
+
loadMore () {
|
|
455
|
+
// Ignore if already loading more or if performing server side filtering.
|
|
456
|
+
if (data.state === 'loadingMore' || data.state === 'filtering' || data.cursor == null) return;
|
|
457
|
+
dispatchFetch({
|
|
458
|
+
type: 'loadingMore'
|
|
459
|
+
}, load);
|
|
460
|
+
},
|
|
461
|
+
sort (sortDescriptor) {
|
|
462
|
+
dispatchFetch({
|
|
463
|
+
type: 'sorting',
|
|
464
|
+
sortDescriptor: sortDescriptor
|
|
465
|
+
}, sort || load);
|
|
466
|
+
},
|
|
467
|
+
...$7eaa5a0dc0e5bafc$export$79c0c687a5963b0a({
|
|
468
|
+
...options,
|
|
469
|
+
getKey: getKey,
|
|
470
|
+
cursor: data.cursor
|
|
471
|
+
}, (fn)=>{
|
|
472
|
+
dispatch({
|
|
473
|
+
type: 'update',
|
|
474
|
+
updater: fn
|
|
475
|
+
});
|
|
476
|
+
}),
|
|
477
|
+
setFilterText (filterText) {
|
|
478
|
+
dispatchFetch({
|
|
479
|
+
type: 'filtering',
|
|
480
|
+
filterText: filterText
|
|
481
|
+
}, load);
|
|
482
|
+
}
|
|
483
|
+
};
|
|
564
484
|
}
|
|
565
485
|
|
|
566
|
-
exports.useAsyncList = useAsyncList;
|
|
567
|
-
|
|
568
|
-
/**
|
|
569
|
-
* Manages state for an immutable tree data structure, and provides convenience methods to
|
|
570
|
-
* update the data over time.
|
|
571
|
-
*/
|
|
572
|
-
function useTreeData(options) {
|
|
573
|
-
let {
|
|
574
|
-
initialItems = [],
|
|
575
|
-
initialSelectedKeys,
|
|
576
|
-
getKey = item => item.id || item.key,
|
|
577
|
-
getChildren = item => item.children || []
|
|
578
|
-
} = options;
|
|
579
|
-
let map = useMemo(() => new Map(), []); // We only want to compute this on initial render.
|
|
580
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
581
|
-
|
|
582
|
-
let initialNodes = useMemo(() => buildTree(initialItems), []);
|
|
583
|
-
let [items, setItems] = useState(initialNodes);
|
|
584
|
-
let [selectedKeys, setSelectedKeys] = useState(new Set(initialSelectedKeys || []));
|
|
585
|
-
|
|
586
|
-
function buildTree(initialItems, parentKey) {
|
|
587
|
-
return initialItems.map(item => {
|
|
588
|
-
let node = {
|
|
589
|
-
key: getKey(item),
|
|
590
|
-
parentKey: parentKey,
|
|
591
|
-
value: item,
|
|
592
|
-
children: null
|
|
593
|
-
};
|
|
594
|
-
node.children = buildTree(getChildren(item), node.key);
|
|
595
|
-
map.set(node.key, node);
|
|
596
|
-
return node;
|
|
597
|
-
});
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
function updateTree(items, key, update) {
|
|
601
|
-
let node = map.get(key);
|
|
602
|
-
|
|
603
|
-
if (!node) {
|
|
604
|
-
return items;
|
|
605
|
-
} // Create a new node. If null, then delete the node, otherwise replace.
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
let newNode = update(node);
|
|
609
|
-
|
|
610
|
-
if (newNode == null) {
|
|
611
|
-
deleteNode(node);
|
|
612
|
-
} else {
|
|
613
|
-
addNode(newNode);
|
|
614
|
-
} // Walk up the tree and update each parent to refer to the new chilren.
|
|
615
|
-
|
|
616
486
|
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
};
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
487
|
+
var $3979341003e2f523$exports = {};
|
|
488
|
+
|
|
489
|
+
$parcel$export($3979341003e2f523$exports, "useTreeData", () => $3979341003e2f523$export$d14e1352e21f4a16);
|
|
490
|
+
|
|
491
|
+
function $3979341003e2f523$export$d14e1352e21f4a16(options) {
|
|
492
|
+
let { initialItems: initialItems1 = [] , initialSelectedKeys: initialSelectedKeys , getKey: getKey = (item)=>item.id || item.key
|
|
493
|
+
, getChildren: getChildren = (item)=>item.children || []
|
|
494
|
+
} = options;
|
|
495
|
+
let map = $huzL3$react.useMemo(()=>new Map()
|
|
496
|
+
, []);
|
|
497
|
+
// We only want to compute this on initial render.
|
|
498
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
499
|
+
let initialNodes = $huzL3$react.useMemo(()=>buildTree(initialItems1)
|
|
500
|
+
, []);
|
|
501
|
+
let [items1, setItems] = $huzL3$react.useState(initialNodes);
|
|
502
|
+
let [selectedKeys, setSelectedKeys] = $huzL3$react.useState(new Set(initialSelectedKeys || []));
|
|
503
|
+
function buildTree(initialItems, parentKey) {
|
|
504
|
+
return initialItems.map((item)=>{
|
|
505
|
+
let node = {
|
|
506
|
+
key: getKey(item),
|
|
507
|
+
parentKey: parentKey,
|
|
508
|
+
value: item,
|
|
509
|
+
children: null
|
|
510
|
+
};
|
|
511
|
+
node.children = buildTree(getChildren(item), node.key);
|
|
512
|
+
map.set(node.key, node);
|
|
513
|
+
return node;
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
function updateTree(items, key, update) {
|
|
517
|
+
let node = map.get(key);
|
|
518
|
+
if (!node) return items;
|
|
519
|
+
// Create a new node. If null, then delete the node, otherwise replace.
|
|
520
|
+
let newNode = update(node);
|
|
521
|
+
if (newNode == null) deleteNode(node);
|
|
522
|
+
else addNode(newNode);
|
|
523
|
+
// Walk up the tree and update each parent to refer to the new chilren.
|
|
524
|
+
while(node.parentKey){
|
|
525
|
+
let nextParent = map.get(node.parentKey);
|
|
526
|
+
let copy = {
|
|
527
|
+
key: nextParent.key,
|
|
528
|
+
parentKey: nextParent.parentKey,
|
|
529
|
+
value: nextParent.value,
|
|
530
|
+
children: null
|
|
531
|
+
};
|
|
532
|
+
let children = nextParent.children;
|
|
533
|
+
if (newNode == null) children = children.filter((c)=>c !== node
|
|
534
|
+
);
|
|
535
|
+
copy.children = children.map((child)=>{
|
|
536
|
+
if (child === node) return newNode;
|
|
537
|
+
return child;
|
|
538
|
+
});
|
|
539
|
+
map.set(copy.key, copy);
|
|
540
|
+
newNode = copy;
|
|
541
|
+
node = nextParent;
|
|
634
542
|
}
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
543
|
+
if (newNode == null) items = items.filter((c)=>c !== node
|
|
544
|
+
);
|
|
545
|
+
return items.map((item)=>{
|
|
546
|
+
if (item === node) return newNode;
|
|
547
|
+
return item;
|
|
548
|
+
});
|
|
641
549
|
}
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
550
|
+
function addNode(node) {
|
|
551
|
+
map.set(node.key, node);
|
|
552
|
+
for (let child of node.children)addNode(child);
|
|
645
553
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
return newNode;
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
return item;
|
|
653
|
-
});
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
function addNode(node) {
|
|
657
|
-
map.set(node.key, node);
|
|
658
|
-
|
|
659
|
-
for (let child of node.children) {
|
|
660
|
-
addNode(child);
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
function deleteNode(node) {
|
|
665
|
-
map.delete(node.key);
|
|
666
|
-
|
|
667
|
-
for (let child of node.children) {
|
|
668
|
-
deleteNode(child);
|
|
554
|
+
function deleteNode(node) {
|
|
555
|
+
map.delete(node.key);
|
|
556
|
+
for (let child of node.children)deleteNode(child);
|
|
669
557
|
}
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
558
|
+
return {
|
|
559
|
+
items: items1,
|
|
560
|
+
selectedKeys: selectedKeys,
|
|
561
|
+
setSelectedKeys: setSelectedKeys,
|
|
562
|
+
getItem (key) {
|
|
563
|
+
return map.get(key);
|
|
564
|
+
},
|
|
565
|
+
insert (parentKey, index, ...values) {
|
|
566
|
+
setItems((items)=>{
|
|
567
|
+
let nodes = buildTree(values, parentKey);
|
|
568
|
+
// If parentKey is null, insert into the root.
|
|
569
|
+
if (parentKey == null) return [
|
|
570
|
+
...items.slice(0, index),
|
|
571
|
+
...nodes,
|
|
572
|
+
...items.slice(index)
|
|
573
|
+
];
|
|
574
|
+
// Otherwise, update the parent node and its ancestors.
|
|
575
|
+
return updateTree(items, parentKey, (parentNode)=>({
|
|
576
|
+
key: parentNode.key,
|
|
577
|
+
parentKey: parentNode.parentKey,
|
|
578
|
+
value: parentNode.value,
|
|
579
|
+
children: [
|
|
580
|
+
...parentNode.children.slice(0, index),
|
|
581
|
+
...nodes,
|
|
582
|
+
...parentNode.children.slice(index)
|
|
583
|
+
]
|
|
584
|
+
})
|
|
585
|
+
);
|
|
586
|
+
});
|
|
587
|
+
},
|
|
588
|
+
insertBefore (key, ...values) {
|
|
589
|
+
let node = map.get(key);
|
|
590
|
+
if (!node) return;
|
|
591
|
+
let parentNode = map.get(node.parentKey);
|
|
592
|
+
let nodes = parentNode ? parentNode.children : items1;
|
|
593
|
+
let index = nodes.indexOf(node);
|
|
594
|
+
this.insert(parentNode?.key, index, ...values);
|
|
595
|
+
},
|
|
596
|
+
insertAfter (key, ...values) {
|
|
597
|
+
let node = map.get(key);
|
|
598
|
+
if (!node) return;
|
|
599
|
+
let parentNode = map.get(node.parentKey);
|
|
600
|
+
let nodes = parentNode ? parentNode.children : items1;
|
|
601
|
+
let index = nodes.indexOf(node);
|
|
602
|
+
this.insert(parentNode?.key, index + 1, ...values);
|
|
603
|
+
},
|
|
604
|
+
prepend (parentKey, ...values) {
|
|
605
|
+
this.insert(parentKey, 0, ...values);
|
|
606
|
+
},
|
|
607
|
+
append (parentKey, ...values) {
|
|
608
|
+
if (parentKey == null) this.insert(null, items1.length, ...values);
|
|
609
|
+
else {
|
|
610
|
+
let parentNode = map.get(parentKey);
|
|
611
|
+
if (!parentNode) return;
|
|
612
|
+
this.insert(parentKey, parentNode.children.length, ...values);
|
|
613
|
+
}
|
|
614
|
+
},
|
|
615
|
+
remove (...keys) {
|
|
616
|
+
let newItems = items1;
|
|
617
|
+
for (let key of keys)newItems = updateTree(newItems, key, ()=>null
|
|
618
|
+
);
|
|
619
|
+
setItems(newItems);
|
|
620
|
+
let selection = new Set(selectedKeys);
|
|
621
|
+
for (let key1 of selectedKeys)if (!map.has(key1)) selection.delete(key1);
|
|
622
|
+
setSelectedKeys(selection);
|
|
623
|
+
},
|
|
624
|
+
removeSelectedItems () {
|
|
625
|
+
this.remove(...selectedKeys);
|
|
626
|
+
},
|
|
627
|
+
move (key, toParentKey, index) {
|
|
628
|
+
setItems((items)=>{
|
|
629
|
+
let node = map.get(key);
|
|
630
|
+
if (!node) return items;
|
|
631
|
+
items = updateTree(items, key, ()=>null
|
|
632
|
+
);
|
|
633
|
+
const movedNode = {
|
|
634
|
+
...node,
|
|
635
|
+
parentKey: toParentKey
|
|
636
|
+
};
|
|
637
|
+
return updateTree(items, toParentKey, (parentNode)=>({
|
|
638
|
+
key: parentNode.key,
|
|
639
|
+
parentKey: parentNode.parentKey,
|
|
640
|
+
value: parentNode.value,
|
|
641
|
+
children: [
|
|
642
|
+
...parentNode.children.slice(0, index),
|
|
643
|
+
movedNode,
|
|
644
|
+
...parentNode.children.slice(index)
|
|
645
|
+
]
|
|
646
|
+
})
|
|
647
|
+
);
|
|
648
|
+
});
|
|
649
|
+
},
|
|
650
|
+
update (oldKey, newValue) {
|
|
651
|
+
setItems((items)=>updateTree(items, oldKey, (oldNode)=>{
|
|
652
|
+
let node = {
|
|
653
|
+
key: oldNode.key,
|
|
654
|
+
parentKey: oldNode.parentKey,
|
|
655
|
+
value: newValue,
|
|
656
|
+
children: null
|
|
657
|
+
};
|
|
658
|
+
node.children = buildTree(getChildren(newValue), node.key);
|
|
659
|
+
return node;
|
|
660
|
+
})
|
|
661
|
+
);
|
|
759
662
|
}
|
|
663
|
+
};
|
|
664
|
+
}
|
|
760
665
|
|
|
761
|
-
this.insert(parentKey, parentNode.children.length, ...values);
|
|
762
|
-
}
|
|
763
|
-
},
|
|
764
|
-
|
|
765
|
-
remove() {
|
|
766
|
-
let newItems = items;
|
|
767
|
-
|
|
768
|
-
for (var _len6 = arguments.length, keys = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
|
|
769
|
-
keys[_key6] = arguments[_key6];
|
|
770
|
-
}
|
|
771
|
-
|
|
772
|
-
for (let key of keys) {
|
|
773
|
-
newItems = updateTree(newItems, key, () => null);
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
setItems(newItems);
|
|
777
|
-
let selection = new Set(selectedKeys);
|
|
778
|
-
|
|
779
|
-
for (let key of selectedKeys) {
|
|
780
|
-
if (!map.has(key)) {
|
|
781
|
-
selection.delete(key);
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
setSelectedKeys(selection);
|
|
786
|
-
},
|
|
787
|
-
|
|
788
|
-
removeSelectedItems() {
|
|
789
|
-
this.remove(...selectedKeys);
|
|
790
|
-
},
|
|
791
|
-
|
|
792
|
-
move(key, toParentKey, index) {
|
|
793
|
-
setItems(items => {
|
|
794
|
-
let node = map.get(key);
|
|
795
|
-
|
|
796
|
-
if (!node) {
|
|
797
|
-
return items;
|
|
798
|
-
}
|
|
799
|
-
|
|
800
|
-
items = updateTree(items, key, () => null);
|
|
801
|
-
|
|
802
|
-
const movedNode = _babelRuntimeHelpersExtends({}, node, {
|
|
803
|
-
parentKey: toParentKey
|
|
804
|
-
});
|
|
805
666
|
|
|
806
|
-
return updateTree(items, toParentKey, parentNode => ({
|
|
807
|
-
key: parentNode.key,
|
|
808
|
-
parentKey: parentNode.parentKey,
|
|
809
|
-
value: parentNode.value,
|
|
810
|
-
children: [...parentNode.children.slice(0, index), movedNode, ...parentNode.children.slice(index)]
|
|
811
|
-
}));
|
|
812
|
-
});
|
|
813
|
-
},
|
|
814
667
|
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
let node = {
|
|
818
|
-
key: oldNode.key,
|
|
819
|
-
parentKey: oldNode.parentKey,
|
|
820
|
-
value: newValue,
|
|
821
|
-
children: null
|
|
822
|
-
};
|
|
823
|
-
node.children = buildTree(getChildren(newValue), node.key);
|
|
824
|
-
return node;
|
|
825
|
-
}));
|
|
826
|
-
}
|
|
668
|
+
$parcel$exportWildcard(module.exports, $c690ccebce22d69f$exports);
|
|
669
|
+
$parcel$exportWildcard(module.exports, $3979341003e2f523$exports);
|
|
827
670
|
|
|
828
|
-
};
|
|
829
|
-
}
|
|
830
671
|
|
|
831
|
-
exports.useTreeData = useTreeData;
|
|
832
672
|
//# sourceMappingURL=main.js.map
|