@react-fabric/data 1.0.0-beta.0
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 +10 -0
- package/dist/cjs/histogram/Histogram.js +89 -0
- package/dist/cjs/histogram/Histogram.module.css +1 -0
- package/dist/cjs/hooks/useFilteredList.js +56 -0
- package/dist/cjs/hooks/usePagination.js +85 -0
- package/dist/cjs/i18n/ar.json.js +56 -0
- package/dist/cjs/i18n/en.json.js +56 -0
- package/dist/cjs/i18n/index.js +33 -0
- package/dist/cjs/index.js +24 -0
- package/dist/cjs/json/JsonViewer.js +246 -0
- package/dist/cjs/pagination/Pagination.js +100 -0
- package/dist/cjs/table/BodyCell.js +47 -0
- package/dist/cjs/table/HeaderCell.js +79 -0
- package/dist/cjs/table/Table.js +133 -0
- package/dist/cjs/table/useTableColumns.js +26 -0
- package/dist/cjs/table/useVirtualTable.js +56 -0
- package/dist/cjs/tree/TreeNode.js +107 -0
- package/dist/cjs/tree/TreePanel.js +136 -0
- package/dist/cjs/tree/types.js +30 -0
- package/dist/cjs/tree/useTree.js +181 -0
- package/dist/cjs/tree/utils.js +145 -0
- package/dist/cjs/virtual/Gallery.js +229 -0
- package/dist/cjs/virtual/List.js +218 -0
- package/dist/cjs/virtual/Scroller.js +77 -0
- package/dist/esm/histogram/Histogram.js +89 -0
- package/dist/esm/histogram/Histogram.module.css +1 -0
- package/dist/esm/hooks/useFilteredList.js +56 -0
- package/dist/esm/hooks/usePagination.js +85 -0
- package/dist/esm/i18n/ar.json.js +47 -0
- package/dist/esm/i18n/en.json.js +47 -0
- package/dist/esm/i18n/index.js +33 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/json/JsonViewer.js +246 -0
- package/dist/esm/pagination/Pagination.js +100 -0
- package/dist/esm/table/BodyCell.js +47 -0
- package/dist/esm/table/HeaderCell.js +79 -0
- package/dist/esm/table/Table.js +133 -0
- package/dist/esm/table/useTableColumns.js +26 -0
- package/dist/esm/table/useVirtualTable.js +56 -0
- package/dist/esm/tree/TreeNode.js +107 -0
- package/dist/esm/tree/TreePanel.js +136 -0
- package/dist/esm/tree/types.js +29 -0
- package/dist/esm/tree/useTree.js +181 -0
- package/dist/esm/tree/utils.js +140 -0
- package/dist/esm/virtual/Gallery.js +229 -0
- package/dist/esm/virtual/List.js +218 -0
- package/dist/esm/virtual/Scroller.js +77 -0
- package/dist/types/histogram/Histogram.d.ts +41 -0
- package/dist/types/hooks/useFilteredList.d.ts +7 -0
- package/dist/types/hooks/usePagination.d.ts +25 -0
- package/dist/types/i18n/index.d.ts +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/json/JsonViewer.d.ts +59 -0
- package/dist/types/pagination/Pagination.d.ts +24 -0
- package/dist/types/table/BodyCell.d.ts +5 -0
- package/dist/types/table/HeaderCell.d.ts +2 -0
- package/dist/types/table/Table.d.ts +2 -0
- package/dist/types/table/types.d.ts +26 -0
- package/dist/types/table/useTableColumns.d.ts +8 -0
- package/dist/types/table/useVirtualTable.d.ts +8 -0
- package/dist/types/tree/TreeNode.d.ts +2 -0
- package/dist/types/tree/TreePanel.d.ts +2 -0
- package/dist/types/tree/types.d.ts +138 -0
- package/dist/types/tree/useTree.d.ts +20 -0
- package/dist/types/tree/utils.d.ts +10 -0
- package/dist/types/virtual/Gallery.d.ts +55 -0
- package/dist/types/virtual/List.d.ts +59 -0
- package/dist/types/virtual/Scroller.d.ts +8 -0
- package/package.json +55 -0
- package/src/histogram/Histogram.module.css +57 -0
- package/src/histogram/Histogram.tsx +176 -0
- package/src/hooks/useFilteredList.tsx +78 -0
- package/src/hooks/usePagination.tsx +135 -0
- package/src/i18n/ar.json +36 -0
- package/src/i18n/en.json +36 -0
- package/src/i18n/index.ts +28 -0
- package/src/index.ts +35 -0
- package/src/json/JsonViewer.tsx +381 -0
- package/src/pagination/Pagination.tsx +157 -0
- package/src/table/BodyCell.tsx +54 -0
- package/src/table/HeaderCell.tsx +70 -0
- package/src/table/Table.tsx +143 -0
- package/src/table/types.ts +65 -0
- package/src/table/useTableColumns.ts +52 -0
- package/src/table/useVirtualTable.ts +54 -0
- package/src/tree/TreeNode.tsx +163 -0
- package/src/tree/TreePanel.tsx +157 -0
- package/src/tree/types.ts +181 -0
- package/src/tree/useTree.ts +227 -0
- package/src/tree/utils.ts +185 -0
- package/src/virtual/Gallery.tsx +349 -0
- package/src/virtual/List.tsx +351 -0
- package/src/virtual/Scroller.tsx +81 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
|
|
4
|
+
var core = require('@react-fabric/core');
|
|
5
|
+
var utilities = require('@react-fabric/utilities');
|
|
6
|
+
var reactVirtual = require('@tanstack/react-virtual');
|
|
7
|
+
var classNames = require('classnames');
|
|
8
|
+
var memoizeOne = require('memoize-one');
|
|
9
|
+
var react = require('react');
|
|
10
|
+
var Scroller = require('./Scroller.js');
|
|
11
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
12
|
+
|
|
13
|
+
var createItemList = memoizeOne(function (items) {
|
|
14
|
+
return items;
|
|
15
|
+
});
|
|
16
|
+
var _VirtualGallery = function _VirtualGallery(_ref) {
|
|
17
|
+
var _virtualItems$3;
|
|
18
|
+
var items = _ref.items,
|
|
19
|
+
children = _ref.children,
|
|
20
|
+
columns = _ref.columns,
|
|
21
|
+
_ref$padding = _ref.padding,
|
|
22
|
+
padding = _ref$padding === void 0 ? "md" : _ref$padding,
|
|
23
|
+
_ref$defaultHeight = _ref.defaultHeight,
|
|
24
|
+
height = _ref$defaultHeight === void 0 ? 48 : _ref$defaultHeight,
|
|
25
|
+
_ref$defaultWidth = _ref.defaultWidth,
|
|
26
|
+
width = _ref$defaultWidth === void 0 ? 48 : _ref$defaultWidth,
|
|
27
|
+
hideScroller = _ref.hideScroller,
|
|
28
|
+
initialScroll = _ref.initialScroll,
|
|
29
|
+
loading = _ref.loading,
|
|
30
|
+
onLoadMore = _ref.onLoadMore,
|
|
31
|
+
onScroll = _ref.onScroll,
|
|
32
|
+
galleryRef = _ref.galleryRef;
|
|
33
|
+
var count = react.useDeferredValue(items.length);
|
|
34
|
+
var extraSize = react.useMemo(function () {
|
|
35
|
+
return onLoadMore ? 64 : 0;
|
|
36
|
+
}, [onLoadMore]);
|
|
37
|
+
var itemList = createItemList(items.map(function (item, idx) {
|
|
38
|
+
return {
|
|
39
|
+
index: idx,
|
|
40
|
+
data: item,
|
|
41
|
+
isLast: idx + 1 === items.length
|
|
42
|
+
};
|
|
43
|
+
}));
|
|
44
|
+
var scrollerRef = react.useRef(null);
|
|
45
|
+
var _useState = react.useState(1),
|
|
46
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
47
|
+
columnCount = _useState2[0],
|
|
48
|
+
setColumnCount = _useState2[1];
|
|
49
|
+
var columnMap = react.useMemo(function () {
|
|
50
|
+
return new Array(columnCount).fill(0).map(function (_, idx) {
|
|
51
|
+
return idx;
|
|
52
|
+
});
|
|
53
|
+
}, [columnCount]);
|
|
54
|
+
core.useEffectDebugger(function () {
|
|
55
|
+
if (scrollerRef.current) {
|
|
56
|
+
var el = scrollerRef.current;
|
|
57
|
+
var handler = function handler() {
|
|
58
|
+
var _el$offsetWidth;
|
|
59
|
+
setColumnCount(columns !== null && columns !== void 0 ? columns : Math.max(1, Math.floor((((_el$offsetWidth = el === null || el === void 0 ? void 0 : el.offsetWidth) !== null && _el$offsetWidth !== void 0 ? _el$offsetWidth : 800) - 32) / width)));
|
|
60
|
+
};
|
|
61
|
+
var ob = new ResizeObserver(handler);
|
|
62
|
+
handler();
|
|
63
|
+
ob.observe(el);
|
|
64
|
+
return function () {
|
|
65
|
+
ob.unobserve(el);
|
|
66
|
+
ob.disconnect();
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}, [scrollerRef.current, columns, width], "virtualGallery recalculate view count");
|
|
70
|
+
var virtualizer = reactVirtual.useVirtualizer({
|
|
71
|
+
count: Math.ceil(count / columnCount),
|
|
72
|
+
getScrollElement: function getScrollElement() {
|
|
73
|
+
return scrollerRef.current;
|
|
74
|
+
},
|
|
75
|
+
estimateSize: function estimateSize() {
|
|
76
|
+
return height;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
var virtualItems = virtualizer.getVirtualItems();
|
|
80
|
+
var currentIndex = react.useRef(0);
|
|
81
|
+
react.useEffect(function () {
|
|
82
|
+
var _virtualItems$1$index, _virtualItems$;
|
|
83
|
+
currentIndex.current = (_virtualItems$1$index = (_virtualItems$ = virtualItems[1]) === null || _virtualItems$ === void 0 ? void 0 : _virtualItems$.index) !== null && _virtualItems$1$index !== void 0 ? _virtualItems$1$index : 0;
|
|
84
|
+
if (scrollerRef.current && virtualItems[0] && scrollerRef.current.scrollTop < virtualItems[0].size) currentIndex.current = 0;
|
|
85
|
+
}, [virtualItems]);
|
|
86
|
+
var calculateActiveScroll = react.useCallback(function () {
|
|
87
|
+
var _scrollerRef$current;
|
|
88
|
+
var _ref2 = (_scrollerRef$current = scrollerRef.current) !== null && _scrollerRef$current !== void 0 ? _scrollerRef$current : {},
|
|
89
|
+
_ref2$scrollTop = _ref2.scrollTop,
|
|
90
|
+
scrollTop = _ref2$scrollTop === void 0 ? 0 : _ref2$scrollTop,
|
|
91
|
+
_ref2$scrollHeight = _ref2.scrollHeight,
|
|
92
|
+
scrollHeight = _ref2$scrollHeight === void 0 ? 0 : _ref2$scrollHeight,
|
|
93
|
+
_ref2$offsetHeight = _ref2.offsetHeight,
|
|
94
|
+
offsetHeight = _ref2$offsetHeight === void 0 ? 0 : _ref2$offsetHeight;
|
|
95
|
+
if (scrollHeight === offsetHeight) return 0;
|
|
96
|
+
if (scrollTop === 0) return 1;
|
|
97
|
+
if (scrollTop >= scrollHeight - offsetHeight - extraSize * 2) return 2;
|
|
98
|
+
return 3;
|
|
99
|
+
}, []);
|
|
100
|
+
var _useState3 = react.useState(0),
|
|
101
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
102
|
+
scrollActive = _useState4[0],
|
|
103
|
+
setScrollActive = _useState4[1];
|
|
104
|
+
var fireScrollEvent = core.useDebounce(function (idx) {
|
|
105
|
+
return onScroll === null || onScroll === void 0 ? void 0 : onScroll(currentIndex.current * idx);
|
|
106
|
+
}, [onScroll]);
|
|
107
|
+
var handleScroll = react.useCallback(function () {
|
|
108
|
+
setScrollActive(calculateActiveScroll());
|
|
109
|
+
fireScrollEvent(columnCount);
|
|
110
|
+
}, [columnCount, onLoadMore]);
|
|
111
|
+
react.useEffect(function () {
|
|
112
|
+
setScrollActive(calculateActiveScroll());
|
|
113
|
+
}, [count, calculateActiveScroll]);
|
|
114
|
+
react.useEffect(function () {
|
|
115
|
+
var db = utilities.debounce(function () {
|
|
116
|
+
var lastItem = virtualItems === null || virtualItems === void 0 ? void 0 : virtualItems.slice(-1).pop();
|
|
117
|
+
if (lastItem && !loading && lastItem.index * columnCount + columnCount >= count - 1) onLoadMore === null || onLoadMore === void 0 || onLoadMore();
|
|
118
|
+
});
|
|
119
|
+
db();
|
|
120
|
+
return function () {
|
|
121
|
+
return db.cancel();
|
|
122
|
+
};
|
|
123
|
+
}, [onLoadMore, loading, itemList, virtualItems, count, columnCount]);
|
|
124
|
+
react.useEffect(function () {
|
|
125
|
+
var _virtualItems$2;
|
|
126
|
+
var firstItem = (_virtualItems$2 = virtualItems[0]) === null || _virtualItems$2 === void 0 ? void 0 : _virtualItems$2.index;
|
|
127
|
+
if (onLoadMore && firstItem !== undefined && items[firstItem * columnCount] === null) {
|
|
128
|
+
var db = utilities.debounce(onLoadMore);
|
|
129
|
+
db(firstItem * columnCount);
|
|
130
|
+
return function () {
|
|
131
|
+
return db.cancel();
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
}, [onLoadMore, items, columnCount, virtualItems, count]);
|
|
135
|
+
react.useLayoutEffect(function () {
|
|
136
|
+
setTimeout(function () {
|
|
137
|
+
initialScroll && virtualizer.scrollToIndex(Math.floor(initialScroll / columnCount), {
|
|
138
|
+
align: "start"
|
|
139
|
+
});
|
|
140
|
+
}, 100);
|
|
141
|
+
}, [columnCount]);
|
|
142
|
+
react.useImperativeHandle(galleryRef, function () {
|
|
143
|
+
return {
|
|
144
|
+
scrollToItem: function scrollToItem(index) {
|
|
145
|
+
virtualizer.scrollToIndex(Math.floor(index / columnCount), {
|
|
146
|
+
align: "start"
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
hilight: function hilight(index) {
|
|
150
|
+
var _scrollerRef$current2;
|
|
151
|
+
(_scrollerRef$current2 = scrollerRef.current) === null || _scrollerRef$current2 === void 0 || (_scrollerRef$current2 = _scrollerRef$current2.querySelector(".hilight")) === null || _scrollerRef$current2 === void 0 || _scrollerRef$current2.classList.remove("hilight", "ring-1");
|
|
152
|
+
virtualizer.scrollToIndex(Math.floor(index / columnCount), {
|
|
153
|
+
align: "start"
|
|
154
|
+
});
|
|
155
|
+
setTimeout(function () {
|
|
156
|
+
var _scrollerRef$current3;
|
|
157
|
+
(_scrollerRef$current3 = scrollerRef.current) === null || _scrollerRef$current3 === void 0 || (_scrollerRef$current3 = _scrollerRef$current3.querySelector("[data-index=\"".concat(index, "\"]"))) === null || _scrollerRef$current3 === void 0 || _scrollerRef$current3.classList.add("hilight", "ring-1");
|
|
158
|
+
}, 100);
|
|
159
|
+
},
|
|
160
|
+
unhilight: function unhilight() {
|
|
161
|
+
var _scrollerRef$current4;
|
|
162
|
+
(_scrollerRef$current4 = scrollerRef.current) === null || _scrollerRef$current4 === void 0 || (_scrollerRef$current4 = _scrollerRef$current4.querySelector(".hilight")) === null || _scrollerRef$current4 === void 0 || _scrollerRef$current4.classList.remove("hilight", "ring-1");
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}, [virtualizer, columnCount]);
|
|
166
|
+
return /*#__PURE__*/jsxRuntime.jsx("div", {
|
|
167
|
+
ref: scrollerRef,
|
|
168
|
+
onScroll: handleScroll,
|
|
169
|
+
className: classNames("area-content bg-base overflow-auto contain-strict"),
|
|
170
|
+
children: /*#__PURE__*/jsxRuntime.jsxs("div", {
|
|
171
|
+
className: classNames("inline-flex flex-nowrap gap-2 justify-center min-h-full min-w-full"),
|
|
172
|
+
children: [/*#__PURE__*/jsxRuntime.jsx("div", {
|
|
173
|
+
className: classNames("box-content", padding === "sm" && "p-1", padding === "md" && "p-2", padding === "lg" && "p-4"),
|
|
174
|
+
children: /*#__PURE__*/jsxRuntime.jsxs("div", {
|
|
175
|
+
style: {
|
|
176
|
+
height: virtualizer.getTotalSize()
|
|
177
|
+
},
|
|
178
|
+
children: [/*#__PURE__*/jsxRuntime.jsx("div", {
|
|
179
|
+
style: {
|
|
180
|
+
height: (_virtualItems$3 = virtualItems[0]) === null || _virtualItems$3 === void 0 ? void 0 : _virtualItems$3.start
|
|
181
|
+
}
|
|
182
|
+
}), virtualItems.map(function (_ref3) {
|
|
183
|
+
var key = _ref3.key,
|
|
184
|
+
index = _ref3.index,
|
|
185
|
+
measureElement = _ref3.measureElement;
|
|
186
|
+
return /*#__PURE__*/jsxRuntime.jsx("div", {
|
|
187
|
+
className: "flex",
|
|
188
|
+
"data-index": index,
|
|
189
|
+
ref: measureElement,
|
|
190
|
+
children: columnMap.map(function (col) {
|
|
191
|
+
return /*#__PURE__*/jsxRuntime.jsx("div", {
|
|
192
|
+
"data-index": index * columnCount + col,
|
|
193
|
+
className: classNames("grid rounded-capped"),
|
|
194
|
+
children: itemList[index * columnCount + col] && children(itemList[index * columnCount + col])
|
|
195
|
+
}, "".concat(key, ":").concat(col));
|
|
196
|
+
})
|
|
197
|
+
}, key);
|
|
198
|
+
}), onLoadMore && /*#__PURE__*/jsxRuntime.jsx("div", {
|
|
199
|
+
className: "h-16",
|
|
200
|
+
children: loading && /*#__PURE__*/jsxRuntime.jsx(core.Skeleton, {})
|
|
201
|
+
})]
|
|
202
|
+
})
|
|
203
|
+
}), !hideScroller && /*#__PURE__*/jsxRuntime.jsx("div", {
|
|
204
|
+
className: classNames("self-end sticky bottom-2"),
|
|
205
|
+
children: /*#__PURE__*/jsxRuntime.jsx(Scroller.Scroller, {
|
|
206
|
+
vertical: true,
|
|
207
|
+
scrollActive: scrollActive,
|
|
208
|
+
onStart: function onStart() {
|
|
209
|
+
return virtualizer.scrollToIndex(0);
|
|
210
|
+
},
|
|
211
|
+
onPrevious: function onPrevious() {
|
|
212
|
+
return scrollerRef.current && virtualizer.scrollBy(-scrollerRef.current.offsetHeight);
|
|
213
|
+
},
|
|
214
|
+
onNext: function onNext() {
|
|
215
|
+
return scrollerRef.current && virtualizer.scrollBy(scrollerRef.current.offsetHeight);
|
|
216
|
+
},
|
|
217
|
+
onEnd: function onEnd() {
|
|
218
|
+
return virtualizer.scrollToIndex(count - 1);
|
|
219
|
+
}
|
|
220
|
+
})
|
|
221
|
+
})]
|
|
222
|
+
})
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
_VirtualGallery.displayName = "VirtualGallery";
|
|
226
|
+
var GenericMemo = react.memo;
|
|
227
|
+
var VirtualGallery = GenericMemo(_VirtualGallery);
|
|
228
|
+
|
|
229
|
+
exports.VirtualGallery = VirtualGallery;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
|
|
4
|
+
var core = require('@react-fabric/core');
|
|
5
|
+
var utilities = require('@react-fabric/utilities');
|
|
6
|
+
var reactVirtual = require('@tanstack/react-virtual');
|
|
7
|
+
var classNames = require('classnames');
|
|
8
|
+
var memoizeOne = require('memoize-one');
|
|
9
|
+
var react = require('react');
|
|
10
|
+
var Scroller = require('./Scroller.js');
|
|
11
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
12
|
+
|
|
13
|
+
var createItemList = memoizeOne(function (items) {
|
|
14
|
+
return items;
|
|
15
|
+
});
|
|
16
|
+
var _VirtualList = function _VirtualList(_ref) {
|
|
17
|
+
var _virtualItems$3, _virtualItems$4;
|
|
18
|
+
var items = _ref.items,
|
|
19
|
+
children = _ref.children,
|
|
20
|
+
fullWidth = _ref.fullWidth,
|
|
21
|
+
_ref$padding = _ref.padding,
|
|
22
|
+
padding = _ref$padding === void 0 ? "md" : _ref$padding,
|
|
23
|
+
_ref$orientation = _ref.orientation,
|
|
24
|
+
orientation = _ref$orientation === void 0 ? "vertical" : _ref$orientation,
|
|
25
|
+
_ref$defaultHeight = _ref.defaultHeight,
|
|
26
|
+
height = _ref$defaultHeight === void 0 ? 48 : _ref$defaultHeight,
|
|
27
|
+
_ref$defaultWidth = _ref.defaultWidth,
|
|
28
|
+
width = _ref$defaultWidth === void 0 ? 48 : _ref$defaultWidth,
|
|
29
|
+
hideScroller = _ref.hideScroller,
|
|
30
|
+
initialScroll = _ref.initialScroll,
|
|
31
|
+
loading = _ref.loading,
|
|
32
|
+
onLoadMore = _ref.onLoadMore,
|
|
33
|
+
onScroll = _ref.onScroll,
|
|
34
|
+
listRef = _ref.listRef;
|
|
35
|
+
var count = react.useDeferredValue(items.length);
|
|
36
|
+
var extraSize = react.useMemo(function () {
|
|
37
|
+
return onLoadMore ? 64 : 0;
|
|
38
|
+
}, [onLoadMore]);
|
|
39
|
+
var itemList = createItemList(items.map(function (item, idx) {
|
|
40
|
+
return {
|
|
41
|
+
index: idx,
|
|
42
|
+
data: item,
|
|
43
|
+
isLast: idx + 1 === items.length
|
|
44
|
+
};
|
|
45
|
+
}));
|
|
46
|
+
var scrollerRef = react.useRef(null);
|
|
47
|
+
var virtualizer = reactVirtual.useVirtualizer({
|
|
48
|
+
count: count,
|
|
49
|
+
horizontal: orientation === "horizontal",
|
|
50
|
+
getScrollElement: function getScrollElement() {
|
|
51
|
+
return scrollerRef.current;
|
|
52
|
+
},
|
|
53
|
+
estimateSize: function estimateSize() {
|
|
54
|
+
return orientation === "horizontal" ? width : height;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
var virtualItems = virtualizer.getVirtualItems();
|
|
58
|
+
var currentIndex = react.useRef(0);
|
|
59
|
+
react.useEffect(function () {
|
|
60
|
+
var _virtualItems$1$index, _virtualItems$;
|
|
61
|
+
currentIndex.current = (_virtualItems$1$index = (_virtualItems$ = virtualItems[1]) === null || _virtualItems$ === void 0 ? void 0 : _virtualItems$.index) !== null && _virtualItems$1$index !== void 0 ? _virtualItems$1$index : 0;
|
|
62
|
+
if (scrollerRef.current && virtualItems[0] && scrollerRef.current.scrollTop < virtualItems[0].size) currentIndex.current = 0;
|
|
63
|
+
}, [virtualItems]);
|
|
64
|
+
var calculateActiveScroll = react.useCallback(function () {
|
|
65
|
+
var _scrollerRef$current;
|
|
66
|
+
var _ref2 = (_scrollerRef$current = scrollerRef.current) !== null && _scrollerRef$current !== void 0 ? _scrollerRef$current : {},
|
|
67
|
+
_ref2$scrollTop = _ref2.scrollTop,
|
|
68
|
+
scrollTop = _ref2$scrollTop === void 0 ? 0 : _ref2$scrollTop,
|
|
69
|
+
_ref2$scrollLeft = _ref2.scrollLeft,
|
|
70
|
+
scrollLeft = _ref2$scrollLeft === void 0 ? 0 : _ref2$scrollLeft,
|
|
71
|
+
_ref2$scrollWidth = _ref2.scrollWidth,
|
|
72
|
+
scrollWidth = _ref2$scrollWidth === void 0 ? 0 : _ref2$scrollWidth,
|
|
73
|
+
_ref2$scrollHeight = _ref2.scrollHeight,
|
|
74
|
+
scrollHeight = _ref2$scrollHeight === void 0 ? 0 : _ref2$scrollHeight,
|
|
75
|
+
_ref2$offsetWidth = _ref2.offsetWidth,
|
|
76
|
+
offsetWidth = _ref2$offsetWidth === void 0 ? 0 : _ref2$offsetWidth,
|
|
77
|
+
_ref2$offsetHeight = _ref2.offsetHeight,
|
|
78
|
+
offsetHeight = _ref2$offsetHeight === void 0 ? 0 : _ref2$offsetHeight;
|
|
79
|
+
if (orientation === "vertical") {
|
|
80
|
+
if (scrollHeight === offsetHeight) return 0;
|
|
81
|
+
if (scrollTop === 0) return 1;
|
|
82
|
+
if (scrollTop >= scrollHeight - offsetHeight - extraSize * 2) return 2;
|
|
83
|
+
}
|
|
84
|
+
if (orientation === "horizontal") {
|
|
85
|
+
if (scrollWidth === offsetWidth) return 0;
|
|
86
|
+
if (Math.abs(scrollLeft) === 0) return 1;
|
|
87
|
+
if (Math.abs(scrollLeft) >= scrollWidth - offsetWidth - extraSize * 2) return 2;
|
|
88
|
+
}
|
|
89
|
+
return 3;
|
|
90
|
+
}, [orientation]);
|
|
91
|
+
var _useState = react.useState(0),
|
|
92
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
93
|
+
scrollActive = _useState2[0],
|
|
94
|
+
setScrollActive = _useState2[1];
|
|
95
|
+
var fireScrollEvent = core.useDebounce(function () {
|
|
96
|
+
return onScroll === null || onScroll === void 0 ? void 0 : onScroll(currentIndex.current);
|
|
97
|
+
}, [onScroll]);
|
|
98
|
+
var handleScroll = react.useCallback(function () {
|
|
99
|
+
setScrollActive(calculateActiveScroll());
|
|
100
|
+
fireScrollEvent();
|
|
101
|
+
}, [orientation, onLoadMore]);
|
|
102
|
+
react.useEffect(function () {
|
|
103
|
+
setScrollActive(calculateActiveScroll());
|
|
104
|
+
}, [itemList, calculateActiveScroll]);
|
|
105
|
+
react.useEffect(function () {
|
|
106
|
+
var db = utilities.debounce(function () {
|
|
107
|
+
var lastItem = virtualItems === null || virtualItems === void 0 ? void 0 : virtualItems.slice(-1).pop();
|
|
108
|
+
if (lastItem && !loading && lastItem.index === count - 1) onLoadMore === null || onLoadMore === void 0 || onLoadMore();
|
|
109
|
+
});
|
|
110
|
+
db();
|
|
111
|
+
return function () {
|
|
112
|
+
return db.cancel();
|
|
113
|
+
};
|
|
114
|
+
}, [onLoadMore, loading, itemList, virtualItems, count]);
|
|
115
|
+
react.useEffect(function () {
|
|
116
|
+
var _virtualItems$2;
|
|
117
|
+
var firstItem = (_virtualItems$2 = virtualItems[0]) === null || _virtualItems$2 === void 0 ? void 0 : _virtualItems$2.index;
|
|
118
|
+
if (onLoadMore && firstItem !== undefined && items[firstItem] === null) {
|
|
119
|
+
var db = utilities.debounce(onLoadMore);
|
|
120
|
+
db(firstItem);
|
|
121
|
+
return function () {
|
|
122
|
+
return db.cancel();
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
}, [onLoadMore, items, virtualItems, count]);
|
|
126
|
+
react.useLayoutEffect(function () {
|
|
127
|
+
setTimeout(function () {
|
|
128
|
+
initialScroll && virtualizer.scrollToIndex(initialScroll, {
|
|
129
|
+
align: "start"
|
|
130
|
+
});
|
|
131
|
+
}, 100);
|
|
132
|
+
}, []);
|
|
133
|
+
react.useImperativeHandle(listRef, function () {
|
|
134
|
+
return {
|
|
135
|
+
scrollToItem: function scrollToItem(index) {
|
|
136
|
+
virtualizer.scrollToIndex(index, {
|
|
137
|
+
align: "start"
|
|
138
|
+
});
|
|
139
|
+
},
|
|
140
|
+
hilight: function hilight(index) {
|
|
141
|
+
var _scrollerRef$current2;
|
|
142
|
+
(_scrollerRef$current2 = scrollerRef.current) === null || _scrollerRef$current2 === void 0 || (_scrollerRef$current2 = _scrollerRef$current2.querySelector(".hilight")) === null || _scrollerRef$current2 === void 0 || _scrollerRef$current2.classList.remove("hilight", "ring-1");
|
|
143
|
+
virtualizer.scrollToIndex(Math.floor(index), {
|
|
144
|
+
align: "start"
|
|
145
|
+
});
|
|
146
|
+
setTimeout(function () {
|
|
147
|
+
var _scrollerRef$current3;
|
|
148
|
+
(_scrollerRef$current3 = scrollerRef.current) === null || _scrollerRef$current3 === void 0 || (_scrollerRef$current3 = _scrollerRef$current3.querySelector("[data-index=\"".concat(index, "\"]"))) === null || _scrollerRef$current3 === void 0 || _scrollerRef$current3.classList.add("hilight", "ring-1");
|
|
149
|
+
}, 100);
|
|
150
|
+
},
|
|
151
|
+
unhilight: function unhilight() {
|
|
152
|
+
var _scrollerRef$current4;
|
|
153
|
+
(_scrollerRef$current4 = scrollerRef.current) === null || _scrollerRef$current4 === void 0 || (_scrollerRef$current4 = _scrollerRef$current4.querySelector(".hilight")) === null || _scrollerRef$current4 === void 0 || _scrollerRef$current4.classList.remove("hilight", "ring-1");
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
}, [virtualizer]);
|
|
157
|
+
return /*#__PURE__*/jsxRuntime.jsx("div", {
|
|
158
|
+
ref: scrollerRef,
|
|
159
|
+
onScroll: handleScroll,
|
|
160
|
+
className: classNames("area-content bg-base overflow-auto contain-strict"),
|
|
161
|
+
children: /*#__PURE__*/jsxRuntime.jsxs("div", {
|
|
162
|
+
className: classNames("inline-flex flex-nowrap gap-2 justify-center min-h-full min-w-full", orientation === "horizontal" && "flex-col"),
|
|
163
|
+
children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
|
|
164
|
+
className: classNames("box-content inline-flex flex-nowrap", orientation === "vertical" && "flex-col", orientation === "horizontal" && "flex-row", fullWidth && "flex-1", padding === "sm" && "p-1", padding === "md" && "p-2", padding === "lg" && "p-4"),
|
|
165
|
+
children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
|
|
166
|
+
style: orientation === "vertical" ? {
|
|
167
|
+
height: virtualizer.getTotalSize()
|
|
168
|
+
} : {
|
|
169
|
+
width: virtualizer.getTotalSize()
|
|
170
|
+
},
|
|
171
|
+
children: [/*#__PURE__*/jsxRuntime.jsx("div", {
|
|
172
|
+
style: orientation === "vertical" ? {
|
|
173
|
+
height: (_virtualItems$3 = virtualItems[0]) === null || _virtualItems$3 === void 0 ? void 0 : _virtualItems$3.start
|
|
174
|
+
} : {
|
|
175
|
+
width: (_virtualItems$4 = virtualItems[0]) === null || _virtualItems$4 === void 0 ? void 0 : _virtualItems$4.start,
|
|
176
|
+
display: "inline-block"
|
|
177
|
+
}
|
|
178
|
+
}), virtualItems.map(function (_ref3) {
|
|
179
|
+
var key = _ref3.key,
|
|
180
|
+
index = _ref3.index;
|
|
181
|
+
return /*#__PURE__*/jsxRuntime.jsx("div", {
|
|
182
|
+
"data-index": index,
|
|
183
|
+
ref: virtualizer.measureElement,
|
|
184
|
+
className: classNames("rounded-capped", orientation === "horizontal" ? "inline-grid" : "grid"),
|
|
185
|
+
children: children(itemList[index])
|
|
186
|
+
}, key);
|
|
187
|
+
})]
|
|
188
|
+
}), onLoadMore && /*#__PURE__*/jsxRuntime.jsx("div", {
|
|
189
|
+
className: orientation === "horizontal" ? "min-w-16" : "h-16",
|
|
190
|
+
children: loading && /*#__PURE__*/jsxRuntime.jsx(core.Skeleton, {})
|
|
191
|
+
})]
|
|
192
|
+
}), !hideScroller && /*#__PURE__*/jsxRuntime.jsx("div", {
|
|
193
|
+
className: classNames(orientation === "vertical" && "self-end sticky bottom-2", orientation === "horizontal" && "self-end sticky end-2"),
|
|
194
|
+
children: /*#__PURE__*/jsxRuntime.jsx(Scroller.Scroller, {
|
|
195
|
+
vertical: orientation === "vertical",
|
|
196
|
+
scrollActive: scrollActive,
|
|
197
|
+
onStart: function onStart() {
|
|
198
|
+
return virtualizer.scrollToIndex(0);
|
|
199
|
+
},
|
|
200
|
+
onPrevious: function onPrevious() {
|
|
201
|
+
return scrollerRef.current && virtualizer.scrollBy(-(orientation === "vertical" ? scrollerRef.current.offsetHeight : scrollerRef.current.offsetWidth));
|
|
202
|
+
},
|
|
203
|
+
onNext: function onNext() {
|
|
204
|
+
return scrollerRef.current && virtualizer.scrollBy(orientation === "vertical" ? scrollerRef.current.offsetHeight : scrollerRef.current.offsetWidth);
|
|
205
|
+
},
|
|
206
|
+
onEnd: function onEnd() {
|
|
207
|
+
return virtualizer.scrollToIndex(count - 1);
|
|
208
|
+
}
|
|
209
|
+
})
|
|
210
|
+
})]
|
|
211
|
+
})
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
_VirtualList.displayName = "VirtualList";
|
|
215
|
+
var GenericMemo = react.memo;
|
|
216
|
+
var VirtualList = GenericMemo(_VirtualList);
|
|
217
|
+
|
|
218
|
+
exports.VirtualList = VirtualList;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@react-fabric/core');
|
|
4
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* React Fabric
|
|
8
|
+
* @version: 1.0.0
|
|
9
|
+
*
|
|
10
|
+
*
|
|
11
|
+
* The MIT License (MIT)
|
|
12
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
13
|
+
*
|
|
14
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
15
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
16
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
17
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
18
|
+
* subject to the following conditions:
|
|
19
|
+
*
|
|
20
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
21
|
+
* portions of the Software.
|
|
22
|
+
*
|
|
23
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
24
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
25
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
26
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
var Scroller = function Scroller(_ref) {
|
|
30
|
+
var _ref$vertical = _ref.vertical,
|
|
31
|
+
vertical = _ref$vertical === void 0 ? true : _ref$vertical,
|
|
32
|
+
scrollActive = _ref.scrollActive,
|
|
33
|
+
onStart = _ref.onStart,
|
|
34
|
+
onEnd = _ref.onEnd,
|
|
35
|
+
onPrevious = _ref.onPrevious,
|
|
36
|
+
onNext = _ref.onNext;
|
|
37
|
+
return /*#__PURE__*/jsxRuntime.jsxs(core.ButtonGroup, {
|
|
38
|
+
vertical: vertical,
|
|
39
|
+
variant: "link",
|
|
40
|
+
className: "outline",
|
|
41
|
+
children: [/*#__PURE__*/jsxRuntime.jsx(core.Button, {
|
|
42
|
+
rtlFlip: true,
|
|
43
|
+
size: "sm",
|
|
44
|
+
variant: "link",
|
|
45
|
+
"aria-label": "scroll to top",
|
|
46
|
+
icon: vertical ? core.CoreIcons.chevronsUp : core.CoreIcons.chevronsLeft,
|
|
47
|
+
disabled: (scrollActive & 2) === 0,
|
|
48
|
+
onClick: onStart
|
|
49
|
+
}), /*#__PURE__*/jsxRuntime.jsx(core.Button, {
|
|
50
|
+
rtlFlip: true,
|
|
51
|
+
size: "sm",
|
|
52
|
+
variant: "link",
|
|
53
|
+
"aria-label": "scroll up",
|
|
54
|
+
icon: vertical ? core.CoreIcons.chevronUp : core.CoreIcons.chevronLeft,
|
|
55
|
+
disabled: (scrollActive & 2) === 0,
|
|
56
|
+
onClick: onPrevious
|
|
57
|
+
}), /*#__PURE__*/jsxRuntime.jsx(core.Button, {
|
|
58
|
+
rtlFlip: true,
|
|
59
|
+
size: "sm",
|
|
60
|
+
variant: "link",
|
|
61
|
+
"aria-label": "scroll down",
|
|
62
|
+
icon: vertical ? core.CoreIcons.chevronDown : core.CoreIcons.chevronRight,
|
|
63
|
+
disabled: scrollActive % 2 === 0,
|
|
64
|
+
onClick: onNext
|
|
65
|
+
}), /*#__PURE__*/jsxRuntime.jsx(core.Button, {
|
|
66
|
+
rtlFlip: true,
|
|
67
|
+
size: "sm",
|
|
68
|
+
variant: "link",
|
|
69
|
+
"aria-label": "scroll to bottom",
|
|
70
|
+
icon: vertical ? core.CoreIcons.chevronsDown : core.CoreIcons.chevronsRight,
|
|
71
|
+
disabled: scrollActive % 2 === 0,
|
|
72
|
+
onClick: onEnd
|
|
73
|
+
})]
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
exports.Scroller = Scroller;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
|
|
4
|
+
import { EmptyContent, Icon, ActionLabel, Button, CoreIcons } from '@react-fabric/core';
|
|
5
|
+
import { compareValues, Format } from '@react-fabric/utilities';
|
|
6
|
+
import classNames from 'classnames';
|
|
7
|
+
import { useMemo } from 'react';
|
|
8
|
+
import { useTranslation } from 'react-i18next';
|
|
9
|
+
import classes from './Histogram.module.css';
|
|
10
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
11
|
+
|
|
12
|
+
var emptyIcon = "M20.84 22.73L11.11 13H3V11H9.11L6.11 8H3V6H4.11L1.11 3L2.39 1.73L22.11 21.46L20.84 22.73M15 11H14.2L15 11.8V11M21 8V6H9.2L11.2 8H21M3 18H9V16H3V18Z";
|
|
13
|
+
var Histogram = function Histogram(_ref) {
|
|
14
|
+
var _ref$items = _ref.items,
|
|
15
|
+
items = _ref$items === void 0 ? [] : _ref$items,
|
|
16
|
+
total = _ref.total,
|
|
17
|
+
_ref$color = _ref.color,
|
|
18
|
+
color = _ref$color === void 0 ? "primary" : _ref$color,
|
|
19
|
+
_ref$sortBy = _ref.sortBy,
|
|
20
|
+
sortBy = _ref$sortBy === void 0 ? "count" : _ref$sortBy,
|
|
21
|
+
emptyTitle = _ref.emptyTitle,
|
|
22
|
+
emptyMessage = _ref.emptyMessage,
|
|
23
|
+
_onClick = _ref.onClick;
|
|
24
|
+
var _useTranslation = useTranslation("data"),
|
|
25
|
+
t = _useTranslation.t;
|
|
26
|
+
var maxTotal = useMemo(function () {
|
|
27
|
+
if (total) return total;
|
|
28
|
+
return Math.max.apply(Math, _toConsumableArray(items.map(function (item) {
|
|
29
|
+
return item.count;
|
|
30
|
+
})));
|
|
31
|
+
}, [total, items]);
|
|
32
|
+
var sorted = useMemo(function () {
|
|
33
|
+
return items.sort(compareValues(sortBy === "label" ? "asc" : "desc", sortBy));
|
|
34
|
+
}, [items, sortBy]);
|
|
35
|
+
return /*#__PURE__*/jsxs("div", {
|
|
36
|
+
className: classes.histogram,
|
|
37
|
+
style: {
|
|
38
|
+
"--color": "var(--".concat(color, ")")
|
|
39
|
+
},
|
|
40
|
+
children: [sorted.length === 0 && /*#__PURE__*/jsx(EmptyContent, {
|
|
41
|
+
icon: /*#__PURE__*/jsx(Icon, {
|
|
42
|
+
icon: emptyIcon,
|
|
43
|
+
size: "2.5rem"
|
|
44
|
+
}),
|
|
45
|
+
title: emptyTitle,
|
|
46
|
+
message: emptyMessage !== null && emptyMessage !== void 0 ? emptyMessage : t("histogram.empty")
|
|
47
|
+
}), sorted === null || sorted === void 0 ? void 0 : sorted.map(function (item) {
|
|
48
|
+
return /*#__PURE__*/jsxs("div", {
|
|
49
|
+
role: "none",
|
|
50
|
+
"data-disabled": item.count === 0 || item.disabled,
|
|
51
|
+
className: classNames("relative cursor-pointer flex flex-nowrap items-center select-none overflow-hidden min-h-6 my-px", classes.histoLabel, (item.count === 0 || item.disabled) && "pointer-events-none"),
|
|
52
|
+
onClick: function onClick(e) {
|
|
53
|
+
return [_onClick === null || _onClick === void 0 ? void 0 : _onClick(item.id), e.stopPropagation()];
|
|
54
|
+
},
|
|
55
|
+
children: [/*#__PURE__*/jsx("div", {
|
|
56
|
+
className: classNames("absolute z-0 inset-0 opacity-50 min-w-2 ltr:bg-gradient-to-r rtl:bg-gradient-to-l from-0% to-100%", classes.histoBar),
|
|
57
|
+
style: {
|
|
58
|
+
width: "".concat(Math.min(item.count / maxTotal, 1) * 100, "%")
|
|
59
|
+
}
|
|
60
|
+
}), /*#__PURE__*/jsx(ActionLabel, {
|
|
61
|
+
className: "flex-1 truncate z-1 text-sm cursor-pointer font-medium",
|
|
62
|
+
actions: [/*#__PURE__*/jsx(Button, {
|
|
63
|
+
color: "danger",
|
|
64
|
+
size: "sm",
|
|
65
|
+
icon: CoreIcons.funnelMinus,
|
|
66
|
+
"aria-label": "Filter Negative",
|
|
67
|
+
onClick: function onClick() {
|
|
68
|
+
return _onClick === null || _onClick === void 0 ? void 0 : _onClick(item.id, true);
|
|
69
|
+
}
|
|
70
|
+
}, "negative"), /*#__PURE__*/jsx(Button, {
|
|
71
|
+
color: "primary",
|
|
72
|
+
size: "sm",
|
|
73
|
+
icon: CoreIcons.funnelPlus,
|
|
74
|
+
"aria-label": "Filter",
|
|
75
|
+
onClick: function onClick() {
|
|
76
|
+
return _onClick === null || _onClick === void 0 ? void 0 : _onClick(item.id);
|
|
77
|
+
}
|
|
78
|
+
}, "positive")],
|
|
79
|
+
children: item.label
|
|
80
|
+
}), /*#__PURE__*/jsx("span", {
|
|
81
|
+
className: classNames("z-1 rounded-full text-xs font-medium px-2 py-1 leading-none", classes.histoBadge),
|
|
82
|
+
children: Format.number(item.count)
|
|
83
|
+
})]
|
|
84
|
+
}, item.id);
|
|
85
|
+
})]
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export { Histogram };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@layer comp{.histogram{.histoLabel{--tw-gradient-from:var(--color) var(--tw-gradient-from-position);--tw-gradient-to:color-mix(in lab,var(--color) 20%,var(--fabric-bg)) var(--tw-gradient-to-position);--tw-gradient-stops:var(--tw-gradient-from),var(--tw-gradient-to);background-color:color(from var(--color) srgb r g b/5%);&:hover{background-color:color(from var(--color) srgb r g b/15%)}&[data-disabled=true]{--color:theme(colors.tint.300);color:theme(colors.tint.600)}}.histoBar{box-shadow:-2px 0 0 var(--color) inset;:global([dir=rtl]) &{box-shadow:2px 0 0 var(--color) inset}}.histoBadge{background-color:color(from var(--color) srgb r g b/15%)}}}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
4
|
+
import { isString, matchString } from '@react-fabric/utilities';
|
|
5
|
+
import { useState, useDeferredValue, useTransition, useCallback, useEffect } from 'react';
|
|
6
|
+
|
|
7
|
+
var filterList = function filterList(items) {
|
|
8
|
+
var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
|
|
9
|
+
var matcher = arguments.length > 2 ? arguments[2] : undefined;
|
|
10
|
+
var newList = [];
|
|
11
|
+
items.forEach(function (item) {
|
|
12
|
+
if (matcher) {
|
|
13
|
+
return (matcher === null || matcher === void 0 ? void 0 : matcher(item, query)) && newList.push(item);
|
|
14
|
+
}
|
|
15
|
+
if (query && isString(query)) {
|
|
16
|
+
var _item$label;
|
|
17
|
+
if (matchString((_item$label = item.label) !== null && _item$label !== void 0 ? _item$label : item.toString(), query)) {
|
|
18
|
+
return newList.push(item);
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
return newList.push(item);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return newList;
|
|
25
|
+
};
|
|
26
|
+
var useFilteredList = function useFilteredList(items, matcher) {
|
|
27
|
+
var _useState = useState(),
|
|
28
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
29
|
+
query = _useState2[0],
|
|
30
|
+
setQuery = _useState2[1];
|
|
31
|
+
var _items = useDeferredValue(items !== null && items !== void 0 ? items : []);
|
|
32
|
+
var _useState3 = useState([]),
|
|
33
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
34
|
+
filteredList = _useState4[0],
|
|
35
|
+
setFilteredList = _useState4[1];
|
|
36
|
+
var _useTransition = useTransition(),
|
|
37
|
+
_useTransition2 = _slicedToArray(_useTransition, 2),
|
|
38
|
+
isSearching = _useTransition2[0],
|
|
39
|
+
startTransition = _useTransition2[1];
|
|
40
|
+
var filterItems = useCallback(function (items, query) {
|
|
41
|
+
return startTransition(function () {
|
|
42
|
+
return setFilteredList(filterList(items, query, matcher));
|
|
43
|
+
});
|
|
44
|
+
}, []);
|
|
45
|
+
useEffect(function () {
|
|
46
|
+
if (query) filterItems(_items, query);else setFilteredList(items);
|
|
47
|
+
}, [query, _items]);
|
|
48
|
+
return {
|
|
49
|
+
onSearch: setQuery,
|
|
50
|
+
query: query,
|
|
51
|
+
filteredList: filteredList,
|
|
52
|
+
isSearching: isSearching
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export { useFilteredList };
|