@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.modern.js
CHANGED
|
@@ -123,13 +123,14 @@ const useVisibleItemCount = ({
|
|
|
123
123
|
enabled,
|
|
124
124
|
strategy: _strategy = 'asc'
|
|
125
125
|
}) => {
|
|
126
|
+
const safeItems = Array.isArray(items) ? items : [];
|
|
126
127
|
const containerRef = useRef(null);
|
|
127
128
|
const measureRef = useRef(null);
|
|
128
129
|
const moreMeasureRef = useRef(null);
|
|
129
130
|
const rafRef = useRef(null);
|
|
130
131
|
const lastWidthRef = useRef(0);
|
|
131
132
|
const [containerEl, setContainerEl] = useState(null);
|
|
132
|
-
const [visibleCount, setVisibleCount] = useState(() =>
|
|
133
|
+
const [visibleCount, setVisibleCount] = useState(() => safeItems.length > 0 ? 1 : 0);
|
|
133
134
|
const setContainerRef = useCallback(node => {
|
|
134
135
|
containerRef.current = node;
|
|
135
136
|
setContainerEl(node || null);
|
|
@@ -156,7 +157,7 @@ const useVisibleItemCount = ({
|
|
|
156
157
|
return null;
|
|
157
158
|
}
|
|
158
159
|
const moreButtonWidth = ((_moreMeasureRef$curre = moreMeasureRef.current) == null ? void 0 : _moreMeasureRef$curre.getBoundingClientRect().width) || 0;
|
|
159
|
-
if (
|
|
160
|
+
if (safeItems.length > 1 && moreButtonWidth <= 0) {
|
|
160
161
|
return null;
|
|
161
162
|
}
|
|
162
163
|
const itemWidths = Array.from(itemNodes, node => node.getBoundingClientRect().width);
|
|
@@ -165,24 +166,24 @@ const useVisibleItemCount = ({
|
|
|
165
166
|
itemWidths,
|
|
166
167
|
moreButtonWidth
|
|
167
168
|
};
|
|
168
|
-
}, [
|
|
169
|
+
}, [safeItems.length]);
|
|
169
170
|
const getWidthForCount = useCallback((itemWidths, moreButtonWidth, count) => {
|
|
170
171
|
let usedWidth = 0;
|
|
171
172
|
for (let i = 0; i < count; i++) {
|
|
172
173
|
usedWidth += itemWidths[i] + (i > 0 ? GAP : 0);
|
|
173
174
|
}
|
|
174
|
-
if (count <
|
|
175
|
+
if (count < safeItems.length) {
|
|
175
176
|
usedWidth += GAP + moreButtonWidth;
|
|
176
177
|
}
|
|
177
178
|
return usedWidth;
|
|
178
|
-
}, [
|
|
179
|
+
}, [safeItems.length]);
|
|
179
180
|
const resolveVisibleCount = useCallback(widths => {
|
|
180
181
|
const {
|
|
181
182
|
containerWidth,
|
|
182
183
|
itemWidths,
|
|
183
184
|
moreButtonWidth
|
|
184
185
|
} = widths;
|
|
185
|
-
const maxCount = Math.min(
|
|
186
|
+
const maxCount = Math.min(safeItems.length, itemWidths.length);
|
|
186
187
|
let count = 1;
|
|
187
188
|
if (_strategy === 'desc') {
|
|
188
189
|
count = maxCount;
|
|
@@ -205,10 +206,10 @@ const useVisibleItemCount = ({
|
|
|
205
206
|
}
|
|
206
207
|
}
|
|
207
208
|
return Math.max(1, count);
|
|
208
|
-
}, [getWidthForCount,
|
|
209
|
+
}, [getWidthForCount, safeItems.length, _strategy]);
|
|
209
210
|
const calculateByWidth = useCallback(() => {
|
|
210
|
-
if (!enabled || !
|
|
211
|
-
setVisibleCount(prev => prev ===
|
|
211
|
+
if (!enabled || !safeItems.length) {
|
|
212
|
+
setVisibleCount(prev => prev === safeItems.length ? prev : safeItems.length);
|
|
212
213
|
return;
|
|
213
214
|
}
|
|
214
215
|
const widths = getWidths();
|
|
@@ -217,7 +218,7 @@ const useVisibleItemCount = ({
|
|
|
217
218
|
}
|
|
218
219
|
const nextCount = resolveVisibleCount(widths);
|
|
219
220
|
setVisibleCount(prev => prev === nextCount ? prev : nextCount);
|
|
220
|
-
}, [enabled, getWidths,
|
|
221
|
+
}, [enabled, getWidths, safeItems.length, resolveVisibleCount]);
|
|
221
222
|
const scheduleCalculate = useCallback(() => {
|
|
222
223
|
if (rafRef.current) {
|
|
223
224
|
window.cancelAnimationFrame(rafRef.current);
|
|
@@ -235,7 +236,7 @@ const useVisibleItemCount = ({
|
|
|
235
236
|
rafRef.current = null;
|
|
236
237
|
}
|
|
237
238
|
};
|
|
238
|
-
}, [
|
|
239
|
+
}, [safeItems, scheduleCalculate]);
|
|
239
240
|
useEffect(() => {
|
|
240
241
|
if (!enabled || !containerEl) {
|
|
241
242
|
return undefined;
|
|
@@ -291,14 +292,14 @@ const normalizeFilterList = list => {
|
|
|
291
292
|
if (isNestedFilterList(safeList)) {
|
|
292
293
|
return {
|
|
293
294
|
mode: 'nested',
|
|
294
|
-
items:
|
|
295
|
+
items: [],
|
|
295
296
|
lines: safeList
|
|
296
297
|
};
|
|
297
298
|
}
|
|
298
299
|
return {
|
|
299
300
|
mode: 'flat',
|
|
300
301
|
items: safeList,
|
|
301
|
-
lines:
|
|
302
|
+
lines: []
|
|
302
303
|
};
|
|
303
304
|
};
|
|
304
305
|
const getMobileFilterList = list => {
|