);
}
/** A calendar next to the date field the cursor is in. */
function DatePicker() {
const form = useForm();
const [focus, setFocus] = useState(null);
useFormEvent((form) => form.onFocusChanged, setFocus);
const anchor = useFormWidgetAnchor(focus?.widget ?? null);
// A date field says so in its format, read from the PDF.
const field = focus?.field;
const format = field && 'format' in field ? field.format : null;
if (!field || field.kind !== 'text' || format?.kind !== 'date') return null;
const pick = (day: DatePartsInput) => {
// A text box writes what was typed when the cursor leaves it: the picked day goes in after.
(document.activeElement as HTMLElement | null)?.blur();
void form.setValue(field.ref, { value: formatDate(day, format.pattern) });
};
return (
{/* A press on the calendar leaves the cursor in the field until a day is picked. */}
event.preventDefault()}>
);
}
/** What the form refused last, so a mistyped date says why. */
function Refused() {
const [refused, setRefused] = useState(null);
useFormEvent(
(form) => form.onValidationRejected,
({ issues }) => setRefused(issues[0]?.message ?? 'The form refused that value.'),
);
useFormEvent(
(form) => form.onValueChanged,
() => setRefused(null),
);
return (
);
}
export default function App() {
return (
Loading…