-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
ToggleGroupControl: Don't set value on focus after a reset #66151
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,10 +141,14 @@ function ToggleGroupControlOptionBase( | |
<Ariakit.Radio | ||
disabled={ disabled } | ||
onFocusVisible={ () => { | ||
const selectedValueIsEmpty = | ||
toggleGroupControlContext.value === null || | ||
toggleGroupControlContext.value === ''; | ||
Comment on lines
+145
to
+146
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to how we add a layer of |
||
|
||
// Conditions ensure that the first visible focus to a radio group | ||
// without a selected option will not automatically select the option. | ||
if ( | ||
toggleGroupControlContext.value !== null || | ||
! selectedValueIsEmpty || | ||
toggleGroupControlContext.activeItemIsNotFirstItem?.() | ||
) { | ||
toggleGroupControlContext.setValue( value ); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ import { useStoreState } from '@ariakit/react'; | |
* WordPress dependencies | ||
*/ | ||
import { useInstanceId } from '@wordpress/compose'; | ||
import { forwardRef, useMemo } from '@wordpress/element'; | ||
import { forwardRef, useEffect, useMemo } from '@wordpress/element'; | ||
import { isRTL } from '@wordpress/i18n'; | ||
|
||
/** | ||
|
@@ -73,6 +73,13 @@ function UnforwardedToggleGroupControlAsRadioGroup( | |
const selectedValue = useStoreState( radio, 'value' ); | ||
const setValue = radio.setValue; | ||
|
||
// Ensures that the active id is also reset after the value is "reset" by the consumer. | ||
useEffect( () => { | ||
if ( selectedValue === '' ) { | ||
radio.setActiveId( undefined ); | ||
} | ||
}, [ radio, selectedValue ] ); | ||
Comment on lines
+76
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can feel the tech debt catching up to us 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Food for thought: I wonder if this can be improved with a centralized controlled component layer that all controlled components would use. Probably a too idealistic idea, since some components use Ariakit and others do not, and this layer would be quite complicated to cover all cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that the most practical fix here is to change the APIs to follow the standard convention for controlled / uncontrolled component. Since that would be a breaking change, we could just release a V2 of the component that would give us also a chance to:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this case specifically, I think there might be room to argue that resetting the active id on empty-ish (or invalid) values should be handled by Ariakit out of the box. I'll try and follow up. |
||
|
||
const groupContextValue = useMemo( | ||
(): ToggleGroupControlContextProps => ( { | ||
activeItemIsNotFirstItem: () => | ||
|
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.
Should we move it next to the other
controlled
-only tests?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.
No strong opinion, but I think I prefer it here because it's closely related to the test right above it.