|
| 1 | +import type { Meta, StoryObj } from '@storybook/react-vite'; |
| 2 | +import { expect, userEvent, waitFor, within } from '@storybook/test'; |
| 3 | +import { useState } from 'react'; |
| 4 | +import { Select } from '@lambdacurry/forms/ui/select'; |
| 5 | + |
| 6 | +const meta = { |
| 7 | + title: 'UI/Select/Alignment', |
| 8 | + component: Select, |
| 9 | + parameters: { |
| 10 | + layout: 'centered', |
| 11 | + docs: { |
| 12 | + description: { |
| 13 | + story: |
| 14 | + 'Use `contentProps` to align the popover with right-aligned triggers, such as when a Select sits near the edge of a container.', |
| 15 | + }, |
| 16 | + }, |
| 17 | + }, |
| 18 | + tags: ['autodocs'], |
| 19 | +} satisfies Meta<typeof Select>; |
| 20 | + |
| 21 | +export default meta; |
| 22 | +type Story = StoryObj<typeof meta>; |
| 23 | + |
| 24 | +const fruits = [ |
| 25 | + { label: 'Apple', value: 'apple' }, |
| 26 | + { label: 'Banana', value: 'banana' }, |
| 27 | + { label: 'Cherry', value: 'cherry' }, |
| 28 | +]; |
| 29 | + |
| 30 | +const RightAlignedSelectExample = () => { |
| 31 | + const [value, setValue] = useState(''); |
| 32 | + |
| 33 | + return ( |
| 34 | + <div className="w-full max-w-md space-y-3"> |
| 35 | + <div className="rounded-lg border border-border bg-card p-6"> |
| 36 | + <div className="flex justify-end"> |
| 37 | + <div className="w-44"> |
| 38 | + <Select |
| 39 | + aria-label="Favorite fruit" |
| 40 | + placeholder="Select a fruit" |
| 41 | + options={fruits} |
| 42 | + value={value} |
| 43 | + onValueChange={setValue} |
| 44 | + contentProps={{ align: 'end' }} |
| 45 | + /> |
| 46 | + </div> |
| 47 | + </div> |
| 48 | + </div> |
| 49 | + <p className="text-sm text-muted-foreground"> |
| 50 | + Right-align the popover when the trigger is flush with the container edge to avoid clipping and keep the |
| 51 | + dropdown visible. |
| 52 | + </p> |
| 53 | + </div> |
| 54 | + ); |
| 55 | +}; |
| 56 | + |
| 57 | +export const RightAligned: Story = { |
| 58 | + render: () => <RightAlignedSelectExample />, |
| 59 | + play: async ({ canvasElement, step }) => { |
| 60 | + const canvas = within(canvasElement); |
| 61 | + const trigger = canvas.getByRole('combobox', { name: 'Favorite fruit' }); |
| 62 | + |
| 63 | + await step('Open the select', async () => { |
| 64 | + await userEvent.click(trigger); |
| 65 | + await waitFor(() => { |
| 66 | + const popover = document.querySelector('[data-slot="popover-content"]'); |
| 67 | + expect(popover).not.toBeNull(); |
| 68 | + expect(popover).toHaveAttribute('data-align', 'end'); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + await step('Navigate and select via keyboard', async () => { |
| 73 | + await waitFor(() => { |
| 74 | + const commandRoot = document.querySelector('[cmdk-root]'); |
| 75 | + expect(commandRoot).not.toBeNull(); |
| 76 | + }); |
| 77 | + const listbox = document.querySelector('[role="listbox"]') as HTMLElement; |
| 78 | + listbox.focus(); |
| 79 | + await waitFor(() => { |
| 80 | + expect(document.activeElement).toBe(listbox); |
| 81 | + }); |
| 82 | + await userEvent.keyboard('{ArrowDown}', { focusTrap: false }); |
| 83 | + await waitFor(() => { |
| 84 | + const activeItem = document.querySelector('[cmdk-item][aria-selected="true"]'); |
| 85 | + expect(activeItem).not.toBeNull(); |
| 86 | + }); |
| 87 | + const activeItem = document.querySelector('[cmdk-item][aria-selected="true"]') as HTMLElement; |
| 88 | + activeItem.dispatchEvent( |
| 89 | + new CustomEvent('cmdk-item-select', { |
| 90 | + detail: activeItem.getAttribute('data-value'), |
| 91 | + bubbles: true, |
| 92 | + }), |
| 93 | + ); |
| 94 | + await waitFor(() => { |
| 95 | + expect(document.querySelector('[data-slot="popover-content"]')).toBeNull(); |
| 96 | + expect(trigger).toHaveAttribute('aria-expanded', 'false'); |
| 97 | + }); |
| 98 | + }); |
| 99 | + }, |
| 100 | +}; |
0 commit comments