import { useState, useEffect } from 'react'; import { fn } from 'storybook/test'; import { storySourceWithoutNoise } from '../../.storybook/helpers'; import Switch, { SwitchProps } from './Switch'; import { Field } from '../field/Field'; import { Meta, StoryObj } from '@storybook/react-webpack5'; import { Label } from '../label'; /** * **Design guidance**: wise.design/components/switch */ const meta: Meta = { component: Switch, title: 'Actions/Switch', args: { checked: false, disabled: false, id: 'switchId', className: 'switchClassName', 'aria-labelledby': undefined, 'aria-label': 'Toggle switch', onClick: fn(), }, argTypes: { id: { description: '', table: { category: 'Common' } }, className: { description: '', table: { category: 'Common' } }, onClick: { description: '', table: { category: 'Common', type: { summary: 'function' } } }, }, parameters: { docs: { toc: true }, }, } satisfies Meta; export default meta; type Story = StoryObj; export const Playground: Story = storySourceWithoutNoise({ render: function Render(args: SwitchProps) { const [checked, setChecked] = useState(args.checked ?? false); useEffect(() => { setChecked(args.checked ?? false); }, [args.checked]); return ( setChecked((value: boolean) => !value)} /> ); }, }); /** * Three labelling patterns: Label, `aria-labelledby`, and Field.
* Use Label for simple label-input pairs. Use `aria-labelledby` to reference existing text as the accessible name. Use Field for form layouts with labels, descriptions, and validation. */ export const LabelPatterns: Story = { render: function Render({ disabled }: SwitchProps) { const [checked1, setCheck1] = useState(true); const [checked2, setCheck2] = useState(false); const [checked3, setCheck3] = useState(true); return (
setCheck1(!checked1)} />
Marketing emails setCheck2(!checked2)} />
setCheck3(!checked3)} />
); }, parameters: { docs: { source: { code: ` function Render() { const [checked1, setCheck1] = useState(false); const [checked2, setCheck2] = useState(true); const [checked3, setCheck3] = useState(false); return ( <> setCheck1(!checked1)} />
setCheck2(!checked2)} />
Using \`aria-labelledby\` setCheck3(!setCheck3)} />
) }`, }, }, }, args: {}, argTypes: { checked: { table: { disable: true } }, onClick: { table: { disable: true } }, }, }; export const Disabled: Story = { render: function Render() { const [checked1, setCheck1] = useState(false); const [checked2, setCheck2] = useState(true); return ( <> setCheck1(!checked1)} /> setCheck2(!checked2)} /> ); }, parameters: { docs: { source: { code: ` function Render() { const [checked1, setCheck1] = useState(false); const [checked2, setCheck2] = useState(true); return ( <> setCheck1(!checked1)} /> setCheck2(!checked2)} /> ); }`, }, }, }, args: { disabled: true, }, argTypes: { disabled: { table: { readonly: true } }, onClick: { table: { disable: true } }, checked: { table: { disable: true } }, }, };