Skip to content

Commit e26ac29

Browse files
fix: Improve activeIndex initialization in Select component
- Split useEffect for filtered items and dropdown open state - Ensure activeIndex is always reset to 0 when dropdown opens - Improve test stability with longer wait time and additional checks
1 parent f0d9cd5 commit e26ac29

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

apps/docs/src/remix-hook-form/select.stories.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,12 @@ export const KeyboardNavigation: Story = {
381381
const firstOption = within(listbox).getByRole('option', { name: 'Alabama' });
382382
expect(firstOption).toHaveAttribute('id', firstOptionId);
383383

384-
// Wait a bit for the component to fully initialize
385-
await new Promise(resolve => setTimeout(resolve, 100));
384+
// Wait a bit for the component to fully initialize and ensure it's stable
385+
await new Promise(resolve => setTimeout(resolve, 200));
386+
387+
// Check that the first option is still active (no unintended keyboard events)
388+
const currentActiveOptionId = searchInput.getAttribute('aria-activedescendant');
389+
expect(currentActiveOptionId).toBe(firstOptionId);
386390
expect(firstOption).toHaveAttribute('data-active', 'true');
387391
});
388392

packages/components/src/ui/select.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,17 @@ export function Select({
7474
[options, query],
7575
);
7676

77-
// Reset activeIndex when filtered items change or dropdown opens
77+
// Reset activeIndex when filtered items change
7878
React.useEffect(() => {
79-
if (filtered.length > 0) {
79+
setActiveIndex(0);
80+
}, [filtered]);
81+
82+
// Reset activeIndex when dropdown opens
83+
React.useEffect(() => {
84+
if (popoverState.isOpen) {
8085
setActiveIndex(0);
8186
}
82-
}, [filtered, popoverState.isOpen]);
87+
}, [popoverState.isOpen]);
8388

8489
// Scroll active item into view when activeIndex changes
8590
React.useEffect(() => {

0 commit comments

Comments
 (0)