teqo-backup/src/components/PostalCodeInput.tsx
Francisco Ignácio Fontoura Solla 01779dfd71 fix zip code input field
2026-05-01 00:22:18 -03:00

21 lines
547 B
TypeScript

import type { ComponentProps } from 'react'
import { FormInput } from './FormInput'
export const PostalCodeInput = (props: ComponentProps<'input'>) => (
<FormInput
name="postalCode"
id="postalCode"
type="text"
placeholder="00000-000"
format={format}
sanitize={sanitize}
{...props}
/>
)
const sanitize = (value: string) => value.replace(/\D/g, '').slice(0, 8)
const format = (value: string) => {
const digits = sanitize(value)
return digits.length <= 5 ? digits : `${digits.slice(0, 5)}-${digits.slice(5)}`
}