import React, { useState } from 'react'; import type { Meta, StoryObj } from '@storybook/react-vite'; import { Form } from 'react-aria-components'; import { now, getLocalTimeZone } from '@internationalized/date'; import { Button } from '../Button/Button.quanta'; import { DateTimePicker } from './DateTimePicker.quanta'; function getCurrentUtcString(): string { return now(getLocalTimeZone()).toAbsoluteString(); } // DateTimePicker Stories const meta = { title: 'Quanta/DateTimePicker', component: DateTimePicker, parameters: { layout: 'centered', backgrounds: { disable: true }, }, tags: ['autodocs'], args: { label: 'Date & Time', }, } satisfies Meta; export default meta; type Story = StoryObj; // Basic Stories export const Default: Story = { render: (args) => , args: { name: 'datetime', label: 'Select date and time', description: 'Default datetime picker with minute granularity', granularity: 'minute', }, }; export const DateOnly: Story = { render: (args) => , args: { name: 'date-only', label: 'Date only', description: 'Date picker without time selection', granularity: 'day', }, }; export const DateOnlyWithDefaultValue: Story = { render: (args) => , args: { name: 'date-only', label: 'Date only', description: 'Date picker without time selection', granularity: 'day', defaultValue: '2025-05-17', }, }; export const WithDefaultValue: Story = { render: (args) => , args: { name: 'datetime-default', label: 'With default value', description: 'Uncontrolled component with default UTC value', defaultValue: '2025-05-17T22:23:00+00:00', // UTC time granularity: 'minute', }, }; // UTC/Local Timezone Demo const TimezoneExample = (args: any) => { const [value, setValue] = useState('2024-01-15T15:00:00'); // 3 PM UTC return (
UTC Value (what the component receives/returns):
{value || 'null'}
Your Local Timezone:
{getLocalTimeZone()}

How it works: The component receives UTC time but displays it converted to your local timezone. When you change the time, it converts back to UTC for the onChange callback.

); }; export const TimezoneConversion: Story = { render: TimezoneExample, args: { name: 'timezone-demo', label: 'UTC ↔ Local Timezone Demo', description: 'Shows how UTC values are converted to/from local timezone', granularity: 'minute', }, }; // Controlled vs Uncontrolled const ControlledExample = (args: any) => { const [value, setValue] = useState( '2024-01-15T10:00:00+00:00', ); return (
Current UTC value: {value || 'null'}
); }; export const Controlled: Story = { render: ControlledExample, args: { name: 'datetime-controlled', label: 'Controlled component', description: 'Value managed by parent component (UTC format)', granularity: 'minute', }, }; // Granularity Examples export const GranularityMinute: Story = { render: (args) => , args: { name: 'granularity-minute', label: 'Minute granularity', description: 'Shows date, hour, and minute segments', granularity: 'minute', }, }; export const GranularityHour: Story = { render: (args) => , args: { name: 'granularity-hour', label: 'Hour granularity', description: 'Shows date and hour segments', granularity: 'hour', }, }; export const GranularitySecond: Story = { render: (args) => , args: { name: 'granularity-second', label: 'Second granularity', description: 'Shows date, hour, minute, and second segments', granularity: 'second', }, }; // States export const Required: Story = { render: (args) => , args: { name: 'required-datetime', label: 'Required field', description: 'This field is required', isRequired: true, granularity: 'minute', }, }; export const Disabled: Story = { render: (args) => , args: { name: 'disabled-datetime', label: 'Disabled picker', defaultValue: getCurrentUtcString(), isDisabled: true, granularity: 'minute', }, }; export const ReadOnly: Story = { render: (args) => , args: { name: 'readonly-datetime', label: 'Read-only picker', defaultValue: getCurrentUtcString(), isReadOnly: true, granularity: 'minute', }, }; export const WithError: Story = { render: (args) => , args: { name: 'errored-datetime', label: 'With error', description: 'Shows error state styling', errorMessage: 'Please select a valid date and time', isInvalid: true, isRequired: true, defaultValue: '2024-01-15T10:30:00', granularity: 'minute', }, }; export const NotResettable: Story = { render: (args) => , args: { name: 'not-resettable', label: 'Non-resettable', description: 'No clear button available', defaultValue: getCurrentUtcString(), resettable: false, granularity: 'minute', }, }; const CustomValidationExample = () => { const [value, setValue] = useState(null); const [error, setError] = useState(null); const handleChange = (newValue: string | null) => { setValue(newValue); // Custom validation logic for string values if (!newValue) { setError('Date is required'); return; } try { const date = new Date(newValue); if (date.getDate() === 13) { setError('Friday the 13th is not allowed!'); return; } setError(null); } catch { setError('Invalid date format'); } }; return (
{value &&
Selected: {value}
}
); }; export const CustomValidation: Story = { render: CustomValidationExample, }; // Auto Current Time Feature const AutoCurrentTimeExample = () => { const [value, setValue] = useState(null); return (

Auto Current Time Feature

{value && (
Current UTC value: {value}
)}

Test: Clear the value, then select a date from the calendar. Notice it automatically sets the current local time and converts it to UTC.

); }; export const AutoCurrentTime: Story = { render: AutoCurrentTimeExample, }; // Form Integration const FormExample = () => (
); export const FormIntegration: Story = { render: FormExample, };