import React from 'react';
import type { Meta, StoryObj } from '@storybook/react-vite';
import {
Autocomplete,
Collection,
DialogTrigger,
Group,
Heading,
Label,
Modal,
Popover,
Select as RACSelect,
SelectValue,
useFilter,
} from 'react-aria-components';
import { Button } from '../Button/Button';
import { Dialog } from '../Dialog/Dialog';
import { Form } from '../Form/Form';
import { SearchField } from '../SearchField/SearchField';
import { Tag, TagGroup } from '../TagGroup/TagGroup';
import {
Select,
SelectItem,
SelectListBox,
SelectSection,
SelectSectionHeader,
} from './Select';
const options = [
{ label: '1', value: 'Aerospace' },
{ label: '2', value: 'Mechanical' },
{ label: '3', value: 'Civil' },
{ label: '4', value: 'Biomedical' },
{ label: '5', value: 'Nuclear' },
{ label: '6', value: 'Industrial' },
{ label: '7', value: 'Chemical' },
{ label: '8', value: 'Agricultural' },
{ label: '9', value: 'Electrical' },
{ label: '10', value: 'Telco' },
];
const groupedOptions = [
{
name: 'Fruit',
children: [
{ id: 'apple', name: 'Apple' },
{ id: 'banana', name: 'Banana' },
{ id: 'orange', name: 'Orange' },
{ id: 'pear', name: 'Pear' },
],
},
{
name: 'Vegetable',
children: [
{ id: 'broccoli', name: 'Broccoli' },
{ id: 'carrots', name: 'Carrots' },
{ id: 'lettuce', name: 'Lettuce' },
{ id: 'spinach', name: 'Spinach' },
],
},
];
const categories = [
{ id: 'news', name: 'News' },
{ id: 'travel', name: 'Travel' },
{ id: 'shopping', name: 'Shopping' },
{ id: 'business', name: 'Business' },
{ id: 'entertainment', name: 'Entertainment' },
{ id: 'food', name: 'Food' },
{ id: 'technology', name: 'Technology' },
{ id: 'health', name: 'Health' },
{ id: 'science', name: 'Science' },
];
const states = [
{ id: 'AL', name: 'Alabama' },
{ id: 'AK', name: 'Alaska' },
{ id: 'AZ', name: 'Arizona' },
{ id: 'CA', name: 'California' },
{ id: 'CO', name: 'Colorado' },
{ id: 'FL', name: 'Florida' },
{ id: 'MA', name: 'Massachusetts' },
{ id: 'NY', name: 'New York' },
{ id: 'TX', name: 'Texas' },
{ id: 'WA', name: 'Washington' },
];
const meta = {
title: 'Basic/Forms/Select',
component: Select,
parameters: {
layout: 'centered',
},
tags: ['autodocs'],
decorators: [
(Story) => (
),
],
} satisfies Meta;
export default meta;
type Story = StoryObj;
function ControlledValueStory(args: any) {
const [value, setValue] = React.useState('10');
return (
<>
Current selection: {JSON.stringify(value)}
>
);
}
function MultipleValueStory(args: any) {
const [value, setValue] = React.useState(['2', '9']);
return (
<>
Current selection: {JSON.stringify(value)}
>
);
}
function AutocompletePopoverStory() {
const { contains } = useFilter({ sensitivity: 'base' });
return (
{(item: (typeof categories)[number]) => (
{item.name}
)}
);
}
function TagGroupValueStory() {
const triggerRef = React.useRef(null);
const { contains } = useFilter({ sensitivity: 'base' });
const [value, setValue] = React.useState([]);
const selectedStateItems = (
selectedItems: Array<(typeof states)[number] | null>,
) =>
selectedItems.filter(
(item): item is (typeof states)[number] => item != null,
);
return (
setValue(nextValue as string[])}
>
style={{ flex: 1 }}>
{({ selectedItems }) => (
'No selected items'}
onRemove={(keys) =>
setValue((current) => current.filter((key) => !keys.has(key)))
}
>
{(item: (typeof states)[number]) => {item.name}}
)}
{(item: (typeof states)[number]) => (
{item.name}
)}
);
}
function InModalStory(args: any) {
return (
);
}
export const Default: Story = {
args: {
name: 'field-default',
label: 'Field title',
description: 'Optional help text',
placeholder: 'Select...',
children: (
<>
Hello
Lorem Ipsum
>
),
},
};
export const Items: Story = {
args: {
name: 'field-items',
label: 'Field title',
description: 'Optional help text',
placeholder: 'Select...',
items: options,
},
};
export const Sections: Story = {
render: (args) => (
),
args: {
name: 'field-sections',
label: 'Preferred fruit or vegetable',
placeholder: 'Select...',
},
};
export const ControlledValue: Story = {
render: ControlledValueStory,
args: {
name: 'field-controlled',
label: 'Pick an industry',
placeholder: 'Select...',
},
};
export const MultipleValue: Story = {
render: MultipleValueStory,
args: {
name: 'field-multiple',
label: 'Pick industries',
placeholder: 'Select...',
},
};
export const AutocompletePopover: Story = {
render: AutocompletePopoverStory,
};
export const TagGroupValue: Story = {
render: TagGroupValueStory,
};
export const InModal: Story = {
render: InModalStory,
args: {
name: 'field-modal',
label: 'Field title',
description: 'Open the modal, then open the Select popover.',
placeholder: 'Select...',
},
};
export const Required: Story = {
...Items,
args: {
...Items.args,
name: 'field-required',
isRequired: true,
},
};
export const Filled: Story = {
...Items,
args: {
...Items.args,
name: 'field-filled',
label: 'Filled field title',
defaultValue: '10',
isRequired: true,
},
};
export const Errored: Story = {
...Items,
args: {
...Items.args,
name: 'field-errored',
label: 'Errored field title',
defaultValue: '10',
errorMessage: 'This is the error',
isInvalid: true,
isRequired: true,
},
};
export const Disabled: Story = {
...Items,
args: {
...Items.args,
name: 'field-disabled',
label: 'Disabled field title',
isDisabled: true,
},
};
export const Validation: Story = {
render: (args) => (
),
args: {
label: 'Animal',
name: 'animal',
isRequired: true,
description: 'Please select an animal.',
},
};