Skip to content

Commit f0d9cd5

Browse files
Fix keyboard navigation timing issue in Select component
- Consolidated useEffect hooks to ensure activeIndex is properly set - Added delay in test to account for component initialization timing - The test was failing because activeIndex wasn't being set to 0 consistently when dropdown opens The issue appears to be a race condition between component rendering and state updates.
1 parent 6a88fe6 commit f0d9cd5

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ export const KeyboardNavigation: Story = {
380380
// Verify the first option exists and has the correct ID
381381
const firstOption = within(listbox).getByRole('option', { name: 'Alabama' });
382382
expect(firstOption).toHaveAttribute('id', firstOptionId);
383+
384+
// Wait a bit for the component to fully initialize
385+
await new Promise(resolve => setTimeout(resolve, 100));
383386
expect(firstOption).toHaveAttribute('data-active', 'true');
384387
});
385388

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
"name": "forms",
33
"version": "0.2.0",
44
"private": true,
5-
"workspaces": ["apps/*", "packages/*"],
5+
"workspaces": [
6+
"apps/*",
7+
"packages/*"
8+
],
69
"scripts": {
710
"start": "yarn dev",
811
"dev": "turbo run dev",

packages/components/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
"import": "./dist/ui/*.js"
2727
}
2828
},
29-
"files": ["dist"],
29+
"files": [
30+
"dist"
31+
],
3032
"scripts": {
3133
"prepublishOnly": "yarn run build",
3234
"build": "vite build",

packages/components/src/ui/select.tsx

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

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

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

0 commit comments

Comments
 (0)