Created
December 19, 2023 11:30
-
-
Save dninomiya/336437e9a2351965bb2c37172d101a2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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