diff --git a/src/forms/DateField.tsx b/src/forms/DateField.tsx index 9d9507f..cb09cf2 100644 --- a/src/forms/DateField.tsx +++ b/src/forms/DateField.tsx @@ -69,6 +69,7 @@ const DateField = (props: DateFieldProps) => { validate: { monthRange: (value: string) => { if (!props.required && !value?.length) return true + return parseInt(value) > 0 && parseInt(value) <= 12 }, }, @@ -95,6 +96,7 @@ const DateField = (props: DateFieldProps) => { validate: { dayRange: (value: string) => { if (!props.required && !value?.length) return true + return parseInt(value) > 0 && parseInt(value) <= 31 }, }, diff --git a/src/forms/TimeField.stories.tsx b/src/forms/TimeField.stories.tsx index f2c43f6..d828232 100644 --- a/src/forms/TimeField.stories.tsx +++ b/src/forms/TimeField.stories.tsx @@ -8,7 +8,7 @@ export default { } export const Default = () => { - const { register, watch, errors } = useForm({ mode: "onChange" }) + const { register, setValue, watch, errors } = useForm({ mode: "onChange" }) return ( { name="time" required={true} register={register} + setValue={setValue} watch={watch} error={!!errors?.time} /> diff --git a/src/forms/TimeField.tsx b/src/forms/TimeField.tsx index 3f5fc0e..e28935f 100644 --- a/src/forms/TimeField.tsx +++ b/src/forms/TimeField.tsx @@ -5,6 +5,7 @@ import { ErrorMessage } from "../notifications/ErrorMessage" import { Field } from "./Field" import { Select } from "../forms/Select" import { UseFormMethods } from "react-hook-form" +import { maskNumber } from "./DateField" export type TimeFieldPeriod = "am" | "pm" @@ -26,6 +27,7 @@ export type TimeFieldProps = { readerOnly?: boolean register: UseFormMethods["register"] required?: boolean + setValue?: UseFormMethods["setValue"] watch: UseFormMethods["watch"] seconds?: boolean dataTestId?: string @@ -54,9 +56,10 @@ const TimeField = ({ required = false, error, register, + setValue, watch, name, - id, + id = "time", label, labelClass, readerOnly, @@ -66,16 +69,16 @@ const TimeField = ({ dataTestId, strings, }: TimeFieldProps) => { - const fieldName = (baseName: string) => { + const getFieldName = (baseName: string) => { return [name, baseName].filter((item) => item).join(".") } // it prevents partial fill, all fields should be filled or nothing const [innerRequiredRule, setInnerRequiredRule] = useState(false) - const hoursField = watch(fieldName("hours")) - const minutesField = watch(fieldName("minutes")) - const secondsField = watch(fieldName("seconds")) + const hoursField = watch(getFieldName("hours")) + const minutesField = watch(getFieldName("minutes")) + const secondsField = watch(getFieldName("seconds")) useEffect(() => { const someFieldsFilled = hoursField || minutesField || secondsField @@ -90,7 +93,7 @@ const TimeField = ({ {label}
{ + if (!setValue) return + + setValue(getFieldName("hours"), maskNumber(e.target.value)) + }} describedBy={`${id}-error`} disabled={disabled} dataTestId={dataTestId ? `${dataTestId}-hours` : undefined} /> { + if (!setValue) return + + setValue(getFieldName("minutes"), maskNumber(e.target.value)) + }} describedBy={`${id}-error`} disabled={disabled} dataTestId={dataTestId ? `${dataTestId}-minutes` : undefined} @@ -141,7 +154,7 @@ const TimeField = ({ { + if (!setValue) return + + setValue(getFieldName("seconds"), maskNumber(e.target.value)) + }} describedBy={`${id}-error`} disabled={disabled} dataTestId={dataTestId ? `${dataTestId}-seconds` : undefined} @@ -164,8 +182,8 @@ const TimeField = ({ )}