diff --git a/src/components/EmailInput.tsx b/src/components/EmailInput.tsx index 4d8676fa..a12871fb 100644 --- a/src/components/EmailInput.tsx +++ b/src/components/EmailInput.tsx @@ -3,12 +3,13 @@ import { FormInput } from './FormInput' export const EmailInput = (props: ComponentProps<'input'>) => ( { - form: string +interface FormInpuProps< + TFieldValues extends FieldValues, + TFieldName extends FieldPath, +> extends ComponentProps<'input'> { + name: TFieldName + options?: RegisterOptions + format?: (value: string) => string } -export const FormInput = ({ form, ...props }: FormInpuProps) => { - const { register } = useFormContext() +export const FormInput = < + TFieldValues extends FieldValues, + TFieldName extends FieldPath, +>({ + name, + options, + format, + onChange, + ...props +}: FormInpuProps) => { + const { register } = useFormContext() + const { onChange: onValueChange, ...registerResult } = register(name, options) - return + const handleChange: ChangeEventHandler = (e) => { + const targetValue = e.target.value + const formattedValue = format?.(targetValue) ?? targetValue + e.target.value = formattedValue + onValueChange(e) + onChange?.(e) + } + + return } diff --git a/src/components/NameInput.tsx b/src/components/NameInput.tsx index 99927224..7c0dd80c 100644 --- a/src/components/NameInput.tsx +++ b/src/components/NameInput.tsx @@ -3,12 +3,13 @@ import { FormInput } from './FormInput' export const NameInput = (props: ComponentProps<'input'>) => ( diff --git a/src/components/PhoneInput.tsx b/src/components/PhoneInput.tsx index 052bb2ed..c2525b1c 100644 --- a/src/components/PhoneInput.tsx +++ b/src/components/PhoneInput.tsx @@ -3,14 +3,30 @@ import { FormInput } from './FormInput' export const PhoneInput = (props: ComponentProps<'input'>) => ( ) + +const sanitizePhone = (value: string) => value.replace(/\D/g, '').slice(0, 11) + +const formatPhone = (value: string) => { + const digits = sanitizePhone(value) + + if (!digits) return '' + if (digits.length <= 2) return `(${digits}` + if (digits.length <= 7) return `(${digits.slice(0, 2)}) ${digits.slice(2)}` + + return `(${digits.slice(0, 2)}) ${digits.slice(2, 7)}-${digits.slice(7)}` +} diff --git a/src/components/PostalCodeInput.tsx b/src/components/PostalCodeInput.tsx index ce8a04b5..291e9d15 100644 --- a/src/components/PostalCodeInput.tsx +++ b/src/components/PostalCodeInput.tsx @@ -2,5 +2,12 @@ import type { ComponentProps } from 'react' import { FormInput } from './FormInput' export const PostalCodeInput = (props: ComponentProps<'input'>) => ( - + ) diff --git a/src/components/StateSelect.tsx b/src/components/StateSelect.tsx index f485fe24..e109525b 100644 --- a/src/components/StateSelect.tsx +++ b/src/components/StateSelect.tsx @@ -6,7 +6,7 @@ interface StateSelectProps { placeholder?: string } -export const StateSelect = ({ placeholder = 'Selecione um estado' }) => { +export const StateSelect = ({ placeholder = 'Selecione um estado' }: StateSelectProps) => { const { register } = useFormContext() return ( diff --git a/src/components/WhatsappForm.tsx b/src/components/WhatsappForm.tsx index 861ff674..81a012f6 100644 --- a/src/components/WhatsappForm.tsx +++ b/src/components/WhatsappForm.tsx @@ -11,7 +11,7 @@ import { Textarea } from '@/components/ui/textarea' import { WhatsAppFormInput, whatsAppFormSchema } from '@/lib/schemas/whatsapp-form' import { zodResolver } from '@hookform/resolvers/zod' import { FormProvider, SubmitHandler, useForm } from 'react-hook-form' -import { useTransition } from 'react' +import { useEffect, useTransition } from 'react' import { submitWhatsapp } from '@/app/(frontend)/actions/submitWhatsapp' interface WhatsappFormProps { @@ -40,7 +40,8 @@ export const WhatsappForm = ({ classname, onSubmit }: WhatsappFormProps) => { await submitWhatsapp(input) methods.reset() onSubmit() - } catch { + } catch (e) { + console.log(e) methods.setError('root', { message: 'Falha ao enviar assinatura. Tente novamente.', }) @@ -78,11 +79,15 @@ export const WhatsappForm = ({ classname, onSubmit }: WhatsappFormProps) => { id="comment" {...methods.register('comment')} placeholder='Campo de Interesse: "O que você mais quer ver no canal?" (Votações, Bastidores, Ações Sociais, Agenda).' - required className="min-h-22" /> + {methods.formState.errors.root?.message ? ( + + {methods.formState.errors.root.message} + + ) : null}