import type { Meta, StoryObj } from '@storybook/react'; import React, { useState } from 'react'; import { Avatar } from '../../Avatar'; import { ListBox } from '../ListBox'; import { Button } from '../../Button'; import { SnapshotContainer } from '../../../test-utils/SnapshotsContainer'; import styles from './ListBox.module.css'; import { Select } from '../../Select'; import { action } from 'storybook/actions'; type Data = { id: string; name: string; birthDate: string; avatar: string; description: string; }; const meta: Meta = { title: 'Data display/ListBox', component: ListBox, tags: ['legacy'], }; export default meta; type Story = StoryObj>; export const Primary: Story = { render: (props) => { const [activeOption, setActiveOption] = useState(); return ( option.id === activeOption} onOptionClick={(option) => { setActiveOption(option.id); }} aria-label="Employees" /> ); }, args: { options: [ { id: 'michael', name: 'Michael Murphy', birthDate: '1979-09-19', avatar: 'images/aurelien.webp', description: 'Growth team', }, { id: 'nayden', name: 'Nayden Lennart', birthDate: '1980-03-01', avatar: 'images/bertrand.webp', description: 'Design team', }, { id: 'nicolas', name: 'Nicolas Harvey', birthDate: '1980-06-05', avatar: 'images/mahedine.webp', description: 'Growth team', }, { id: 'lewis', name: 'Lewis Barker', birthDate: '1980-07-30', avatar: 'images/jean.webp', description: 'Design team', }, { id: 'george', name: 'George Gray', birthDate: '1980-07-31', avatar: 'images/laurent.webp', description: 'Design team', }, { id: 'laura', name: 'Laura Lagarde', birthDate: '1981-02-13', avatar: 'images/chloe.webp', description: 'Marketing team', }, ], getOptionId: (option) => option.id, children: (option, titleId) => { const dtf = new Intl.DateTimeFormat(); return (
{option.name} {dtf.format(new Date(option.birthDate))}
{option.description}
); }, }, }; export const EmptyList: Story = { ...Primary, args: { options: [], emptyState: { title: 'No employee found', }, getOptionId: (option) => option.id, children: (option) => { return
{option.description}
; }, }, }; export const WithRowVariant: Story = { ...Primary, args: { ...Primary.args, getOptionVariant: (option) => { switch (option.id) { case 'george': return 'alert'; case 'lewis': return 'warning'; default: return undefined; } }, }, }; export const GroupBy: Story = { ...Primary, args: { ...Primary.args, groupBy: (option) => { const date = new Date(option.birthDate); return `${date.getFullYear()}`; }, renderGroupedOptionsHeader: (value, aggregatedOptions) => { const aggregatedCount = aggregatedOptions.length; return (
{value} {aggregatedCount} {aggregatedCount > 1 ? 'members' : 'member'}
); }, }, }; export const GroupByWithHeaderAndFooter: Story = { ...Primary, args: { ...Primary.args, header:
Members
, footer: (
), groupBy: (option) => { const date = new Date(option.birthDate); return `${date.getFullYear()}`; }, renderGroupedOptionsHeader: (value, aggregatedOptions) => { const aggregatedCount = aggregatedOptions.length; return (
{value} {aggregatedCount} {aggregatedCount > 1 ? 'members' : 'member'}
); }, }, }; export const WithAFooter: Story = { ...Primary, args: { ...Primary.args, footer: (
), }, }; export const WithAHeader: Story = { ...Primary, args: { ...Primary.args, header:
Members
, }, }; export const Compact: Story = { ...Primary, args: { ...Primary.args, rowHeight: 'compact', children: (option, titleId) => { const dtf = new Intl.DateTimeFormat(); return (
{option.name} {dtf.format(new Date(option.birthDate))}
); }, }, }; export const WithoutClick: Story = { ...Primary, args: { ...Primary.args, }, render: (props) => , }; export const SelectOption: Story = { args: { ...Primary.args, checkedOptionIds: ['nayden', 'nicolas'], header:

Employee

, }, render: (props) => { const [optionsSelected, setOptionSelected] = useState( props.checkedOptionIds || [], ); const [activeOption, setActiveOption] = useState(); return ( { setOptionSelected((options) => { if (checked) { return options.concat(id); } return options.filter((optionId) => optionId !== id); }); }} getIsOptionActive={(option) => option.id === activeOption} onOptionClick={(option) => setActiveOption(option.id)} onAllOptionsChange={(_, ids, checked) => { setOptionSelected(checked ? ids : []); }} checkedOptionIds={optionsSelected} aria-label="employees" /> ); }, }; export const WithDisabledOption: Story = { ...SelectOption, args: { ...SelectOption.args, getIsOptionDisabled: (option) => option.id === 'laura', }, }; export const WithOnlyDisabledOptions: Story = { ...SelectOption, args: { ...SelectOption.args, checkedOptionIds: [], getIsOptionDisabled: () => true, }, }; export const WithNonCheckableOption: Story = { ...SelectOption, args: { ...SelectOption.args, getIsOptionCheckable: (option) => option.id !== 'laura', }, }; export const Snapshot: Story = { parameters: { chromatic: { disableSnapshot: false }, }, render: () => ( ), }; const costCenters = [ { key: 'marketing', label: 'Marketing' }, { key: 'legal', label: 'Legal' }, { key: 'office', label: 'Office' }, { key: 'platform', label: 'Platform' }, { key: 'finance', label: 'Finance' }, { key: 'product', label: 'Product' }, { key: 'engineering', label: 'Engineering' }, ]; export const WithDropdownInside: Story = { render: (props) => { return ; }, args: { options: [ { id: 'michael', name: 'Michael Murphy', birthDate: '1979-09-19', avatar: 'images/aurelien.webp', description: 'Growth team', }, { id: 'nayden', name: 'Nayden Lennart', birthDate: '1980-03-01', avatar: 'images/bertrand.webp', description: 'Design team', }, { id: 'nicolas', name: 'Nicolas Harvey', birthDate: '1980-06-05', avatar: 'images/mahedine.webp', description: 'Growth team', }, { id: 'lewis', name: 'Lewis Barker', birthDate: '1980-07-30', avatar: 'images/jean.webp', description: 'Design team', }, { id: 'george', name: 'George Gray', birthDate: '1980-07-31', avatar: 'images/laurent.webp', description: 'Design team', }, { id: 'laura', name: 'Laura Lagarde', birthDate: '1981-02-13', avatar: 'images/chloe.webp', description: 'Marketing team', }, ], getOptionId: (option) => option.id, children: (option, titleId) => { return (
{option.name}