Skip to content

Commit c383d29

Browse files
authored
fix(FilterPicker): sorting behavior in controlled mode (#742)
1 parent 4474403 commit c383d29

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

.changeset/gold-bats-live.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cube-dev/ui-kit": patch
3+
---
4+
5+
Fix the FilterPicker sorting behavior in controlled mode.

src/components/fields/FilterPicker/FilterPicker.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,10 @@ export const FilterPicker = forwardRef(function FilterPicker<T extends object>(
542542
// If children is not provided or is a render function, return it as-is
543543
if (!children || typeof children === 'function') return children;
544544

545-
// When the popover is **closed**, reuse the cached order if we have it to
546-
// avoid unnecessary reflows. If we don't have a cache yet (first open),
547-
// fall through to compute the sorted order so the very first render is
548-
// already correct.
549-
if (!isPopoverOpen && cachedChildrenOrder.current) {
545+
// Reuse the cached order if we have it. We only want to compute the sorted
546+
// order once per pop-over opening session. The cache is cleared when the
547+
// pop-over closes so the next opening can recompute.
548+
if (cachedChildrenOrder.current) {
550549
return cachedChildrenOrder.current;
551550
}
552551

@@ -695,8 +694,9 @@ export const FilterPicker = forwardRef(function FilterPicker<T extends object>(
695694
const getSortedItems = useCallback(() => {
696695
if (!items) return items;
697696

698-
// Reuse cached order when popover is closed to avoid needless re-renders
699-
if (!isPopoverOpen && cachedItemsOrder.current) {
697+
// Reuse the cached order if we have it. We only compute the sorted array
698+
// once when the pop-over is opened. Cache is cleared on close.
699+
if (cachedItemsOrder.current) {
700700
return cachedItemsOrder.current;
701701
}
702702

@@ -836,10 +836,11 @@ export const FilterPicker = forwardRef(function FilterPicker<T extends object>(
836836
if (state.isOpen !== isPopoverOpen) {
837837
setIsPopoverOpen(state.isOpen);
838838
if (!state.isOpen) {
839-
// Popover just closed – preserve the current sorted order so the
840-
// fade-out animation keeps its layout unchanged. We only need to
841-
// record the latest selection for the next session.
839+
// Popover just closed – record the latest selection for the next opening
840+
// and clear the cached order so the next session can compute afresh.
842841
selectionsWhenClosed.current = { ...latestSelectionRef.current };
842+
cachedChildrenOrder.current = null;
843+
cachedItemsOrder.current = null;
843844
}
844845
}
845846
}, [state.isOpen, isPopoverOpen]);

0 commit comments

Comments
 (0)