import { useState } from 'react'; import { StoryObj } from '@storybook/react-webpack5'; import { fn } from 'storybook/test'; import { action } from 'storybook/actions'; import { createSandboxStory, globalScope, } from '../../../.storybook/components/sandbox/SandboxEditor'; import { storySourceWithoutNoise } from '../../../.storybook/helpers'; import { Size } from '../../common'; import Field from '../../Field'; import DateLookup, { type DateLookupProps } from '..'; export default { component: DateLookup, title: 'Forms/DateLookup', args: { onFocus: fn(), onBlur: fn(), }, argTypes: { size: { control: 'select', options: [Size.SMALL, Size.MEDIUM, Size.LARGE] }, disabled: { control: 'boolean' }, label: { control: 'text' }, monthFormat: { control: 'select', options: ['long', 'short'] }, placeholder: { control: 'text' }, id: { control: 'text' }, clearable: { control: 'boolean' }, min: { control: 'date' }, max: { control: 'date' }, }, }; 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, 14)); return ( setValue(date)} /> ); };`, scope: globalScope, }); export const Basic: Story = { render: (args: DateLookupProps) => { const [value, setValue] = useState(epoch); return ( setValue(v)} /> ); }, args: { size: Size.MEDIUM, disabled: false, label: 'label', monthFormat: 'long', placeholder: 'placeholder', id: 'date-lookup', clearable: true, min: thePast, max: theFuture, }, }; export const WithField: Story = { render: (args: DateLookupProps) => { const [value, setValue] = useState(epoch); return ( setValue(v)} /> ); }, }; export const RightAligned: Story = { render: (args: DateLookupProps) => { const [value, setValue] = useState(epoch); return (

This example demonstrates the automatic right alignment behaviour of the calendar. If, when the calendar is opened, it is cut off on the right, the component will align the calendar to the rightmost edge of the lookup input.

setValue(v)} />
); }, }; /** * Use `disabledRules` to disable specific weekdays, individual dates, or date ranges. * Disabled dates remain focusable and keyboard-navigable — clicking or focusing one * keeps the panel open, fires `onDateSelect` 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 9 December 2024 and * 22 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: { size: Size.MEDIUM, label: 'Select a date', placeholder: 'Choose date', monthFormat: 'long', min: new Date(2024, 11, 9), max: new Date(2025, 0, 22), }, render: function Render(args: DateLookupProps) { const min = new Date(2024, 11, 9); const max = new Date(2025, 0, 22); const bankHolidays = [new Date(2024, 11, 25), new Date(2024, 11, 26), new Date(2025, 0, 1)]; const [value, setValue] = useState(null); return ( { action('onChange')(date); setValue(date); }} onDateSelect={({ date, isEnabled }) => { action('onDateSelect')({ date, isEnabled }); }} /> ); }, }); /** * When different disabled dates warrant different explanations, drive the `reason` * dynamically from `onDateSelect`. 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: { size: Size.MEDIUM, label: 'Select a date', placeholder: 'Choose date', monthFormat: 'long', min: new Date(2024, 11, 9), max: new Date(2025, 0, 22), }, render: function Render(args: DateLookupProps) { const DEFAULT_REASON = 'Some dates are unavailable for this service.'; const bankHolidays = [new Date(2024, 11, 25), new Date(2024, 11, 26), new Date(2025, 0, 1)]; const [value, setValue] = useState(null); const [reason, setReason] = useState(DEFAULT_REASON); function determineReason(date: Date, isEnabled: boolean): string { if (isEnabled) return DEFAULT_REASON; if (bankHolidays.some((holiday) => holiday.toDateString() === date.toDateString())) { return 'This date is a bank holiday.'; } if (date.getDay() === 0 || date.getDay() === 6) { return 'We do not operate on weekends.'; } return 'This date is unavailable due to scheduled maintenance.'; } return ( { action('onChange')(date); setValue(date); }} onDateSelect={({ date, isEnabled }) => { action('onDateSelect')({ date, isEnabled }); setReason(determineReason(date, isEnabled)); }} /> ); }, });