Skip to content

Commit e946b01

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 5dcd45a commit e946b01

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

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
@@ -77,12 +77,17 @@ export function Select({
7777
[options, query],
7878
);
7979

80-
// Reset activeIndex when filtered items change or dropdown opens
80+
// Reset activeIndex when filtered items change
8181
React.useEffect(() => {
82-
if (filtered.length > 0) {
82+
setActiveIndex(0);
83+
}, [filtered]);
84+
85+
// Reset activeIndex when dropdown opens
86+
React.useEffect(() => {
87+
if (popoverState.isOpen) {
8388
setActiveIndex(0);
8489
}
85-
}, [filtered, popoverState.isOpen]);
90+
}, [popoverState.isOpen]);
8691

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

0 commit comments

Comments
 (0)