Skip to content

Commit a292dc3

Browse files
committed
chore!: refactor Radio to use radix primitive
1 parent a843b4c commit a292dc3

File tree

15 files changed

+294
-324
lines changed

15 files changed

+294
-324
lines changed

.changeset/beige-crabs-compete.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@strapi/design-system': major
3+
---
4+
5+
chore!: refactor Radio to use radix primitive

docs/stories/00-getting started/migration guides/migration-v1-v2.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ for any of these components to understand better how to migrate your code.
205205
- `Accordion`
206206
- `Avatar`
207207
- `Popover`
208+
- `Radio`
208209
- `Tabs`
209210
- `Tooltip`
210211

docs/stories/03-inputs/Radio.mdx

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,76 @@
11
import { Meta, Canvas } from '@storybook/blocks';
2-
import { Radio, RadioGroup } from '@strapi/design-system';
2+
import { Radio } from '@strapi/design-system';
33

44
import * as RadioStories from './Radio.stories';
55

66
<Meta of={RadioStories} />
77

88
# Radio
99

10-
Radio buttons allow users to choose a single value from a range of options.
10+
- [Overview](#overview)
11+
- [Usage](#usage)
12+
- [Props](#props)
13+
- [Variants](#variants)
14+
- [Accessibility](#accessibility)
1115

12-
**Best practices**
16+
## Overview
1317

14-
- When a user picks an option, the previous one is automatically deselected.
15-
- Radio buttons should be used when there is at least two options.
16-
- Label of the Radio button should be concise and straightforward.
17-
- Use the Checkbox component if the user could select one or several values.
18-
- Start all Radio button labels with a capital letter.
19-
- There is no need to include Radio buttons in a Select component when only a single value can be selected.
20-
- Radio buttons should be used sparingly in the interface.
21-
- Radio buttons should be used only when it is not obvious enough that a choice is required.
22-
- Radio buttons should not be used in Table components.
23-
- Use Switch or Toggle component to make binary choices.
18+
Radio buttons allow users to choose a single value from a range of options.
2419

25-
[View source](https://github.com/strapi/design-system/tree/main/packages/design-system/src/Radio)
20+
<ViewSource path="components/Radio" />
2621

27-
## Imports
22+
## Usage
2823

2924
```js
30-
import { Radio, RadioGroup } from '@strapi/design-system';
25+
import { Radio } from '@strapi/design-system';
3126
```
3227

33-
## Usage
28+
<Canvas of={RadioStories.Base} />
29+
30+
## Props
31+
32+
### Radio.Group
33+
34+
<TypeTable of={Radio.GroupProps} />
35+
36+
### Radio.Item
37+
38+
<TypeTable of={Radio.ItemProps} />
3439

35-
Radio buttons allow users to choose a single value from a range of at least two options.
40+
## Variants
41+
42+
### Basic
3643

3744
<Canvas of={RadioStories.Base} />
3845

39-
## Props
46+
### With initial value
47+
48+
By default, no radio item is selected. You can set an initial value using the `defaultValue` prop. This behaviour can be
49+
useful if you require a value to be submitted.
50+
51+
<Canvas of={RadioStories.DefaultValue} />
52+
53+
### Controlled
54+
55+
<Canvas of={RadioStories.Controlled} />
56+
57+
### Disabled
58+
59+
The below story shows passing `disabled` to the `Radio.Group` component. This will disable all radio items within.
60+
However, it is possible to disable individual items by passing `disabled` to said `Radio.Item`.
61+
62+
<Canvas of={RadioStories.Disabled} />
63+
64+
### With Label
4065

41-
### Radio
66+
The `Radio.Group` component requires a label of some description. This can either be via the `aria-label` attribute or
67+
alternatively if you want to show the label you can use `aria-labelledby` & share the `id` with the respective label
68+
Element.
4269

43-
<TypeTable of={Radio} />
70+
<Canvas of={RadioStories.WithLabel} />
4471

45-
### RadioGroup
72+
## Accessibility
4673

47-
<TypeTable of={RadioGroup} />
74+
Adheres to the [Radio Group WAI-ARIA design pattern](https://www.w3.org/WAI/ARIA/apg/patterns/radio/) and uses
75+
[roving tabindex](https://www.w3.org/WAI/ARIA/apg/patterns/radio/examples/radio/) to manage focus movement among radio
76+
items.
Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,82 @@
1-
import * as React from 'react';
1+
import { useId } from 'react';
22

33
import { Meta, StoryObj } from '@storybook/react';
4-
import { Radio, RadioGroup, Typography } from '@strapi/design-system';
4+
import { fn } from '@storybook/test';
5+
import { Radio, Typography } from '@strapi/design-system';
56

6-
const meta: Meta<typeof Radio> = {
7+
type RadioArgs = Radio.GroupProps;
8+
9+
const meta: Meta<RadioArgs> = {
710
title: 'Inputs/Radio',
8-
component: Radio,
11+
component: Radio.Group,
12+
argTypes: {
13+
defaultValue: {
14+
control: 'select',
15+
options: ['system', 'light', 'dark'],
16+
},
17+
},
18+
args: {
19+
defaultValue: '',
20+
disabled: false,
21+
loop: true,
22+
name: 'theme',
23+
required: false,
24+
onValueChange: fn(),
25+
},
26+
render: (args) => {
27+
return (
28+
<Radio.Group {...args} aria-label="Theme">
29+
<Radio.Item value="system">System</Radio.Item>
30+
<Radio.Item value="light">Light</Radio.Item>
31+
<Radio.Item value="dark">Dark</Radio.Item>
32+
</Radio.Group>
33+
);
34+
},
935
};
1036

1137
export default meta;
1238

13-
type Story = StoryObj<typeof Radio>;
39+
type Story = StoryObj<RadioArgs>;
1440

1541
export const Base = {
16-
render: () => {
17-
const [selected, setSelected] = React.useState<string>();
42+
name: 'base',
43+
} satisfies Story;
44+
45+
export const DefaultValue = {
46+
args: {
47+
defaultValue: 'system',
48+
},
49+
name: 'default value',
50+
} satisfies Story;
51+
52+
export const Controlled = {
53+
args: {
54+
value: 'dark',
55+
},
56+
name: 'controlled',
57+
} satisfies Story;
58+
59+
export const Disabled = {
60+
args: {
61+
disabled: true,
62+
},
63+
name: 'disabled',
64+
} satisfies Story;
65+
66+
export const WithLabel = {
67+
render: (args) => {
68+
const id = useId();
1869

1970
return (
20-
<div>
21-
<Typography variant="beta" id="trophy-champions">
22-
Make a choice
71+
<Radio.Group aria-labelledby={id} {...args}>
72+
<Typography tag="label" id={id} variant="pi" textColor="neutral800" fontWeight="bold">
73+
Select Theme
2374
</Typography>
24-
<RadioGroup
25-
labelledBy="trophy-champions"
26-
onChange={(e) => setSelected(e.target.value)}
27-
value={selected}
28-
name="meal"
29-
>
30-
<Radio value="pizza">Pizza</Radio>
31-
<Radio value="bagel">Bagel</Radio>
32-
</RadioGroup>
33-
</div>
75+
<Radio.Item value="system">System</Radio.Item>
76+
<Radio.Item value="light">Light</Radio.Item>
77+
<Radio.Item value="dark">Dark</Radio.Item>
78+
</Radio.Group>
3479
);
3580
},
36-
37-
name: 'base',
81+
name: 'with label',
3882
} satisfies Story;

docs/stories/BaseRadio.mdx

Lines changed: 0 additions & 30 deletions
This file was deleted.

docs/stories/BaseRadio.stories.tsx

Lines changed: 0 additions & 79 deletions
This file was deleted.

packages/design-system/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@radix-ui/react-dropdown-menu": "2.0.6",
2222
"@radix-ui/react-focus-scope": "1.0.4",
2323
"@radix-ui/react-popover": "1.0.7",
24+
"@radix-ui/react-radio-group": "1.1.3",
2425
"@radix-ui/react-scroll-area": "1.0.5",
2526
"@radix-ui/react-tabs": "1.0.4",
2627
"@radix-ui/react-tooltip": "1.0.7",

0 commit comments

Comments
 (0)