diff --git a/src/components/FormInput.tsx b/src/components/FormInput.tsx index 60436014..395c5721 100644 --- a/src/components/FormInput.tsx +++ b/src/components/FormInput.tsx @@ -14,8 +14,8 @@ interface FormInpuProps< TFieldName extends FieldPath, > extends ComponentProps<'input'> { name: TFieldName - options?: RegisterOptions format?: (value: string) => string + sanitize?: (value: string) => string } export const FormInput = < @@ -23,14 +23,14 @@ export const FormInput = < TFieldName extends FieldPath, >({ name, - options, format, + sanitize, onChange, ...props }: FormInpuProps) => { const { onChange: onValueChange, ...registerResult } = useFormContext().register( name, - options, + sanitize ? { setValueAs: sanitize } : undefined, ) const handleChange: ChangeEventHandler = (e) => { diff --git a/src/components/PhoneInput.tsx b/src/components/PhoneInput.tsx index c2525b1c..fdf2ae0a 100644 --- a/src/components/PhoneInput.tsx +++ b/src/components/PhoneInput.tsx @@ -11,18 +11,16 @@ export const PhoneInput = (props: ComponentProps<'input'>) => ( type="tel" maxLength={15} minLength={15} - format={formatPhone} - options={{ - setValueAs: sanitizePhone, - }} + format={format} + sanitize={sanitize} {...props} /> ) -const sanitizePhone = (value: string) => value.replace(/\D/g, '').slice(0, 11) +const sanitize = (value: string) => value.replace(/\D/g, '').slice(0, 11) -const formatPhone = (value: string) => { - const digits = sanitizePhone(value) +const format = (value: string) => { + const digits = sanitize(value) if (!digits) return '' if (digits.length <= 2) return `(${digits}` diff --git a/src/components/PostalCodeInput.tsx b/src/components/PostalCodeInput.tsx index f13a1a21..1233b259 100644 --- a/src/components/PostalCodeInput.tsx +++ b/src/components/PostalCodeInput.tsx @@ -8,9 +8,7 @@ export const PostalCodeInput = (props: ComponentProps<'input'>) => ( type="text" placeholder="00000-000" format={format} - options={{ - setValueAs: sanitize, - }} + sanitize={sanitize} pattern="^(?:\d{8})?$" {...props} />