Skip to content

Commit

Permalink
ToggleGroupControl: Don't set value on focus after a reset (#66151)
Browse files Browse the repository at this point in the history
* Add test

* ToggleGroupControl: Don't set value on focus after a reset

* Add changelog

* Rename to `selectedValueIsEmpty`

Co-authored-by: mirka <[email protected]>
Co-authored-by: tyxla <[email protected]>
Co-authored-by: ciampo <[email protected]>
# Conflicts:
#	packages/components/CHANGELOG.md
  • Loading branch information
mirka committed Oct 17, 2024
1 parent 97e96d7 commit 4a7de72
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fixes

- `ToggleGroupControl`: Don't set value on focus after a reset ([#66151](https://github.com/WordPress/gutenberg/pull/66151)).

## 28.8.6 (2024-10-14)

### Bug Fixes
Expand Down
26 changes: 26 additions & 0 deletions packages/components/src/toggle-group-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,32 @@ describe.each( [
expect( radio ).not.toBeChecked();
} );

if ( mode === 'controlled' ) {
it( 'should not set a value on focus, after the value is reset', async () => {
render(
<Component label="Test Toggle Group Control" value="jack">
{ options }
</Component>
);

expect( screen.getByRole( 'radio', { name: 'J' } ) ).toBeChecked();

await click( screen.getByRole( 'button', { name: 'Reset' } ) );

expect(
screen.getByRole( 'radio', { name: 'J' } )
).not.toBeChecked();

await press.ShiftTab();
expect(
screen.getByRole( 'radio', { name: 'R' } )
).not.toBeChecked();
expect(
screen.getByRole( 'radio', { name: 'J' } )
).not.toBeChecked();
} );
}

it( 'should render tooltip where `showTooltip` === `true`', async () => {
render(
<Component label="Test Toggle Group Control">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,14 @@ function ToggleGroupControlOptionBase(
<Ariakit.Radio
disabled={ disabled }
onFocusVisible={ () => {
const selectedValueIsEmpty =
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 ||
! selectedValueIsEmpty ||
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 @@ -72,6 +72,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(
() =>
( {
Expand Down

0 comments on commit 4a7de72

Please sign in to comment.