Improve Inputs readability

This commit is contained in:
Francisco Ignácio Fontoura Solla 2026-03-20 12:46:16 -03:00
parent d3577ceb6a
commit e13b55dadb
3 changed files with 9 additions and 13 deletions

View file

@ -14,8 +14,8 @@ interface FormInpuProps<
TFieldName extends FieldPath<TFieldValues>,
> extends ComponentProps<'input'> {
name: TFieldName
options?: RegisterOptions<TFieldValues, TFieldName>
format?: (value: string) => string
sanitize?: (value: string) => string
}
export const FormInput = <
@ -23,14 +23,14 @@ export const FormInput = <
TFieldName extends FieldPath<TFieldValues>,
>({
name,
options,
format,
sanitize,
onChange,
...props
}: FormInpuProps<TFieldValues, TFieldName>) => {
const { onChange: onValueChange, ...registerResult } = useFormContext<TFieldValues>().register(
name,
options,
sanitize ? { setValueAs: sanitize } : undefined,
)
const handleChange: ChangeEventHandler<HTMLInputElement> = (e) => {

View file

@ -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}`

View file

@ -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}
/>