import { format, subYears } from 'date-fns'; import { h, VNode } from 'preact'; import { useLayoutEffect, useRef, useState } from 'preact/hooks'; import { DatePicker } from '../picker/DatePicker'; export interface DateInputProps { label: string; grabFocus?: boolean; tooltip?: string; error?: string; years?: Array; onConfirm?: () => void; bind: [string, (x: string) => void]; } export function DateInput(props: DateInputProps): VNode { const inputRef = useRef(null); useLayoutEffect(() => { if (props.grabFocus) inputRef.current?.focus(); }, [props.grabFocus]); const [opened, setOpened] = useState(false); const value = props.bind[0] || ''; const [dirty, setDirty] = useState(false); const showError = dirty && props.error; const calendar = subYears(new Date(), 30); return (

{ if (e.key === 'Enter' && props.onConfirm) props.onConfirm() }} onInput={(e) => { const text = e.currentTarget.value; setDirty(true); props.bind[1](text); }} ref={inputRef} />

{ setOpened(true); }} >

Using the format yyyy-mm-dd

{showError &&

{props.error}

} setOpened(false)} dateReceiver={(d) => { setDirty(true); const v = format(d, 'yyyy-MM-dd'); props.bind[1](v); }} />
); }