import React, { useState } from 'react'; import type { Meta, StoryObj } from '@storybook/react-vite'; import { Form } from 'react-aria-components'; import { CalendarDate, CalendarDateTime, today, getLocalTimeZone, } from '@internationalized/date'; import { Button } from '../Button/Button.quanta'; import { Label, Description, FieldError } from '../Field/Field.quanta'; import { DateInput } from './DateInput.quanta'; import { DateField } from '../DateField/DateField.quanta'; // DateInput Stories const meta = { title: 'Quanta/DateInput', component: DateInput, parameters: { layout: 'centered', backgrounds: { disable: true }, }, tags: ['autodocs'], } satisfies Meta; export default meta; type Story = StoryObj; // Basic Stories export const Default: Story = { render: (args) => , args: {}, }; export const WithDefaultValue: Story = { render: (args) => ( Date input with default value (Christmas 2024) ), args: {}, }; // Controlled Example const ControlledExample = (args: any) => { const [value, setValue] = useState( today(getLocalTimeZone()), ); return (
Value managed by parent component
Current value: {value ? value.toString() : 'null'}
); }; export const Controlled: Story = { render: ControlledExample, args: {}, }; // DateTime Example const DateTimeExample = (args: any) => { const [value, setValue] = useState( new CalendarDateTime(2024, 6, 15, 14, 30), ); return (
Date input with time segments (granularity: minute)
Current value: {value ? value.toString() : 'null'}
); }; export const DateTime: Story = { render: DateTimeExample, args: {}, }; // States Example const StatesExample = () => { const [value, setValue] = useState(null); const [error, setError] = useState(null); const handleChange = (newValue: CalendarDate | null) => { setValue(newValue); // Custom validation if (newValue) { const today = new Date(); const selectedDate = new Date( newValue.year, newValue.month - 1, newValue.day, ); if (selectedDate < today) { setError('Past dates are not allowed'); } else { setError(null); } } else { setError(null); } }; return (
{/* Required Field */} This field is required {/* Invalid Field */} This field is invalid {/* Disabled Field */} This field is disabled {/* Read-only Field */} This field is read-only {/* Invalid Field with Custom Validation */} Only future dates are allowed {error && {error}} {value && !error && (
Selected date: {value.toString()} ✓
)}
); }; export const States: Story = { render: StatesExample, }; // Form Integration const FormExample = () => { return (
Enter your date of birth
); }; export const FormIntegration: Story = { render: FormExample, }; // Custom Styling export const CustomStyling: Story = { render: (args) => ( Date input with custom styling ), args: {}, };