Skip to content

Commit

Permalink
ToggleGroupControl: Don't set value on focus after a reset
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Oct 15, 2024
1 parent 9a85cd8 commit 65e2a9b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,14 @@ function ToggleGroupControlOptionBase(
<Ariakit.Radio
disabled={ disabled }
onFocusVisible={ () => {
const selectedValueIsNullish =
toggleGroupControlContext.value === null ||
toggleGroupControlContext.value === '';

// 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 ||
! selectedValueIsNullish ||
toggleGroupControlContext.activeItemIsNotFirstItem?.()
) {
toggleGroupControlContext.setValue( value );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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 ] );

const groupContextValue = useMemo(
(): ToggleGroupControlContextProps => ( {
activeItemIsNotFirstItem: () =>
Expand Down

0 comments on commit 65e2a9b

Please sign in to comment.