Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow define RadioControl label with ReactNode component #66615

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from
3 changes: 2 additions & 1 deletion packages/components/src/radio-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ A function that receives the value of the new option that is being selected as i

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

- `label`: `string` The label to be shown to the user.
- `label`: `ReactNode` The label to be shown to the user. When the label is not a string, make sure that the element is accessibly labeled.
retrofox marked this conversation as resolved.
Show resolved Hide resolved
- ```
- `value`: `string` The internal value compared against select and passed to onChange.

* Required: No
Expand Down
40 changes: 40 additions & 0 deletions packages/components/src/radio-control/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import type { Meta, StoryFn } from '@storybook/react';
/**
* WordPress dependencies
*/
import { starFilled } from '@wordpress/icons';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import RadioControl from '..';
import Icon from '../../icon';

const meta: Meta< typeof RadioControl > = {
component: RadioControl,
Expand Down Expand Up @@ -91,3 +93,41 @@ WithOptionDescriptions.args = {
},
],
};

/**
* When the label is not a string,
* make sure that the element is accessibly labeled.
*/
export const WithComponentLabels: StoryFn< typeof RadioControl > =
retrofox marked this conversation as resolved.
Show resolved Hide resolved
Template.bind( {} );

function Rating( {
stars,
...restProps
}: { stars: number } & JSX.IntrinsicElements[ 'div' ] ) {
return (
<div style={ { display: 'flex' } } { ...restProps }>
{ Array.from( { length: stars }, ( _, index ) => (
<Icon key={ index } icon={ starFilled } />
) ) }
</div>
);
}

WithComponentLabels.args = {
label: 'Rating',
options: [
{
label: <Rating stars={ 3 } aria-label="Three Stars" />,
value: '3',
},
{
label: <Rating stars={ 2 } aria-label="Two Stars" />,
value: '2',
},
{
label: <Rating stars={ 1 } aria-label="One Star" />,
value: '1',
},
],
};
6 changes: 4 additions & 2 deletions packages/components/src/radio-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ export type RadioControlProps = Pick<
*/
options?: {
/**
* The label to be shown to the user
* The label to be shown to the user.
retrofox marked this conversation as resolved.
Show resolved Hide resolved
*
* When the label is not a string, make sure that the element is accessibly labeled.
*/
label: string;
label: string | React.ReactElement;
/**
* The internal value compared against select and passed to onChange
*/
Expand Down
Loading