Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Enhancements

- `ToggleGroupControl`: Honor the root `disabled` prop so the whole control is unselectable ([#82259](https://github.com/WordPress/gutenberg/pull/82259)).
- Validated form controls: Use `--wpds-color-stroke-interactive-error` for the invalid-state focus ring and border ([#82410](https://github.com/WordPress/gutenberg/pull/82410)).
- `Popover`: Widen `offset` to also accept an object with separate main and cross axis offsets. The same applies to `BorderBoxControl`'s `popoverOffset` prop ([#82060](https://github.com/WordPress/gutenberg/pull/82060)).
- `ToolsPanelItem`: Add `defaultShown` to show an optional item that has no value, and an `onShownChange` callback that fires only when the user toggles the item in the panel's menu ([#78010](https://github.com/WordPress/gutenberg/pull/78010)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ const Template: StoryFn< typeof ToggleGroupControl > = ( {
onChange,
...props
} ) => {
const [ value, setValue ] =
useState< ToggleGroupControlProps[ 'value' ] >();
const [ value, setValue ] = useState< ToggleGroupControlProps[ 'value' ] >(
props.value
);

return (
<ToggleGroupControl
Expand Down Expand Up @@ -135,3 +136,15 @@ Deselectable.args = {
...WithIcons.args,
isDeselectable: true,
};

/**
* When the `disabled` prop is true, the control is unselectable.
*/
export const Disabled: StoryFn< typeof ToggleGroupControl > = Template.bind(
{}
);
Disabled.args = {
...Default.args,
disabled: true,
value: 'left',
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports[`ToggleGroupControl controlled should render correctly with icons 1`] =
>
<label
class="style-label components-base-control__label"
for="wp-components-base-control-19"
for="wp-components-base-control-27"
>
Test Toggle Group Control
</label>
Expand All @@ -19,7 +19,7 @@ exports[`ToggleGroupControl controlled should render correctly with icons 1`] =
class="style-toggle-group-control style-has-enclosing-borders components-toggle-group-control"
data-wp-c16t="true"
data-wp-component="ToggleGroupControl"
id="wp-components-base-control-19"
id="wp-components-base-control-27"
role="radiogroup"
>
<div
Expand All @@ -33,8 +33,8 @@ exports[`ToggleGroupControl controlled should render correctly with icons 1`] =
data-value="uppercase"
data-wp-c16t="true"
data-wp-component="ToggleGroupControlOptionBase"
id="wp-components-base-control-19-40"
name=":r3e:"
id="wp-components-base-control-27-57"
name=":r4k:"
role="radio"
type="button"
value="uppercase"
Expand Down Expand Up @@ -68,8 +68,8 @@ exports[`ToggleGroupControl controlled should render correctly with icons 1`] =
data-value="lowercase"
data-wp-c16t="true"
data-wp-component="ToggleGroupControlOptionBase"
id="wp-components-base-control-19-41"
name=":r3e:"
id="wp-components-base-control-27-58"
name=":r4k:"
role="radio"
type="button"
value="lowercase"
Expand Down Expand Up @@ -115,7 +115,7 @@ exports[`ToggleGroupControl controlled should render correctly with text options
>
<label
class="style-label components-base-control__label"
for="wp-components-base-control-18"
for="wp-components-base-control-26"
>
Test Toggle Group Control
</label>
Expand All @@ -124,7 +124,7 @@ exports[`ToggleGroupControl controlled should render correctly with text options
class="style-toggle-group-control style-has-enclosing-borders components-toggle-group-control"
data-wp-c16t="true"
data-wp-component="ToggleGroupControl"
id="wp-components-base-control-18"
id="wp-components-base-control-26"
role="radiogroup"
>
<div
Expand All @@ -137,8 +137,8 @@ exports[`ToggleGroupControl controlled should render correctly with text options
data-value="rigas"
data-wp-c16t="true"
data-wp-component="ToggleGroupControlOptionBase"
id="wp-components-base-control-18-38"
name=":r37:"
id="wp-components-base-control-26-55"
name=":r4d:"
role="radio"
type="button"
value="rigas"
Expand All @@ -160,8 +160,8 @@ exports[`ToggleGroupControl controlled should render correctly with text options
data-value="jack"
data-wp-c16t="true"
data-wp-component="ToggleGroupControlOptionBase"
id="wp-components-base-control-18-39"
name=":r37:"
id="wp-components-base-control-26-56"
name=":r4d:"
role="radio"
type="button"
value="jack"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ const hoverOutside = async () => {
await hover( document.body, { clientX: 10, clientY: 10 } );
};

const expectGroupChromeDisabled = ( group: HTMLElement ) => {
expect( group ).toBeVisible();
expect( group ).toHaveAttribute( 'aria-disabled', 'true' );
// `toBeEnabled()` treats aria-disabled as disabled, so it cannot
// catch an HTML `disabled` leak onto this div.
// eslint-disable-next-line jest-dom/prefer-enabled-disabled
expect( group ).not.toHaveAttribute( 'disabled' );
};

const getGeneratedEmotionClassNames = ( element: HTMLElement ) =>
Array.from( element.classList ).filter( ( className ) =>
/^(css|emotion)-/.test( className )
Expand Down Expand Up @@ -626,6 +635,228 @@ describe.each( [
} );
} );
} );

describe( 'disabled', () => {
it( 'should not select another option when the control is disabled', async () => {
const mockOnChange = jest.fn();

render(
<Component
value="rigas"
label="Test"
disabled
onChange={ mockOnChange }
>
{ options }
</Component>
);

expectGroupChromeDisabled(
screen.getByRole( 'radiogroup', { name: 'Test' } )
);

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

await click( screen.getByRole( 'radio', { name: 'J' } ) );

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

await press.ArrowRight();
expect( mockOnChange ).not.toHaveBeenCalled();
} );

it( 'should skip the disabled control when tabbing', async () => {
render(
<>
<button>Before ToggleGroupControl</button>
<Component value="rigas" label="Test" disabled>
{ options }
</Component>
<button>After ToggleGroupControl</button>
</>
);

await press.Tab();
expect(
screen.getByRole( 'button', {
name: 'Before ToggleGroupControl',
} )
).toHaveFocus();

await press.Tab();

const expectedFocusTarget =
mode === 'uncontrolled'
? screen.getByRole( 'button', {
name: 'After ToggleGroupControl',
} )
: screen.getByRole( 'button', { name: 'Reset' } );

expect( expectedFocusTarget ).toHaveFocus();
} );

it( 'should keep the selected option when the control is disabled', () => {
render(
<Component value="jack" label="Test" disabled>
{ options }
</Component>
);

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

it( 'should not call onChange when a disabled control is clicked', async () => {
const mockOnChange = jest.fn();

render(
<Component
value="rigas"
label="Test"
disabled
onChange={ mockOnChange }
>
{ options }
</Component>
);

await click( screen.getByRole( 'radio', { name: 'J' } ) );
await click( screen.getByRole( 'radio', { name: 'R' } ) );

expect( mockOnChange ).not.toHaveBeenCalled();
} );

it( 'should disable every option when the control is disabled, including options without their own disabled prop', () => {
render(
<Component value="pizza" label="Test" disabled>
{ optionsWithDisabledOption }
</Component>
);

expectGroupChromeDisabled(
screen.getByRole( 'radiogroup', { name: 'Test' } )
);

expect(
screen.getByRole( 'radio', { name: 'Pizza' } )
).toBeDisabled();
expect(
screen.getByRole( 'radio', { name: 'Rice' } )
).toBeDisabled();
expect(
screen.getByRole( 'radio', { name: 'Pasta' } )
).toBeDisabled();
} );

it( 'should not deselect the pressed option when the control is disabled', async () => {
const mockOnChange = jest.fn();

render(
<Component
value="rigas"
label="Test"
onChange={ mockOnChange }
isDeselectable
disabled
>
{ options }
</Component>
);

expectGroupChromeDisabled(
screen.getByRole( 'group', { name: 'Test' } )
);

const pressed = screen.getByRole( 'button', {
name: 'R',
pressed: true,
} );
expect( pressed ).toBeVisible();
expect( pressed ).toBeDisabled();

await click( pressed );

expect(
screen.getByRole( 'button', {
name: 'R',
pressed: true,
} )
).toBeVisible();
expect( mockOnChange ).not.toHaveBeenCalled();
} );

it( 'should skip a deselectable disabled control when tabbing', async () => {
render(
<>
<button>Before ToggleGroupControl</button>
<Component
isDeselectable
value="rigas"
label="Test"
disabled
>
{ options }
</Component>
<button>After ToggleGroupControl</button>
</>
);

await press.Tab();
expect(
screen.getByRole( 'button', {
name: 'Before ToggleGroupControl',
} )
).toHaveFocus();

await press.Tab();

const expectedFocusTarget =
mode === 'uncontrolled'
? screen.getByRole( 'button', {
name: 'After ToggleGroupControl',
} )
: screen.getByRole( 'button', { name: 'Reset' } );

expect( expectedFocusTarget ).toHaveFocus();
} );

it( 'should not call onChange when a deselectable disabled control is clicked', async () => {
const mockOnChange = jest.fn();

render(
<Component
value="rigas"
label="Test"
onChange={ mockOnChange }
isDeselectable
disabled
>
{ options }
</Component>
);

await click(
screen.getByRole( 'button', {
name: 'R',
pressed: true,
} )
);
await click(
screen.getByRole( 'button', {
name: 'J',
pressed: false,
} )
);

expect( mockOnChange ).not.toHaveBeenCalled();
} );
} );
} );

test( 'should render block styles without Emotion-generated classes', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ function ToggleGroupControlOptionBase(
...otherButtonProps
} = buttonProps;

const isOptionDisabled = Boolean(
toggleGroupControlContext.disabled || disabled
);
const isPressed = toggleGroupControlContext.value === value;
const labelClasses = clsx(
styles.label,
Expand Down Expand Up @@ -109,7 +112,7 @@ function ToggleGroupControlOptionBase(
{ isDeselectable ? (
<button
{ ...commonProps }
disabled={ disabled }
disabled={ isOptionDisabled }
aria-pressed={ isPressed }
type="button"
onClick={ buttonOnClick }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ Icon displayed as the content of the option. Usually one of the icons from the `
The text to accessibly label the icon option. Will also be shown in a tooltip.

- Required: Yes

### `disabled`: `boolean`

If true, this option is unselectable.

- Required: No
- Default: `false`
Loading
Loading