import type { Meta, StoryObj } from '@storybook/react-webpack5'; import { fn } from 'storybook/test'; import { useState } from 'react'; import Chips, { type ChipValue, type ChipsProps } from '..'; import Container from '../../Container'; import { storySourceWithoutNoise } from '../../../.storybook/helpers'; import { createSandboxStory, globalScope, } from '../../../.storybook/components/sandbox/SandboxEditor'; /** * Can be used for making a choice, or as a filter with multiple options. * * **Design guidance**: wise.design/components/chip */ export default { component: Chips, title: 'Actions/Chips', args: { multiple: false, onChange: fn(), 'aria-label': 'Category filter', }, argTypes: { 'aria-label': { control: 'text', description: 'Provides the accessible name for the chip group.', }, className: { table: { category: 'Common' } }, }, parameters: { docs: { toc: true }, }, } satisfies Meta; type Story = StoryObj; const categoryChips = [ { value: 'all', label: 'All' }, { value: 'accounting', label: 'Accounting' }, { value: 'payroll', label: 'Payroll' }, { value: 'reporting', label: 'Reporting' }, { value: 'payments', label: 'Payments' }, ]; const amountChips = [ { value: 100, label: '100 GBP' }, { value: 200, label: '200 GBP' }, { value: 300, label: '300 GBP' }, { value: 500, label: '500 GBP+' }, ]; /** * Toggle `multiple` in the controls panel to switch between filter (multi-select) * and choice (single-select) modes. */ export const Playground: Story = storySourceWithoutNoise({ args: { chips: categoryChips, multiple: true, }, render: function Render(args: ChipsProps) { const [selectedMulti, setSelectedMulti] = useState(['accounting']); const [selectedSingle, setSelectedSingle] = useState('accounting'); if (args.multiple) { return ( { setSelectedMulti((prev) => isEnabled ? [...prev, selectedValue] : prev.filter((v) => v !== selectedValue), ); args.onChange({ selectedValue, isEnabled }); }} /> ); } return ( { setSelectedSingle(selectedValue); args.onChange({ selectedValue, isEnabled }); }} /> ); }, }); export const Sandbox = createSandboxStory({ code: `const App = () => { const [selected, setSelected] = React.useState('accounting'); return ( setSelected(selectedValue)} aria-label="Category filter" /> ); };`, scope: globalScope, }); /** * The user can select any number of chips with the selected prop as an array (`multiple` prop set to `true`).
* Design documentation */ export const Filter: Story = storySourceWithoutNoise({ args: { chips: categoryChips, }, argTypes: { multiple: { table: { disable: true } }, }, render: function Render(args: ChipsProps) { const [selected, setSelected] = useState(['accounting', 'payments']); return ( { setSelected((prev) => isEnabled ? [...prev, selectedValue] : prev.filter((v) => v !== selectedValue), ); args.onChange({ selectedValue, isEnabled }); }} /> ); }, }); /** * The user can only select one chip with the selected prop as a single value (`multiple` prop set to `false`).
* Design documentation */ export const Choice: Story = storySourceWithoutNoise({ args: { chips: amountChips, }, argTypes: { multiple: { table: { disable: true } }, }, render: function Render(args: ChipsProps) { const [selected, setSelected] = useState(300); return ( { setSelected(selectedValue); args.onChange({ selectedValue, isEnabled }); }} /> ); }, }); const interestChips = [ { value: 'send', label: 'Send money internationally' }, { value: 'hold', label: 'Hold multiple currencies' }, { value: 'salary', label: 'Receive salary' }, { value: 'card', label: 'Get a travel debit card' }, { value: 'spend', label: 'Spend locally' }, { value: 'exchange', label: 'Exchange currencies' }, { value: 'receive', label: 'Receive money internationally' }, { value: 'large', label: 'Send £50,000 or more' }, { value: 'save', label: 'Set money aside' }, { value: 'invest', label: 'Earn a return' }, { value: 'split', label: 'Split expenses' }, ]; /** * Chips wrap onto new lines when they don't fit the width of their parent, such as a `Container`. */ export const MultipleRows: Story = storySourceWithoutNoise({ name: 'Multiple rows', args: { chips: interestChips, }, argTypes: { multiple: { table: { disable: true } }, }, render: function Render(args: ChipsProps) { const [selected, setSelected] = useState(['salary']); return ( { setSelected((prev) => isEnabled ? [...prev, selectedValue] : prev.filter((v) => v !== selectedValue), ); args.onChange({ selectedValue, isEnabled }); }} /> ); }, });