Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@clayui/core): fixes bug when visual focus is lost when items are updated in Picker #5766

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 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,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
Loading