-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathCheckboxes.stories.tsx
More file actions
186 lines (176 loc) · 6.59 KB
/
Checkboxes.stories.tsx
File metadata and controls
186 lines (176 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import React from 'react';
import { Fieldset, Checkboxes, Input } from '../../src';
import { Button } from '../../src/components/button';
import { Meta, StoryObj } from '@storybook/react';
/**
* This component can be found in the `nhsuk-frontend` repository <a href="https://github.com/nhsuk/nhsuk-frontend/tree/master/packages/components/checkboxes" target="_blank" rel="noopener noreferrer">here</a>.
*
* ## Implementation Notes
*
* For details on the new form design pattern, check the documentation for the `Form` component.
*
* The `Checkbox` component provides a `CheckboxContext` context, which the `Checkbox.Box` components use. When each box initially renders, it will attempt to assign itself an `id` by calling the `getBoxId` method in the context. This will assign each box a sequential ID using either the `idPrefix` or `name` supplied to the Checkbox. If neither are provided, the element will generate it's own unique identifier.
*
* ## Usage
*
* ### Standard
*
* ```jsx
* import { Checkboxes } from "nhsuk-react-components";
*
* const Element = () => {
* return (
* <Checkboxes name="nationality" id="nationality">
* <Checkboxes.Box value="british">British</Checkboxes.Box>
* <Checkboxes.Box value="irish">Irish</Checkboxes.Box>
* <Checkboxes.Box value="other">Citizen of another country</Checkboxes.Box>
* </Checkboxes>
* );
* }
* ```
*/
const meta: Meta<typeof Checkboxes> = {
title: 'Components/Checkboxes',
component: Checkboxes,
};
export default meta;
type Story = StoryObj<typeof Checkboxes>;
Checkboxes.Box.displayName = 'Checkboxes.Box';
export const Standard: Story = {
render: (args) => (
<Fieldset aria-describedby="nationality--hint">
<Fieldset.Legend>What is your nationality?</Fieldset.Legend>
<Checkboxes
idPrefix={args.idPrefix}
name="nationality"
id="nationality"
hint="If you have more than 1 nationality, select all options that are relevant to you."
>
<Checkboxes.Box value="british">British</Checkboxes.Box>
<Checkboxes.Box value="irish">Irish</Checkboxes.Box>
<Checkboxes.Box value="other">Citizen of another country</Checkboxes.Box>
</Checkboxes>
</Fieldset>
),
};
export const WithHintText: Story = {
render: (args) => (
<Fieldset>
<Fieldset.Legend isPageHeading>How do you want to sign in?</Fieldset.Legend>
<Checkboxes id="sign-in">
<Checkboxes.Box
id="government-gateway"
name="gateway"
type="checkbox"
value="gov-gateway"
hint="You’ll have a user ID if you’ve registered for Self Assessment or filed a tax return online before."
>
Sign in with Government Gateway
</Checkboxes.Box>
<Checkboxes.Box
id="nhsuk-login"
name="verify"
value="nhsuk-verify"
hint="You’ll have an account if you’ve already proved your identity with either Barclays, CitizenSafe, Digidentity, Experian, Post Office, Royal Mail or SecureIdentity."
>
Sign in with NHS.UK login
</Checkboxes.Box>
</Checkboxes>
</Fieldset>
),
};
export const WithDisabledItem: Story = {
render: (args) => (
<Checkboxes id="colours" name="colours">
<Checkboxes.Box value="red">Red</Checkboxes.Box>
<Checkboxes.Box value="green">Green</Checkboxes.Box>
<Checkboxes.Box value="red" disabled>
Blue
</Checkboxes.Box>
</Checkboxes>
),
};
export const WithConditionalContent: Story = {
render: (args) => (
<Fieldset aria-describedby="waste--hint">
<Fieldset.Legend isPageHeading>
Which types of waste do you transport regularly?
</Fieldset.Legend>
<Checkboxes id="waste" name="waste" hint="Select all that apply">
<Checkboxes.Box conditional={<p>This includes rocks and earth.</p>} value="mines">
Waste from mines or quarries
</Checkboxes.Box>
</Checkboxes>
</Fieldset>
),
};
export const WithLegendAsPageHeading: Story = {
render: (args) => (
<Fieldset aria-describedby="waste--hint">
<Fieldset.Legend isPageHeading>
Which types of waste do you transport regularly?
</Fieldset.Legend>
<Checkboxes id="waste" name="waste" hint="Select all that apply">
<Checkboxes.Box value="animal">Waste from animal carcasses</Checkboxes.Box>
<Checkboxes.Box value="mines">Waste from mines or quarries</Checkboxes.Box>
<Checkboxes.Box value="farm">Farm or agricultural waste</Checkboxes.Box>
</Checkboxes>
</Fieldset>
),
};
export const WithErrorBoolean: Story = {
render: (args) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const [errorToggle, setErrorToggle] = React.useState(true);
return (
<>
<Fieldset aria-describedby="waste-hint">
<Fieldset.Legend isPageHeading>
Which types of waste do you transport regularly?
</Fieldset.Legend>
<Checkboxes error={errorToggle} id="waste" name="waste" hint="Select all that apply">
<Checkboxes.Box value="animal">Waste from animal carcasses</Checkboxes.Box>
<Checkboxes.Box value="mines">Waste from mines or quarries</Checkboxes.Box>
<Checkboxes.Box value="farm">Farm or agricultural waste</Checkboxes.Box>
</Checkboxes>
</Fieldset>
<Button
onClick={(e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
setErrorToggle(!errorToggle);
}}
>
Toggle Error
</Button>
</>
);
},
name: 'With Error (Boolean)',
};
export const WithErrorString: Story = {
render: (args) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const [error, setError] = React.useState('Please select an option');
return (
<>
<Fieldset aria-describedby="waste-hint">
<Fieldset.Legend isPageHeading>
Which types of waste do you transport regularly?
</Fieldset.Legend>
<Checkboxes id="waste" name="waste" hint="Select all that apply" error={error}>
<Checkboxes.Box value="animal">Waste from animal carcasses</Checkboxes.Box>
<Checkboxes.Box value="mines">Waste from mines or quarries</Checkboxes.Box>
<Checkboxes.Box value="farm">Farm or agricultural waste</Checkboxes.Box>
</Checkboxes>
</Fieldset>
<Input
label="Error Value"
value={error}
id="error-value"
onChange={(e) => setError(e.currentTarget.value)}
/>
</>
);
},
name: 'With Error (String)',
};