-
Notifications
You must be signed in to change notification settings - Fork 14
/
RadioGroupCodePage.tsx
281 lines (278 loc) · 8.73 KB
/
RadioGroupCodePage.tsx
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import {
DxcFlex,
DxcTable,
DxcParagraph,
} from "@dxc-technology/halstack-react";
import QuickNavContainerLayout from "@/common/QuickNavContainerLayout";
import QuickNavContainer from "@/common/QuickNavContainer";
import Code from "@/common/Code";
import DocFooter from "@/common/DocFooter";
import Example from "@/common/example/Example";
import controlled from "./examples/controlled";
import uncontrolled from "./examples/uncontrolled";
import errorHandling from "./examples/errorHandling";
import TableCode from "@/common/TableCode";
import StatusBadge from "@/common/StatusBadge";
const sections = [
{
title: "Props",
content: (
<DxcTable>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
<th>Default</th>
</tr>
</thead>
<tbody>
<tr>
<td>defaultValue</td>
<td>
<TableCode>string</TableCode>
</td>
<td>
Initial value of the radio group, only when it is uncontrolled.
</td>
<td>-</td>
</tr>
<tr>
<td>value</td>
<td>
<TableCode>string</TableCode>
</td>
<td>
Value of the radio group. If undefined, the component will be
uncontrolled and the value will be managed internally by the
component.
</td>
<td>-</td>
</tr>
<tr>
<td>label</td>
<td>
<TableCode>string</TableCode>
</td>
<td>Text to be placed above the radio group.</td>
<td>-</td>
</tr>
<tr>
<td>name</td>
<td>
<TableCode>string</TableCode>
</td>
<td>
Name attribute of the input element. This attribute will allow
users to find the component's value during the submit event.
</td>
<td>-</td>
</tr>
<tr>
<td>helperText</td>
<td>
<TableCode>string</TableCode>
</td>
<td>Helper text to be placed above the radio group.</td>
<td>-</td>
</tr>
<tr>
<td>
<DxcFlex direction="column" gap="0.25rem" alignItems="baseline">
<StatusBadge status="required" />
options
</DxcFlex>
</td>
<td>
<TableCode>
{"{ value: string; label: string; disabled?: boolean; }[]"}
</TableCode>
</td>
<td>
An array of objects representing the selectable options. Each
object Option has the following properties:
<ul>
<li>
<b>label</b>: Label of the option placed next to the radio
input.
</li>
<li>
<b>value</b>: Value of the option. It should be unique and not
an empty string, which is reserved to the optional item added
by the <Code>optional</Code> prop.
</li>
<li>
<b>disabled</b>: disables the option.
</li>
</ul>
</td>
<td>-</td>
</tr>
<tr>
<td>disabled</td>
<td>
<TableCode>boolean</TableCode>
</td>
<td>If true, the component will be disabled.</td>
<td>
<TableCode>false</TableCode>
</td>
</tr>
<tr>
<td>optional</td>
<td>
<TableCode>boolean</TableCode>
</td>
<td>
If true, the radio group will be optional, showing the text
'(Optional)' next to the label and adding a default last option
with an empty string as value. Otherwise, the field will be
considered required and an error will be passed as a parameter to
the <Code>onBlur</Code> function if an option wasn't selected.
</td>
<td>
<TableCode>false</TableCode>
</td>
</tr>
<tr>
<td>optionalItemLabel</td>
<td>
<TableCode>string</TableCode>
</td>
<td>Label of the optional radio input.</td>
<td>
<TableCode>'N/A'</TableCode>
</td>
</tr>
<tr>
<td>readOnly</td>
<td>
<TableCode>boolean</TableCode>
</td>
<td>
If true, the component will not be mutable, meaning the user can
not edit the control.
</td>
<td>
<TableCode>false</TableCode>
</td>
</tr>
<tr>
<td>stacking</td>
<td>
<TableCode>'row' | 'column'</TableCode>
</td>
<td>Sets the orientation of the options within the radio group.</td>
<td>
<TableCode>'column'</TableCode>
</td>
</tr>
<tr>
<td>onChange</td>
<td>
<TableCode>{"(value: string) => void"}</TableCode>
</td>
<td>
This function will be called when the user chooses an option. The
new value will be passed to this function.
</td>
<td>-</td>
</tr>
<tr>
<td>onBlur</td>
<td>
<TableCode>
{"(val: { value?: string; error?: string }) => void"}
</TableCode>
</td>
<td>
This function will be called when the radio group loses the focus.
An object including the value and the error will be passed to this
function. If there is no error, error will not be defined.
</td>
<td>-</td>
</tr>
<tr>
<td>error</td>
<td>
<TableCode>string</TableCode>
</td>
<td>
If it is a defined value and also a truthy string, the component
will change its appearance, showing the error below the radio
group. If the defined value is an empty string, it will reserve a
space below the component for a future error, but it would not
change its look. In case of being undefined or null, both the
appearance and the space for the error message would not be
modified.
</td>
<td>-</td>
</tr>
<tr>
<td>tabIndex</td>
<td>
<TableCode>number</TableCode>
</td>
<td>
Value of the <Code>tabindex</Code> attribute.
</td>
<td>
<TableCode>0</TableCode>
</td>
</tr>
<tr>
<td>ref</td>
<td>
<TableCode>{"React.Ref <HTMLDivElement>"}</TableCode>
</td>
<td>Reference to the component.</td>
<td>-</td>
</tr>
</tbody>
</DxcTable>
),
},
{
title: "Examples",
subSections: [
{
title: "Controlled",
content: <Example example={controlled} defaultIsVisible />,
},
{
title: "Uncontrolled",
content: <Example example={uncontrolled} defaultIsVisible />,
},
{
title: "Error handling",
content: (
<>
<DxcParagraph>
For handling errors, we suggest initializing the{" "}
<Code>error</Code> prop with an empty string. This will reserve
space for a possible future error message and prevent unintended
layout changes. Also, the <Code>onBlur</Code> event will send{" "}
<Code>undefined</Code> when there is no error, so you may also
need to control this too to avoid the same problem.
</DxcParagraph>
<Example example={errorHandling} defaultIsVisible />
</>
),
},
],
},
];
const RadioGroupCodePage = () => {
return (
<DxcFlex direction="column" gap="4rem">
<QuickNavContainerLayout>
<QuickNavContainer
sections={sections}
startHeadingLevel={2}
></QuickNavContainer>
</QuickNavContainerLayout>
<DocFooter githubLink="https://github.com/dxc-technology/halstack-react/blob/master/website/screens/components/radio-group/code/RadioGroupCodePage.tsx" />
</DxcFlex>
);
};
export default RadioGroupCodePage;