Skip to content

Commit 00ff629

Browse files
Add radios stories to show controlled checked change via state
1 parent ca7dd7e commit 00ff629

1 file changed

Lines changed: 132 additions & 1 deletion

File tree

‎stories/Form Elements/Radios.stories.tsx‎

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type Meta, type StoryObj } from '@storybook/react-vite';
2-
import { type ChangeEvent, useState } from 'react';
2+
import { type ChangeEvent, Fragment, useState } from 'react';
33

44
import { SummaryList } from '#components/content-presentation/summary-list/index.js';
55
import { Radios } from '#components/form-elements/radios/index.js';
@@ -279,6 +279,47 @@ export const WithValues: Story = {
279279
),
280280
};
281281

282+
export const WithValueStateControlled: Story = {
283+
name: 'Radios with pre-checked value state, controlled',
284+
parameters: {
285+
chromatic: {
286+
disableSnapshot: true,
287+
},
288+
},
289+
render: function WithValueStateControlledRender(args) {
290+
const [value, setValue] = useState<string | undefined>('email');
291+
292+
const handleChange = ({ target }: ChangeEvent<HTMLInputElement>) => {
293+
setValue(target.value);
294+
};
295+
296+
return (
297+
<>
298+
<Radios {...args}>
299+
<Radios.Item value="email" checked={value === 'email'} onChange={handleChange}>
300+
Email
301+
</Radios.Item>
302+
<Radios.Item value="phone" checked={value === 'phone'} onChange={handleChange}>
303+
Phone
304+
</Radios.Item>
305+
<Radios.Item value="text" checked={value === 'text'} onChange={handleChange}>
306+
Text message
307+
</Radios.Item>
308+
</Radios>
309+
310+
<SummaryList>
311+
<SummaryList.Row>
312+
<SummaryList.Key>Value</SummaryList.Key>
313+
<SummaryList.Value>
314+
<samp>{value ?? 'None'}</samp>
315+
</SummaryList.Value>
316+
</SummaryList.Row>
317+
</SummaryList>
318+
</>
319+
);
320+
},
321+
};
322+
282323
export const WithConditionalContent: Story = {
283324
name: 'Radios with conditional content',
284325
args: {
@@ -324,6 +365,96 @@ export const WithConditionalContentValues: Story = {
324365
),
325366
};
326367

368+
export const WithConditionalContentValuesStateControlled: Story = {
369+
name: 'Radios with conditional content and pre-checked value state, controlled',
370+
parameters: {
371+
chromatic: {
372+
disableSnapshot: true,
373+
},
374+
},
375+
render: function WithConditionalContentValuesStateControlledRender(args) {
376+
const [value, setValue] = useState<string | undefined>('email');
377+
const [inputs, setInputs] = useState<Map<string, string>>(
378+
new Map([['contact-by-email', 'karen.francis@example.com']]),
379+
);
380+
381+
const handleChange = ({ target }: ChangeEvent<HTMLInputElement>) => {
382+
setValue(target.value);
383+
};
384+
385+
const handleInput = ({ target }: ChangeEvent<HTMLInputElement>) => {
386+
const updated = new Map(inputs);
387+
388+
updated.set(target.name, target.value);
389+
setInputs(updated);
390+
};
391+
392+
return (
393+
<>
394+
<Radios {...args}>
395+
<Radios.Item
396+
value="email"
397+
conditional={
398+
<ExampleEmail value={inputs?.get('contact-by-email')} onChange={handleInput} />
399+
}
400+
checked={value === 'email'}
401+
onChange={handleChange}
402+
>
403+
Email
404+
</Radios.Item>
405+
<Radios.Item
406+
value="phone"
407+
conditional={
408+
<ExamplePhoneNumber value={inputs?.get('contact-by-phone')} onChange={handleInput} />
409+
}
410+
checked={value === 'phone'}
411+
onChange={handleChange}
412+
>
413+
Phone
414+
</Radios.Item>
415+
<Radios.Item
416+
value="text"
417+
conditional={
418+
<ExampleMobilePhoneNumber
419+
value={inputs?.get('contact-by-text')}
420+
onChange={handleInput}
421+
/>
422+
}
423+
checked={value === 'text'}
424+
onChange={handleChange}
425+
>
426+
Text message
427+
</Radios.Item>
428+
</Radios>
429+
430+
<SummaryList>
431+
<SummaryList.Row>
432+
<SummaryList.Key>Value</SummaryList.Key>
433+
<SummaryList.Value>
434+
<samp>{value ?? 'None'}</samp>
435+
</SummaryList.Value>
436+
</SummaryList.Row>
437+
<SummaryList.Row>
438+
<SummaryList.Key>Inputs</SummaryList.Key>
439+
<SummaryList.Value>
440+
{inputs.size ? (
441+
[...inputs.entries()].map(([key, value]) => (
442+
<Fragment key={key}>
443+
<samp>{value}</samp>
444+
<br />
445+
</Fragment>
446+
))
447+
) : (
448+
<samp>None</samp>
449+
)}
450+
</SummaryList.Value>
451+
</SummaryList.Row>
452+
</SummaryList>
453+
</>
454+
);
455+
},
456+
};
457+
327458
export const WithConditionalContentError: Story = {
328459
name: 'Radios with conditional content, error message',
329460
args: {

0 commit comments

Comments
 (0)