Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dninomiya/336437e9a2351965bb2c37172d101a2d to your computer and use it in GitHub Desktop.
Save dninomiya/336437e9a2351965bb2c37172d101a2d to your computer and use it in GitHub Desktop.
'use client';
import { Controller, useFieldArray, useForm } from 'react-hook-form';
export default function Job() {
const { control } = useForm({
defaultValues: {
emails: [
{
value: '',
},
],
},
});
const { fields, append, remove } = useFieldArray({
control,
name: 'emails',
});
return (
<div className="space-y-4 flex flex-col">
{fields.map((field, i) => {
return (
<Controller
key={field.id}
control={control}
name={`emails.${i}.value`}
render={({ field: { onChange, onBlur, value } }) => (
<input
className="p-4"
onChange={(e) => {
if (e.target.value === '' && i !== 0) {
remove(i);
}
if (e.target.value.length > 0 && i === fields.length - 1) {
append({ value: '' });
}
onChange(e);
}}
value={value}
/>
)}
/>
);
})}
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment