|
| 1 | +/* eslint-disable smarthr/a11y-input-in-form-control */ |
| 2 | +import { Meta, StoryObj } from '@storybook/react' |
| 3 | +import { userEvent, within } from '@storybook/test' |
| 4 | +import React, { useState } from 'react' |
| 5 | + |
| 6 | +import { Stack } from '../../../Layout' |
| 7 | +import { ComboBoxItem } from '../../types' |
| 8 | +import { MultiComboBox } from '../MultiComboBox' |
| 9 | + |
| 10 | +import { defaultItems } from './MultiComboBox.stories' |
| 11 | + |
| 12 | +/* |
| 13 | + * pict multiComboBox.pict |
| 14 | + * disabled error width selectedItems |
| 15 | + * false true なし なし |
| 16 | + * false false あり 複数 |
| 17 | + * true true あり 一つ |
| 18 | + * false false なし 一つ |
| 19 | + * true true なし 複数 |
| 20 | + * true false あり なし |
| 21 | + */ |
| 22 | + |
| 23 | +const _cases: Array<Omit<Parameters<typeof MultiComboBox>[0], 'items'>> = [ |
| 24 | + { disabled: false, error: true, width: undefined, selectedItems: [] }, |
| 25 | + { |
| 26 | + disabled: false, |
| 27 | + error: false, |
| 28 | + width: '15em', |
| 29 | + selectedItems: [defaultItems['option 1'], defaultItems['option 4']], |
| 30 | + }, |
| 31 | + { disabled: true, error: true, width: '15em', selectedItems: [defaultItems['option 3']] }, |
| 32 | + { |
| 33 | + disabled: false, |
| 34 | + error: false, |
| 35 | + width: undefined, |
| 36 | + selectedItems: [defaultItems['アイテムのラベルがReactNodeの場合']], |
| 37 | + }, |
| 38 | + { |
| 39 | + disabled: true, |
| 40 | + error: true, |
| 41 | + width: undefined, |
| 42 | + selectedItems: [ |
| 43 | + defaultItems['option 2'], |
| 44 | + defaultItems[ |
| 45 | + 'アイテムのラベルが長い場合(ダミーテキストダミーテキストダミーテキストダミーテキスト)' |
| 46 | + ], |
| 47 | + ], |
| 48 | + }, |
| 49 | + { disabled: true, error: false, width: '15em', selectedItems: [] }, |
| 50 | +] |
| 51 | + |
| 52 | +const waitForRAF = () => |
| 53 | + new Promise<void>((resolve) => { |
| 54 | + requestAnimationFrame(() => { |
| 55 | + resolve() |
| 56 | + }) |
| 57 | + }) |
| 58 | +const playMulti = async ({ canvasElement }: { canvasElement: HTMLElement }) => { |
| 59 | + const canvas = within(canvasElement) |
| 60 | + const comboboxes = await canvas.findAllByRole('combobox') |
| 61 | + comboboxes[comboboxes.length - 1].focus() |
| 62 | + const body = canvasElement.ownerDocument.body |
| 63 | + const option1 = await within(body).findByRole('option', { name: 'option 1' }) |
| 64 | + await userEvent.click(option1) |
| 65 | + await waitForRAF() |
| 66 | + const option2 = await within(body).findByRole('option', { name: 'option 2' }) |
| 67 | + await userEvent.click(option2) |
| 68 | + await waitForRAF() |
| 69 | + const helpMessage = await within(body).findAllByText('入力でフィルタリングできます。') |
| 70 | + await userEvent.click(helpMessage[0]) // カーソルの点滅によるVRTのフレーキーを避けるためにフォーカスを移動する |
| 71 | +} |
| 72 | + |
| 73 | +export default { |
| 74 | + title: 'Forms(フォーム)/MultiComboBox/VRT', |
| 75 | + component: MultiComboBox, |
| 76 | + render: (args) => { |
| 77 | + const items = Object.values(defaultItems) |
| 78 | + const [selectedItems, setSelectedItems] = useState<Array<ComboBoxItem<unknown>>>([]) |
| 79 | + return ( |
| 80 | + <Stack align="flex-start" gap={2} className="shr-h-screen"> |
| 81 | + {_cases.map((props, i) => ( |
| 82 | + <MultiComboBox {...args} {...props} items={items} key={i} /> |
| 83 | + ))} |
| 84 | + <MultiComboBox |
| 85 | + {...args} |
| 86 | + name="default" |
| 87 | + items={items} |
| 88 | + dropdownHelpMessage="入力でフィルタリングできます。" |
| 89 | + selectedItems={selectedItems} |
| 90 | + onChangeSelected={(its) => setSelectedItems(its)} |
| 91 | + /> |
| 92 | + </Stack> |
| 93 | + ) |
| 94 | + }, |
| 95 | + play: playMulti, |
| 96 | + parameters: { |
| 97 | + withTheming: true, |
| 98 | + chromatic: { disableSnapshot: false }, |
| 99 | + }, |
| 100 | + tags: ['!autodocs', 'skip-test-runner'], |
| 101 | +} as Meta<typeof MultiComboBox> |
| 102 | + |
| 103 | +export const VRT: StoryObj<typeof MultiComboBox> = {} |
| 104 | + |
| 105 | +export const VRTForcedColors: StoryObj<typeof MultiComboBox> = { |
| 106 | + ...VRT, |
| 107 | + parameters: { |
| 108 | + chromatic: { forcedColors: 'active' }, |
| 109 | + }, |
| 110 | +} |
0 commit comments