Skip to content

Commit

Permalink
ToggleGroupControl: Don't autoselect option on first group focus
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Oct 5, 2024
1 parent 90d7c3b commit 4f78454
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ exports[`ToggleGroupControl controlled should render correctly with icons 1`] =
class="components-toggle-group-control emotion-8 emotion-9"
data-wp-c16t="true"
data-wp-component="ToggleGroupControl"
id="toggle-group-control-as-radio-group-11"
id="toggle-group-control-as-radio-group-12"
role="radiogroup"
>
<div
Expand All @@ -297,7 +297,7 @@ exports[`ToggleGroupControl controlled should render correctly with icons 1`] =
data-value="uppercase"
data-wp-c16t="true"
data-wp-component="ToggleGroupControlOptionBase"
id="toggle-group-control-as-radio-group-11-30"
id="toggle-group-control-as-radio-group-12-32"
role="radio"
type="button"
value="uppercase"
Expand Down Expand Up @@ -330,7 +330,7 @@ exports[`ToggleGroupControl controlled should render correctly with icons 1`] =
data-value="lowercase"
data-wp-c16t="true"
data-wp-component="ToggleGroupControlOptionBase"
id="toggle-group-control-as-radio-group-11-31"
id="toggle-group-control-as-radio-group-12-33"
role="radio"
type="button"
value="lowercase"
Expand Down Expand Up @@ -575,7 +575,7 @@ exports[`ToggleGroupControl controlled should render correctly with text options
class="components-toggle-group-control emotion-8 emotion-9"
data-wp-c16t="true"
data-wp-component="ToggleGroupControl"
id="toggle-group-control-as-radio-group-10"
id="toggle-group-control-as-radio-group-11"
role="radiogroup"
>
<div
Expand All @@ -588,7 +588,7 @@ exports[`ToggleGroupControl controlled should render correctly with text options
data-value="rigas"
data-wp-c16t="true"
data-wp-component="ToggleGroupControlOptionBase"
id="toggle-group-control-as-radio-group-10-28"
id="toggle-group-control-as-radio-group-11-30"
role="radio"
type="button"
value="rigas"
Expand All @@ -610,7 +610,7 @@ exports[`ToggleGroupControl controlled should render correctly with text options
data-value="jack"
data-wp-c16t="true"
data-wp-component="ToggleGroupControlOptionBase"
id="toggle-group-control-as-radio-group-10-29"
id="toggle-group-control-as-radio-group-11-31"
role="radio"
type="button"
value="jack"
Expand Down
11 changes: 11 additions & 0 deletions packages/components/src/toggle-group-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { press, click, hover, sleep } from '@ariakit/test';

/**
Expand Down Expand Up @@ -162,6 +163,16 @@ describe.each( [
expect( mockOnChange ).toHaveBeenCalledWith( 'rigas' );
} );

it( 'should not set a value on focus', async () => {
render(
<Component label="Test Toggle Group Control">{ options }</Component>
);

await userEvent.tab();
const radio = screen.getByRole( 'radio', { name: 'R' } );
expect( radio ).not.toBeChecked();

Check failure on line 173 in packages/components/src/toggle-group-control/test/index.tsx

View workflow job for this annotation

GitHub Actions / JavaScript (Node.js 20) 4/4

Error: expect(element).not.toBeChecked() Received element is checked: <button aria-checked="true" aria-label="R" class="css-wg4gy8-pressed-buttonView components-toggle-group-control-option-base" data-active-item="true" data-focus-visible="true" data-value="rigas" data-wp-c16t="true" data-wp-component="ToggleGroupControlOptionBase" id="toggle-group-control-as-radio-group-5-10" role="radio" type="button" value="rigas" /> at Object.toBeChecked (/home/runner/work/gutenberg/gutenberg/packages/components/src/toggle-group-control/test/index.tsx:173:23)

Check failure on line 173 in packages/components/src/toggle-group-control/test/index.tsx

View workflow job for this annotation

GitHub Actions / JavaScript (Node.js 20) 4/4

Error: expect(element).not.toBeChecked() Received element is checked: <button aria-checked="true" aria-label="R" class="css-wg4gy8-pressed-buttonView components-toggle-group-control-option-base" data-active-item="true" data-focus-visible="true" data-value="rigas" data-wp-c16t="true" data-wp-component="ToggleGroupControlOptionBase" id="toggle-group-control-as-radio-group-16-40" role="radio" type="button" value="rigas" /> at Object.toBeChecked (/home/runner/work/gutenberg/gutenberg/packages/components/src/toggle-group-control/test/index.tsx:173:23)
} );

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 @@ -75,7 +75,6 @@ function ToggleGroupControlOptionBase(
value,
children,
showTooltip = false,
onFocus: onFocusProp,
disabled,
...otherButtonProps
} = buttonProps;
Expand Down Expand Up @@ -132,7 +131,6 @@ function ToggleGroupControlOptionBase(
<button
{ ...commonProps }
disabled={ disabled }
onFocus={ onFocusProp }
aria-pressed={ isPressed }
type="button"
onClick={ buttonOnClick }
Expand All @@ -142,19 +140,17 @@ function ToggleGroupControlOptionBase(
) : (
<Ariakit.Radio
disabled={ disabled }
render={
<button
type="button"
{ ...commonProps }
onFocus={ ( event ) => {
onFocusProp?.( event );
if ( event.defaultPrevented ) {
return;
}
toggleGroupControlContext.setValue( value );
} }
/>
}
onFocusVisible={ () => {
// 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 ||
toggleGroupControlContext.activeItemIsNotFirstItem?.()
) {
toggleGroupControlContext.setValue( value );
}
} }
render={ <button type="button" { ...commonProps } /> }
value={ value }
>
<ButtonContentView>{ children }</ButtonContentView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ function UnforwardedToggleGroupControlAsRadioGroup(

const groupContextValue = useMemo(
(): ToggleGroupControlContextProps => ( {
activeItemIsNotFirstItem: () =>
radio.getState().activeId !== radio.first(),
baseId,
isBlock: ! isAdaptiveWidth,
size,
Expand All @@ -87,6 +89,7 @@ function UnforwardedToggleGroupControlAsRadioGroup(
[
baseId,
isAdaptiveWidth,
radio,
selectedValue,
setSelectedElement,
setValue,
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/toggle-group-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export type ToggleGroupControlProps = Pick<
};

export type ToggleGroupControlContextProps = {
activeItemIsNotFirstItem?: () => boolean;
isDeselectable?: boolean;
baseId: string;
isBlock: ToggleGroupControlProps[ 'isBlock' ];
Expand Down

0 comments on commit 4f78454

Please sign in to comment.