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 15, 2024
1 parent af20c55 commit 202962b
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/clay-core/src/picker/Picker.tsx
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,19 @@ 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 (
!collection.getItem(selectedKey) &&
!collection.getItem(activeDescendant)
) {
setActiveDescendant(collection.getFirstItem().key);
}
}, [items]);

const [isArrowVisible, setIsArrowVisible] = useState<
null | 'top' | 'bottom' | 'both'
>(null);
Expand Down Expand Up @@ -491,9 +504,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 202962b

Please sign in to comment.