From 329d8c358d4aee801cf1cd494c81f2ccf8308ad6 Mon Sep 17 00:00:00 2001 From: Jared White Date: Wed, 4 Sep 2024 20:49:27 -0700 Subject: [PATCH] feat: allow only numbers when typing in the Date field (#157) * feat: allow only numbers for typing in the Date field, improve validation * chore: remove spurious console log --- src/forms/DateField.stories.tsx | 29 +++++++++++++++++++---------- src/forms/DateField.tsx | 29 +++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/forms/DateField.stories.tsx b/src/forms/DateField.stories.tsx index b864429..4c5a531 100644 --- a/src/forms/DateField.stories.tsx +++ b/src/forms/DateField.stories.tsx @@ -8,17 +8,26 @@ export default { } export const Default = () => { - const { register, watch, errors } = useForm({ mode: "onChange" }) + const { register, watch, errors, setValue, handleSubmit } = useForm() + + const onSubmit = (data: any) => {} return ( - +
+ +
+ + ) } diff --git a/src/forms/DateField.tsx b/src/forms/DateField.tsx index f2c24b1..9d9507f 100644 --- a/src/forms/DateField.tsx +++ b/src/forms/DateField.tsx @@ -23,6 +23,7 @@ export interface DateFieldProps { readerOnly?: boolean register: UseFormMethods["register"] required?: boolean + setValue?: UseFormMethods["setValue"] watch: UseFormMethods["watch"] dataTestId?: string strings?: { @@ -36,6 +37,10 @@ export interface DateFieldProps { } } +export const maskNumber = (value: string) => { + return value.match(/\d+/g)?.join("") +} + const DateField = (props: DateFieldProps) => { const { defaultDate, error, register, name, id, errorMessage } = props @@ -70,6 +75,11 @@ const DateField = (props: DateFieldProps) => { }} inputProps={{ maxLength: 2 }} register={register} + onChange={(e) => { + if (!props.setValue) return + + props.setValue(getFieldName("month"), maskNumber(e.target.value)) + }} dataTestId={props.dataTestId ? `${props.dataTestId}-month` : undefined} /> { }} inputProps={{ maxLength: 2 }} register={register} + onChange={(e) => { + if (!props.setValue) return + + props.setValue(getFieldName("day"), maskNumber(e.target.value)) + }} dataTestId={props.dataTestId ? `${props.dataTestId}-day` : undefined} /> { required: props.required, validate: { yearRange: (value: string) => { - if (props.required && value && parseInt(value) < 1900) return false - if (props.required && value && parseInt(value) > dayjs().year() + 10) return false if (!props.required && !value?.length) return true - return true + + const numVal = parseInt(value) + if (isNaN(numVal)) return false + return !(numVal < 1900 || numVal > dayjs().year() + 10) }, }, }} inputProps={{ maxLength: 4 }} register={register} + onChange={(e) => { + if (!props.setValue) return + + props.setValue(getFieldName("year"), maskNumber(e.target.value)) + }} dataTestId={props.dataTestId ? `${props.dataTestId}-year` : undefined} /> @@ -121,7 +142,7 @@ const DateField = (props: DateFieldProps) => { {(error?.month || error?.day || error?.year) && (
- + {errorMessage ? errorMessage : props.strings?.dateError ?? t("errors.dateError")}