|
| 1 | +import type { Meta, StoryObj } from '@storybook/react-vite'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import { ConditionalField } from './ConditionalField'; |
| 4 | +import { Checkbox } from '../Checkbox'; |
| 5 | +import { Select } from '../Select'; |
| 6 | +import { Input } from '../Input'; |
| 7 | +import { FormGroup } from '../FormGroup'; |
| 8 | + |
| 9 | +const meta: Meta<typeof ConditionalField> = { |
| 10 | + title: 'Form/ConditionalField', |
| 11 | + component: ConditionalField, |
| 12 | + tags: ['autodocs'], |
| 13 | + parameters: { layout: 'padded' }, |
| 14 | +}; |
| 15 | + |
| 16 | +export default meta; |
| 17 | +type Story = StoryObj<typeof ConditionalField>; |
| 18 | + |
| 19 | +/** Show an extra field when a checkbox is checked. */ |
| 20 | +export const CheckboxDriven: Story = { |
| 21 | + render: function Example() { |
| 22 | + const [hasAlias, setHasAlias] = useState(false); |
| 23 | + return ( |
| 24 | + <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}> |
| 25 | + <Checkbox |
| 26 | + label="I go by a different name" |
| 27 | + checked={hasAlias} |
| 28 | + onChange={(e) => setHasAlias(e.target.checked)} |
| 29 | + /> |
| 30 | + <ConditionalField show={hasAlias}> |
| 31 | + <Input label="Preferred name" placeholder="e.g. Alex" /> |
| 32 | + </ConditionalField> |
| 33 | + </div> |
| 34 | + ); |
| 35 | + }, |
| 36 | +}; |
| 37 | + |
| 38 | +/** Show a group of fields based on a select value. */ |
| 39 | +export const SelectDriven: Story = { |
| 40 | + render: function Example() { |
| 41 | + const [deliveryType, setDeliveryType] = useState(''); |
| 42 | + return ( |
| 43 | + <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}> |
| 44 | + <Select |
| 45 | + label="Delivery method" |
| 46 | + placeholder="Choose…" |
| 47 | + options={[ |
| 48 | + { value: 'pickup', label: 'Store pickup' }, |
| 49 | + { value: 'delivery', label: 'Home delivery' }, |
| 50 | + ]} |
| 51 | + value={deliveryType} |
| 52 | + onChange={(e) => setDeliveryType(e.target.value)} |
| 53 | + /> |
| 54 | + <ConditionalField show={deliveryType === 'delivery'}> |
| 55 | + <FormGroup title="Delivery Address"> |
| 56 | + <Input label="Street" /> |
| 57 | + <Input label="City" /> |
| 58 | + <Input label="Postal code" /> |
| 59 | + </FormGroup> |
| 60 | + </ConditionalField> |
| 61 | + </div> |
| 62 | + ); |
| 63 | + }, |
| 64 | +}; |
| 65 | + |
| 66 | +/** |
| 67 | + * With `keepMounted`, the hidden content stays in the DOM (aria-hidden). |
| 68 | + * Useful when you need to preserve controlled state across visibility toggles. |
| 69 | + */ |
| 70 | +export const KeepMounted: Story = { |
| 71 | + render: function Example() { |
| 72 | + const [show, setShow] = useState(false); |
| 73 | + return ( |
| 74 | + <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}> |
| 75 | + <Checkbox |
| 76 | + label="Show secondary contact (keepMounted)" |
| 77 | + checked={show} |
| 78 | + onChange={(e) => setShow(e.target.checked)} |
| 79 | + /> |
| 80 | + <ConditionalField show={show} keepMounted> |
| 81 | + <Input label="Secondary email" placeholder="backup@example.com" /> |
| 82 | + </ConditionalField> |
| 83 | + </div> |
| 84 | + ); |
| 85 | + }, |
| 86 | +}; |
0 commit comments