@kne/react-filter 1.0.6 → 1.0.7
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/index.js +14 -13
- package/dist/index.js.map +1 -1
- package/dist/index.modern.js +14 -13
- package/dist/index.modern.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -132,13 +132,14 @@ const useVisibleItemCount = ({
|
|
|
132
132
|
enabled,
|
|
133
133
|
strategy = 'asc'
|
|
134
134
|
}) => {
|
|
135
|
+
const safeItems = Array.isArray(items) ? items : [];
|
|
135
136
|
const containerRef = react.useRef(null);
|
|
136
137
|
const measureRef = react.useRef(null);
|
|
137
138
|
const moreMeasureRef = react.useRef(null);
|
|
138
139
|
const rafRef = react.useRef(null);
|
|
139
140
|
const lastWidthRef = react.useRef(0);
|
|
140
141
|
const [containerEl, setContainerEl] = react.useState(null);
|
|
141
|
-
const [visibleCount, setVisibleCount] = react.useState(() =>
|
|
142
|
+
const [visibleCount, setVisibleCount] = react.useState(() => safeItems.length > 0 ? 1 : 0);
|
|
142
143
|
const setContainerRef = react.useCallback(node => {
|
|
143
144
|
containerRef.current = node;
|
|
144
145
|
setContainerEl(node || null);
|
|
@@ -164,7 +165,7 @@ const useVisibleItemCount = ({
|
|
|
164
165
|
return null;
|
|
165
166
|
}
|
|
166
167
|
const moreButtonWidth = moreMeasureRef.current?.getBoundingClientRect().width || 0;
|
|
167
|
-
if (
|
|
168
|
+
if (safeItems.length > 1 && moreButtonWidth <= 0) {
|
|
168
169
|
return null;
|
|
169
170
|
}
|
|
170
171
|
const itemWidths = Array.from(itemNodes, node => node.getBoundingClientRect().width);
|
|
@@ -173,24 +174,24 @@ const useVisibleItemCount = ({
|
|
|
173
174
|
itemWidths,
|
|
174
175
|
moreButtonWidth
|
|
175
176
|
};
|
|
176
|
-
}, [
|
|
177
|
+
}, [safeItems.length]);
|
|
177
178
|
const getWidthForCount = react.useCallback((itemWidths, moreButtonWidth, count) => {
|
|
178
179
|
let usedWidth = 0;
|
|
179
180
|
for (let i = 0; i < count; i++) {
|
|
180
181
|
usedWidth += itemWidths[i] + (i > 0 ? GAP : 0);
|
|
181
182
|
}
|
|
182
|
-
if (count <
|
|
183
|
+
if (count < safeItems.length) {
|
|
183
184
|
usedWidth += GAP + moreButtonWidth;
|
|
184
185
|
}
|
|
185
186
|
return usedWidth;
|
|
186
|
-
}, [
|
|
187
|
+
}, [safeItems.length]);
|
|
187
188
|
const resolveVisibleCount = react.useCallback(widths => {
|
|
188
189
|
const {
|
|
189
190
|
containerWidth,
|
|
190
191
|
itemWidths,
|
|
191
192
|
moreButtonWidth
|
|
192
193
|
} = widths;
|
|
193
|
-
const maxCount = Math.min(
|
|
194
|
+
const maxCount = Math.min(safeItems.length, itemWidths.length);
|
|
194
195
|
let count = 1;
|
|
195
196
|
if (strategy === 'desc') {
|
|
196
197
|
count = maxCount;
|
|
@@ -213,10 +214,10 @@ const useVisibleItemCount = ({
|
|
|
213
214
|
}
|
|
214
215
|
}
|
|
215
216
|
return Math.max(1, count);
|
|
216
|
-
}, [getWidthForCount,
|
|
217
|
+
}, [getWidthForCount, safeItems.length, strategy]);
|
|
217
218
|
const calculateByWidth = react.useCallback(() => {
|
|
218
|
-
if (!enabled || !
|
|
219
|
-
setVisibleCount(prev => prev ===
|
|
219
|
+
if (!enabled || !safeItems.length) {
|
|
220
|
+
setVisibleCount(prev => prev === safeItems.length ? prev : safeItems.length);
|
|
220
221
|
return;
|
|
221
222
|
}
|
|
222
223
|
const widths = getWidths();
|
|
@@ -225,7 +226,7 @@ const useVisibleItemCount = ({
|
|
|
225
226
|
}
|
|
226
227
|
const nextCount = resolveVisibleCount(widths);
|
|
227
228
|
setVisibleCount(prev => prev === nextCount ? prev : nextCount);
|
|
228
|
-
}, [enabled, getWidths,
|
|
229
|
+
}, [enabled, getWidths, safeItems.length, resolveVisibleCount]);
|
|
229
230
|
const scheduleCalculate = react.useCallback(() => {
|
|
230
231
|
if (rafRef.current) {
|
|
231
232
|
window.cancelAnimationFrame(rafRef.current);
|
|
@@ -243,7 +244,7 @@ const useVisibleItemCount = ({
|
|
|
243
244
|
rafRef.current = null;
|
|
244
245
|
}
|
|
245
246
|
};
|
|
246
|
-
}, [
|
|
247
|
+
}, [safeItems, scheduleCalculate]);
|
|
247
248
|
react.useEffect(() => {
|
|
248
249
|
if (!enabled || !containerEl) {
|
|
249
250
|
return undefined;
|
|
@@ -298,14 +299,14 @@ const normalizeFilterList = list => {
|
|
|
298
299
|
if (isNestedFilterList(safeList)) {
|
|
299
300
|
return {
|
|
300
301
|
mode: 'nested',
|
|
301
|
-
items:
|
|
302
|
+
items: [],
|
|
302
303
|
lines: safeList
|
|
303
304
|
};
|
|
304
305
|
}
|
|
305
306
|
return {
|
|
306
307
|
mode: 'flat',
|
|
307
308
|
items: safeList,
|
|
308
|
-
lines:
|
|
309
|
+
lines: []
|
|
309
310
|
};
|
|
310
311
|
};
|
|
311
312
|
const getMobileFilterList = list => {
|