mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-16 20:13:04 +00:00
Improve Inputs readability
This commit is contained in:
parent
d3577ceb6a
commit
e13b55dadb
3 changed files with 9 additions and 13 deletions
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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}`
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
/>
|
||||
|
|
|
|||
Loading…
Reference in a new issue