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 @@ -17,6 +17,7 @@

### Enhancements

- `RadioControl`: Allow individual options to be disabled while the rest of the group remains available ([#82026](https://github.com/WordPress/gutenberg/pull/82026)).
- `PaletteEdit`: Add a duotone variant. Passing `duotones` (with an optional `colorPalette` for the shadows and highlights pickers) edits duotone presets with the same UI as colors and gradients. Palette colors that do not resolve to a concrete value, such as `color-mix()`, are left out of the duotone pickers, since a duotone built from one cannot be rendered, and the rest are normalized to hex, since the front end's duotone parser does not accept CSS named colors ([#81605](https://github.com/WordPress/gutenberg/pull/81605)).
- `DuotonePicker`: Add `selectedSlug` prop for slug-based selection and pass the picked preset's index and slug to `onChange`, so two presets sharing a pair of colors keep their identity ([#81605](https://github.com/WordPress/gutenberg/pull/81605)).
- `PaletteEdit`: Name the swatch picker after the palette's heading, so it is announced as Theme, Default or Custom rather than an unlabelled list ([#81605](https://github.com/WordPress/gutenberg/pull/81605)).
Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/radio-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ A function that receives the value of the new option that is being selected as i

- Required: Yes

#### `options`: `{ label: string, value: string }[]`
#### `options`: `{ label: string, value: string, description?: string, disabled?: boolean }[]`

An array of objects containing the value and label of the options.

- `label`: `string` The label to be shown to the user.
- `value`: `string` The internal value compared against select and passed to onChange.
- `description`: `string` Optional help text shown below the label.
- `disabled`: `boolean` Whether the option is disabled.

* Required: No

Expand Down
1 change: 1 addition & 0 deletions packages/components/src/radio-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export function RadioControl(
type="radio"
name={ id }
value={ option.value }
disabled={ option.disabled }
onChange={ onChangeValue }
checked={ option.value === selected }
aria-describedby={
Expand Down
12 changes: 12 additions & 0 deletions packages/components/src/radio-control/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,15 @@ WithOptionDescriptions.args = {
},
],
};

export const WithDisabledOption: StoryFn< typeof RadioControl > = Template.bind(
{}
);
WithDisabledOption.args = {
...Default.args,
options: [
{ label: 'Public', value: 'public' },
{ label: 'Private', value: 'private', disabled: true },
{ label: 'Password Protected', value: 'password' },
],
};
4 changes: 1 addition & 3 deletions packages/components/src/radio-control/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

font-family: $default-font;
font-size: $default-font-size;

}

.components-radio-control__group-wrapper.has-help {
Expand Down Expand Up @@ -62,7 +61,6 @@
&:checked::before {
border-color: $components-color-gray-400;
opacity: 1; // Override style from wp-admin forms.css.

}
}
}
Expand All @@ -71,7 +69,7 @@
grid-column: 2;
grid-row: 1;

.components-radio-control:not(:disabled) & {
.components-radio-control__input:not(:disabled) + & {
cursor: var(--wpds-cursor-control);
}

Expand Down
59 changes: 59 additions & 0 deletions packages/components/src/radio-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ const defaultPropsWithDescriptions = {
} ) ),
};

const defaultPropsWithDisabledOption = {
...defaultProps,
options: defaultProps.options.map( ( option, index ) => ( {
...option,
disabled: index === 1,
} ) ),
};

describe.each( [
// TODO: `RadioControl` doesn't currently support uncontrolled mode.
// [ 'uncontrolled', RadioControl ],
Expand Down Expand Up @@ -81,6 +89,29 @@ describe.each( [
).toBeDisabled();
} );

it( 'should disable an individual option without disabling the radio group', () => {
render(
<Component
{ ...defaultPropsWithDisabledOption }
onChange={ () => {} }
/>
);

expect(
screen.getByRole( 'radiogroup', { name: defaultProps.label } )
).toBeEnabled();
expect(
screen.getByRole( 'radio', {
name: defaultProps.options[ 0 ].label,
} )
).toBeEnabled();
expect(
screen.getByRole( 'radio', {
name: defaultProps.options[ 1 ].label,
} )
).toBeDisabled();
} );

it( 'should describe the radio group with the help text', () => {
const onChangeSpy = jest.fn();
render(
Expand Down Expand Up @@ -166,6 +197,34 @@ describe.each( [
} );

describe( 'interaction', () => {
it( 'should keep enabled options interactive when another option is disabled', async () => {
const user = userEvent.setup();
const onChangeSpy = jest.fn();
render(
<Component
{ ...defaultPropsWithDisabledOption }
onChange={ onChangeSpy }
/>
);

await user.click(
screen.getByRole( 'radio', {
name: defaultProps.options[ 1 ].label,
} )
);
expect( onChangeSpy ).not.toHaveBeenCalled();

await user.click(
screen.getByRole( 'radio', {
name: defaultProps.options[ 2 ].label,
} )
);
expect( onChangeSpy ).toHaveBeenCalledTimes( 1 );
expect( onChangeSpy ).toHaveBeenLastCalledWith(
defaultProps.options[ 2 ].value
);
} );

it( 'should select a new value when clicking on the radio input', async () => {
const user = userEvent.setup();
const onChangeSpy = jest.fn();
Expand Down
6 changes: 6 additions & 0 deletions packages/components/src/radio-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export type RadioControlProps = Pick<
* Optional help text to be shown in addition the label.
*/
description?: string;
/**
* Whether the option should be disabled.
*
* @default false
*/
disabled?: boolean;
}[];
/**
* The value property of the currently selected option.
Expand Down
Loading