import { useState } from 'react'; import { StoryObj } from '@storybook/react-webpack5'; import { fn, userEvent, within } from 'storybook/test'; import { createSandboxStory, globalScope, } from '../../../.storybook/components/sandbox/SandboxEditor'; import { storySourceWithoutNoise } from '../../../.storybook/helpers'; import Calendar, { type CalendarProps } from '../Calendar'; import Modal from '../../Modal'; import Button from '../../Button'; import { action } from 'storybook/actions'; export default { component: Calendar, title: 'Forms/Calendar', args: { value: undefined, min: undefined, max: undefined, monthFormat: undefined, placeholder: undefined, disabledRules: undefined, onChange: fn(), onDateFocus: fn(), }, argTypes: { monthFormat: { control: 'select', options: ['long', 'short'] }, min: { control: 'date' }, max: { control: 'date' }, disabledRules: { control: false, table: { type: { summary: 'DisabledRules', detail: `{ weekdays?: ('mon'|'tue'|'wed'|'thu'|'fri'|'sat'|'sun')[] dates?: Date[] ranges?: { start: Date; end: Date }[] reason: string // required — shown in prompt, linked via aria-describedby }`, }, }, }, }, }; type Story = StoryObj; const epoch = new Date('2011-01-01'); const theFuture = new Date(epoch); theFuture.setUTCDate(epoch.getUTCDate() + 10); const thePast = new Date(epoch); thePast.setUTCDate(epoch.getUTCDate() - 10); export const Sandbox = createSandboxStory({ code: `const App = () => { const [value, setValue] = React.useState(new Date(2024, 5, 18)); return ( setValue(date)} /> ); };`, scope: { ...globalScope, Calendar }, }); export const Basic: Story = { render: (args: CalendarProps) => { const [value, setValue] = useState(epoch); return ( { action('onDateFocus')(date, options); }} onChange={(date) => { action('onChange')(date); setValue(date); }} /> ); }, args: { monthFormat: 'long', min: thePast, max: theFuture, }, }; export const WithoutConstraints: Story = { render: () => { const [value, setValue] = useState(epoch); return setValue(v)} />; }, }; export const NullValue: Story = { render: () => { const [value, setValue] = useState(null); return setValue(v)} />; }, }; export const InsideModal: Story = { render: () => { const [open, setOpen] = useState(false); const [value, setValue] = useState(epoch); return ( <> setValue(v)} /> } onClose={() => setOpen(false)} /> ); }, play: async ({ canvasElement }) => { const canvas = within(canvasElement); await userEvent.click(canvas.getByRole('button', { name: 'Open modal' })); }, }; /** * Use `disabledRules` to disable specific weekdays, individual dates, or date ranges. * Disabled dates remain focusable and keyboard-navigable — clicking or focusing one * fires `onDateFocus` with `isEnabled: false` and shows a negative inline prompt with * the `reason` text. `min`/`max` out-of-range dates show a built-in boundary prompt * (e.g. "Please select a date between 4 December 2024 and 18 January 2025."). * * The `reason` field is **required for accessibility**: it renders as an InlinePrompt * below the grid and is linked to every disabled cell via `aria-describedby`, so screen * reader users understand why a date is unavailable. For per-date reason customisation * see [DisabledDatesWithDynamicReason](#disabled-dates-with-dynamic-reason). */ export const DisabledDates: Story = storySourceWithoutNoise({ args: { min: new Date(2024, 11, 4), max: new Date(2025, 0, 18), }, render: function Render(args) { const [value, setValue] = useState(new Date(2024, 11, 13)); return ( { action('onDateFocus')(date, { isEnabled }); }} onChange={(date) => { action('onChange')(date); setValue(date); }} /> ); }, }); /** * When different disabled dates warrant different explanations, drive the `reason` * dynamically from `onDateFocus`. The `isEnabled` flag tells you whether the focused * date is selectable; from there you can look up the specific reason and update state. * * `min`/`max` out-of-range dates are handled automatically — the component shows a * built-in prompt with the boundary dates, so `determineReason` only needs to cover * rule-disabled dates. */ export const DisabledDatesWithDynamicReason: Story = storySourceWithoutNoise({ args: { min: new Date(2024, 11, 4), max: new Date(2025, 0, 18), }, render: function Render(args) { const DEFAULT_REASON = 'Some dates are unavailable for this service.'; const maintenanceDays = [new Date(2024, 11, 10), new Date(2025, 0, 15)]; const [value, setValue] = useState(new Date(2024, 11, 13)); const [reason, setReason] = useState(DEFAULT_REASON); function determineReason(date: Date, isEnabled: boolean): string { if (isEnabled) return DEFAULT_REASON; if ( maintenanceDays.some( (maintenanceDay) => maintenanceDay.toDateString() === date.toDateString(), ) ) { return 'This date is a maintenance day.'; } if (date.getDay() === 0 || date.getDay() === 6) { return 'We do not operate on weekends.'; } return 'This date is unavailable.'; } return ( { action('onDateFocus')(date, { isEnabled }); if (date) { setReason(determineReason(date, isEnabled)); } }} onChange={(date) => { action('onChange')(date); setValue(date); }} /> ); }, });