Skip to content

Commit 32c7422

Browse files
ThunearmimarzBarsnes
authored
docs(radio): web-first (#4868)
Co-authored-by: Michael Marszalek <mimarz@gmail.com> Co-authored-by: Tobias Barsnes <tobias.barsnes@digdir.no> Co-authored-by: Tobias Barsnes <tobiasbarsnes@gmail.com>
1 parent dec7c78 commit 32c7422

9 files changed

Lines changed: 168 additions & 202 deletions

File tree

apps/www/app/content/components/fieldset/en/code.mdx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A good rule of thumb is that **fieldset is a set of fields**
44
<Story story="PreviewEn" defaultOpen />
55

66
## Usage
7-
The class name `ds-fieldset` is added to the `<fieldset>` element.
7+
Use `ds-fieldset` on the `<fieldset>` element.
88

99
```html
1010
<fieldset class="ds-fieldset">
@@ -21,12 +21,20 @@ Build the group with [checkbox](/en/components/docs/checkbox/code) or [radio](/e
2121

2222
<Story story="WithCheckboxEn" />
2323

24-
### More fields
24+
### Multiple field types
2525

2626
A fieldset can also group other fields, for example [field](/en/components/docs/field/code) with [input](/en/components/docs/input/code) and [select](/en/components/docs/select/code).
2727

2828
<Story story="WithFieldsEn" />
2929

30+
### Readonly
31+
32+
Fields with the `readonly` attribute are included in the tab order. Users can copy the content but not edit it. Information is included when the form is submitted.
33+
34+
`Readonly` fields can be confusing for some users. Not everyone will understand why they cannot change the content of the field. We therefore recommend avoiding `readonly` as much as possible.
35+
36+
<Story story="ReadOnlyEn" />
37+
3038

3139
## CSS variables and data attributes
3240

apps/www/app/content/components/fieldset/fieldset.stories.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,33 @@ export const WithFieldsEn = () => {
119119
</Fieldset>
120120
);
121121
};
122+
123+
export const ReadOnly = () => {
124+
return (
125+
<Fieldset>
126+
<Fieldset.Legend>Hvilken bydel bor du i?</Fieldset.Legend>
127+
<Fieldset.Description>
128+
Trondheim er delt inn i fire bydeler
129+
</Fieldset.Description>
130+
<Radio label='Østbyen' value='ostbyen' name='city' readOnly />
131+
<Radio label='Lerkendal' value='lerkendal' name='city' readOnly />
132+
<Radio label='Heimdal' value='heimdal' name='city' readOnly checked />
133+
<Radio label='Midtbyen' value='midtbyen' name='city' readOnly />
134+
</Fieldset>
135+
);
136+
};
137+
138+
export const ReadOnlyEn = () => {
139+
return (
140+
<Fieldset>
141+
<Fieldset.Legend>Which district do you live in?</Fieldset.Legend>
142+
<Fieldset.Description>
143+
Trondheim is divided into four districts
144+
</Fieldset.Description>
145+
<Radio label='Østbyen' value='ostbyen' name='city' readOnly />
146+
<Radio label='Lerkendal' value='lerkendal' name='city' readOnly />
147+
<Radio label='Heimdal' value='heimdal' name='city' readOnly checked />
148+
<Radio label='Midtbyen' value='midtbyen' name='city' readOnly />
149+
</Fieldset>
150+
);
151+
};

apps/www/app/content/components/fieldset/no/code.mdx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ En god huskeregel er at **fieldset er et sett med fields**
44
<Story story="Preview" defaultOpen />
55

66
## Bruk
7-
Klassenavnet `ds-fieldset` legges`<fieldset>`-elementet.
7+
Bruk `ds-fieldset``<fieldset>`-elementet.
88

99
```html
1010
<fieldset class="ds-fieldset">
@@ -21,12 +21,20 @@ Bygg opp gruppen med [checkbox](/no/components/docs/checkbox/code) eller [radio]
2121

2222
<Story story="WithCheckbox" />
2323

24-
### Flere felter
24+
### Ulike felt typer
2525

2626
Et fieldset kan også gruppere andre felter, for eksempel [field](/no/components/docs/field/code) med [input](/no/components/docs/input/code) og [select](/no/components/docs/select/code).
2727

2828
<Story story="WithFields" />
2929

30+
### Readonly
31+
32+
Felter med `readonly`-attributtet er med i tabrekkefølgen. Brukerne kan kopiere innholdet men ikke redigere det. Informasjon blir med når skjemaet sendes inn.
33+
34+
`readonly`-felter kan være forvirrende for noen brukere. Ikke alle vil skjønne hvorfor de ikke får til å endre innholdet i feltet. Vi anbefaler derfor å unngå `readonly` så langt det lar seg gjøre.
35+
36+
<Story story="ReadOnly" />
37+
3038

3139
## CSS variabler og data-attributter
3240

Lines changed: 49 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,80 @@
1-
<Story story="Preview" />
2-
<ReactComponentDocs />
1+
<Story story="Preview" defaultOpen />
32

43
## Usage
54

6-
```tsx
7-
import { Radio } from '@digdir/designsystemet-react';
5+
Use the class `ds-input` on `<input>` together with `type="radio"`.
86

9-
<Radio label="Radio" value="value" name="name" />
7+
```html
8+
<input
9+
class="ds-input"
10+
type="radio"
11+
/>
12+
```
13+
14+
For a valid form element, we recommend using `<ds-field>` together with a `<label>`.
15+
16+
```html
17+
<ds-field>
18+
<label class="ds-label">Radio</label>
19+
<input
20+
class="ds-input"
21+
type="radio"
22+
value="value"
23+
name="name"
24+
>
25+
</ds-field>
1026
```
27+
## CSS variables and data attributes
28+
Sizes are controlled with [data-size](/en/fundamentals/code/sizes) and colors with [data-color](/en/fundamentals/code/colors).
29+
The component will inherit from the closest parent where these are set.
30+
31+
Radio is `ds-input` with `type="radio"`, so variables and data attributes that apply to [input](/en/components/docs/input/overview) also apply to radio.
32+
33+
<CssVariables />
34+
35+
<CssAttributes />
1136

1237
## Examples
1338

1439
### Grouping
15-
Use [`Fieldset`](/en/components/docs/fieldset/code) around a group of radio buttons to provide a common prompt (`<legend>`) and optionally a description (`description`).
40+
41+
Use [fieldset](/en/components/docs/fieldset/code) for grouping multiple choices.
1642

1743
<Story story="GroupEn" />
1844

1945
### Outline
20-
`variant="outline"` adds extra padding and a border around the selection, increasing the clickable area.
21-
46+
`data-variant="outline"` adds extra padding and a border around the option to create a larger clickable area.
2247
<Story story="OutlineEn" />
2348

24-
### With Error
25-
Here we must use [`Fieldset`](/en/components/docs/fieldset/code), because it activates the correct style and ensures that the content has the correct connections for accessibility.
49+
### Inline
50+
Radio *can* be positioned horizontally with `flex`, but should generally be [placed vertically](/en/components/docs/radio/overview#placement).
51+
<Story story="InlineEn" />
2652

27-
<Story story="WithErrorEn" />
53+
### ReadOnly
54+
`type="radio"` is always `:read-only` and does not support this in the same way as input types you can type into.
2855

29-
### Readonly
56+
If you set `readonly` on radio, we ensure that it behaves as if it has read-only access by stopping `onClick` and `onChange`.
3057

31-
Fields with the `readonly` attribute are included in the tab order. Users can copy the content but not edit it. Information is included when the form is submitted.
58+
The user will still be able to focus on the element.
3259

33-
`Readonly` fields can be confusing for some users. Not everyone will understand why they cannot change the content of the field. We therefore recommend avoiding `readonly` as much as possible.
60+
<Story story="ReadOnly" />
3461

35-
<Story story="ReadOnlyEn" />
3662

37-
### Inline
38-
Radio *can* be positioned horizontally with `flex`, but should generally be [placed vertically](/en/components/docs/radio/overview#placement).
39-
<Story story="InlineEn" />
4063

41-
## HTML
4264

43-
`Radio` is a composite component, so in HTML you have to build it from multiple components.
44-
We use [`Fieldset`](/en/components/docs/fieldset/code), [`Label`](/en/components/docs/label/code) and [`Input`](/en/components/docs/input/code) to create a `Radio`.
4565

46-
The code below shows how you can visually build a `Radio` in HTML, but you have to make sure that `<input>` is connected with `<label>` and description.
66+
## React
4767

48-
```html
49-
<div class="ds-field">
50-
<input
51-
class="ds-input"
52-
type="radio"
53-
value="value"
54-
name="name"
55-
id="my-radio"
56-
>
57-
<label
58-
class="ds-label"
59-
data-weight="regular"
60-
for="my-radio"
61-
>Radio</label>
62-
</div>
68+
In React, building a radio is easier, because we have a ready-made component that handles most of the logic and state management for us.
6369

64-
```
70+
`Radio` is a composite component that uses [field](/en/components/docs/field/overview), [label](/en/components/docs/label/overview), [validation message](/en/components/docs/validation-message/overview), and [input](/en/components/docs/input/overview) to create a radio.
6571

66-
### In Fieldset
72+
In addition, we have a dedicated [useRadioGroup](/en/components/utilities/use-radio-group) React hook to make it easier to manage a group of Radio components.
6773

68-
The code below is an example of how to use multiple `Radio` inside `<fieldset>`.
74+
```tsx
75+
import { Radio } from '@digdir/designsystemet-react';
6976

70-
```html
71-
<fieldset class="ds-fieldset">
72-
<legend class="ds-label" data-weight="medium">Can we contact you by email?</legend>
73-
<p class="ds-paragraph" data-variant="default">If you do not wish to be contacted by email, we will send you a letter by post.</p>
74-
<div class="ds-field">
75-
<input
76-
class="ds-input"
77-
type="radio"
78-
value="yes"
79-
id="radio-input"
80-
>
81-
<label
82-
class="ds-label"
83-
data-weight="regular"
84-
for="radio-input"
85-
>Yes</label>
86-
</div>
87-
<div class="ds-field">
88-
<input
89-
class="ds-input"
90-
type="radio"
91-
value="no"
92-
id="radio-input"
93-
>
94-
<label
95-
class="ds-label"
96-
data-weight="regular"
97-
for="radio-input"
98-
>No</label>
99-
</div>
100-
</fieldset>
77+
<Radio label="Radio" value="value" name="name" />
10178
```
10279

103-
## CSS variables and data attributes
104-
Checkbox is a composite component, and therefore we show variables and data attributes from `ds-input` here.
105-
<CssVariables />
106-
107-
<CssAttributes />
80+
<ReactComponentDocs />

apps/www/app/content/components/radio/en/overview.mdx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,34 @@ search_terms: radiobutton, option, options, selection, single
44

55
<Story story="Preview" />
66

7-
**Use `Radio` when**
7+
**Use radio when**
88
- the user must choose one of several options in a form
99

10-
**Avoid `Radio` when**
11-
- the user should be able to choose more than one option, use [`Checkbox`](/en/components/docs/checkbox/overview) instead
10+
**Avoid radio when**
11+
- the user should be able to choose more than one option, use [checkbox](/en/components/docs/checkbox/overview) instead
1212

1313
## Example
1414

1515
### Grouping
16-
1716
Use a legend to ask the question, and include a description if additional explanation is needed.
1817

1918
<Story story="GroupEn" />
2019

2120
### Outline
22-
`variant="outline"` adds extra padding and a border around the selection, increasing the clickable area.
21+
There is a variant of radio called `outline`. It adds extra padding and a border around the selection, increasing the clickable area.
2322

2423
<Story story="OutlineEn" />
2524

2625
### With error
27-
2826
If the user attempts to continue without answering a required question, the error message should apply to the entire group of options.
2927

3028
<Story story="WithErrorEn" />
3129

3230
## Guidelines
3331

34-
Use `Radio` when users must select only one option. If they need to select more, use [`Checkbox`](/en/components/docs/checkbox/overview).
32+
Use radio when users must select only one option. If they need to select more, use [checkbox](/en/components/docs/checkbox/overview).
3533

36-
There should not be more than seven options in one group. If users need more choices, consider using [`Suggestion`](/en/components/docs/suggestion/overview) or [`Select`](/en/components/docs/select/overview). If there is only one choice to make, a [`Switch`](/en/components/docs/switch/overview) or a [`Checkbox`](/en/components/docs/checkbox/overview) may be more suitable.
34+
There should not be more than seven options in one group. If users need more choices, consider using [suggestion](/en/components/docs/suggestion/overview) or [select](/en/components/docs/select/overview). If there is only one choice to make, a [switch](/en/components/docs/switch/overview) or a [checkbox](/en/components/docs/checkbox/overview) may be more suitable.
3735

3836
### Sorting
3937

apps/www/app/content/components/radio/metadata.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"no": {
33
"title": "Radio",
4-
"subtitle": "`Radio` er et alternativ brukeren kan velge. Bruk flere `Radio` for å vise en liste med alternativer. Brukerne kan bytte mellom alternativene, men kan kun velge ett."
4+
"subtitle": "Radio er et alternativ brukeren kan velge. Bruk flere radio for å vise en liste med alternativer. Brukerne kan bytte mellom alternativene, men kan kun velge ett."
55
},
66
"en": {
77
"title": "Radio",
8-
"subtitle": "A `Radio` is an option the user can select. Use multiple `Radio` to show a list of options. Users can switch between the options, but can only select one."
8+
"subtitle": "A Radio is an option the user can select. Use multiple radio to show a list of options. Users can switch between the options, but can only select one."
99
},
1010
"image": "Radio.svg",
1111
"cssFile": "input.css"

0 commit comments

Comments
 (0)