Skip to content

Commit

Permalink
fix(@clayui/core): fixes bug when visual focus is lost when items are…
Browse files Browse the repository at this point in the history
… updated in Picker
  • Loading branch information
matuzalemsteles committed Feb 9, 2024
1 parent af20c55 commit 340064d
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/clay-core/src/picker/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export function Picker<T extends Record<string, any> | string | number>({
});

const [activeDescendant, setActiveDescendant] = useState(() =>
typeof selectedKey !== 'undefined'
selectedKey || selectedKey === 0
? selectedKey
: collection.getFirstItem().key
);
Expand Down Expand Up @@ -272,6 +272,16 @@ export function Picker<T extends Record<string, any> | string | number>({
}
}, [active]);

// When the items are updated, the visual focus is stale, meaning that when
// navigating via the keyboard it will not work because the key does not
// exist in the list, so we need to update the visual focus when the list
// is updated during the component life cycle.
useEffect(() => {
if (active && !collection.getItem(selectedKey)) {
setActiveDescendant(collection.getFirstItem().key);
}
}, [active, items]);

const [isArrowVisible, setIsArrowVisible] = useState<
null | 'top' | 'bottom' | 'both'
>(null);
Expand Down Expand Up @@ -491,9 +501,9 @@ export function Picker<T extends Record<string, any> | string | number>({
onPress();
} else {
const key =
typeof selectedKey === 'undefined'
? collection.getFirstItem().key
: selectedKey;
selectedKey || selectedKey === 0
? selectedKey
: collection.getFirstItem().key;

if (key !== activeDescendant) {
setActiveDescendant(key);
Expand Down

0 comments on commit 340064d

Please sign in to comment.