import React, { useState, useCallback } from 'react'; import { Story, Meta } from '@storybook/react/types-6-0'; import { DateInput, DateInputProps, } from '../../components/widgets/NumberAndDateInputs'; export default { title: 'Widgets/Date Input', component: DateInput, } as Meta; const ControlledTemplate: Story = (args) => { const [value, setValue] = useState('2005-01-03'); const onValueChange = useCallback( (newValue) => { console.log(`new value = ${newValue}`); setValue(newValue as string); }, [setValue] ); return ( ); }; export const Controlled = ControlledTemplate.bind({}); Controlled.args = { label: 'Controlled', }; export const ControlledBounded = ControlledTemplate.bind({}); ControlledBounded.args = { label: 'Controlled (2000-01-01 to 2009-12-31)', minValue: '2000-01-01', maxValue: '2009-12-31', }; export const ControlledLinkedPair: Story = () => { const [linkedValue, setLinkedValue] = useState('2020-02-20'); return ( <> { console.log(`A new value = ${newValue}`); setLinkedValue(newValue as string); }} containerStyles={{ margin: 25 }} /> { console.log(`B new value = ${newValue}`); setLinkedValue(newValue as string); }} containerStyles={{ margin: 25 }} /> ); }; export const ControlledBounds: Story = () => { const [value, setValue] = useState('2020-01-04'); const [min, setMin] = useState('2020-01-01'); const [max, setMax] = useState('2020-01-09'); return ( <> { console.log(`new value = ${newValue}`); // for some reason the `newValue != null` is needed because // the `useState(0)` has an initial value provided if (newValue != null) setValue(newValue as string); }} containerStyles={{ margin: 25 }} /> { if (newValue != null) setMin(newValue as string); }} containerStyles={{ margin: 25 }} /> { if (newValue != null) setMax(newValue as string); }} containerStyles={{ margin: 25 }} /> ); }; export const CustomValidator = ControlledTemplate.bind({}); CustomValidator.args = { label: 'Weekdays only', validator: (newValue) => { // allow empty values in this case (emulates `required: false`) if (newValue == null) return { validity: true, message: '' }; // uncomment to emulate `required: true` functionality // if (newValue == null) return { validity: false, message: 'please pick a day' }; const date = new Date(newValue); if (date) { if (date.getDay() > 0 && date.getDay() < 6) { return { validity: true, message: '' }; } else { return { validity: false, message: 'Please pick a weekday' }; } } else { return { validity: false, message: 'badly formatted date' }; } }, };