-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 for HiddenSelect Component Auto-Completion Issue #7670
base: main
Are you sure you want to change the base?
Conversation
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, and thanks for the demo link!
@@ -68,6 +68,7 @@ export function useHiddenSelect<T>(props: AriaHiddenSelectOptions, state: Select | |||
let {autoComplete, name = data.name, isDisabled = data.isDisabled} = props; | |||
let {validationBehavior, isRequired} = data; | |||
let {visuallyHiddenProps} = useVisuallyHidden(); | |||
let selectRefValue = props.selectRef?.current?.value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading from a ref during render is typically unsafe and unfortunately it's hard to tell when it potentially breaks React's concurrent mode since they don't expose any of that for tests.
We should try to find another way to handle this.
does the hidden select fire an onChange that we aren't listening to? I'm worried that the state doesn't know about this value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for raising this concern!
This repository already has a lint rule to detect issues related to reading from refs during rendering. In my initial proposal, the lint failed because it did indeed read from a ref during render. However, I addressed this, and the lint has now passed.
Regarding the use of selectRef
, there are existing references to it in this function:
- Lines 73, 76, and 77.
- Additionally, line 74 calls
useFormValidation
and passesselectRef
as a parameter. WithinuseFormValidation
,ref
is heavily used to access the ref's value.
@snowystinger given that the lint passes for this case and considering the function's existing use of refs, I believe it’s safe to use selectRef
here. 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The concern that the state might not be aware of the value is valid. I added a debugger
to the demo link to check everything is still working as intended. By opening the DevTools and triggering the native browser autocomplete, we can see that the value is set. Additionally, we can check all values when we submit the form.
Closes #7660
Summary
This pull request addresses the issue #7660, focusing on fixing the value assignment in the
HiddenSelect
component within the@react-aria/select
package. The changes ensure that thevalue
attribute now defaults to the current value ofselectRef
whenstate.selectedKey
isn't set. This should enhance the native browser autocomplete.Changes Made
packages/@react-aria/select/src/HiddenSelect.tsx
value
property in theuseHiddenSelect
function to default toprops.selectRef?.current?.value
ifstate.selectedKey
is unavailable.Verification
HiddenSelect
component now assigns thevalue
attribute, and the fallback mechanism works as intended!Additional Context
When the browser triggers autocomplete, it first fills out the hidden select component before passing the value to the visible React Aria Select component. Unfortunately, there was a hiccup where the values weren't being passed down properly, which meant the select form field wasn't being filled out correctly by the native browser autocomplete.
Demo Link - Inspired by the sandbox @nabanitabania shared in ISSUE-7660.
Feel free to share thoughts!
✅ Pull Request Checklist:
📝 Test Instructions:
There isn't a specific React Aria form story to test this issue, but I've included a demo link to check it out. It was inspired by the sandbox that @nabanitabania shared in a previous PR.
🧢 Your Project: