import type { Meta, StoryObj } from '@storybook/react-webpack5';
import { fn } from 'storybook/test';
import { useState } from 'react';
import Chips, { type ChipValue, type ChipsProps } from './Chips';
import { storySourceWithoutNoise } from '../../.storybook/helpers';
/**
* 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 });
}}
/>
);
},
});
/**
* 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 });
}}
/>
);
},
});