Created
January 10, 2022 22:52
-
-
Save dayhaysoos/c64ebef70480914a0bbdb991b1426c64 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React, { useState, useRef, useEffect } from 'react' | |
import { Box, Flex, Button, Text, Input } from 'theme-ui' | |
import { useFieldArray, useFormContext } from 'react-hook-form' | |
function ContributionInput() { | |
const { trigger, control, register } = useFormContext() | |
const { fields, append, remove } = useFieldArray({ | |
name: 'contributions', | |
control | |
}) | |
return ( | |
<> | |
{fields.map((field, index) => { | |
return ( | |
<div key={field.id}> | |
<input | |
{...register(`contributions.${index}.value`)} | |
defaultValue="" | |
/> | |
<button | |
type="button" | |
onClick={() => { | |
remove(index) | |
trigger() | |
}} | |
> | |
Remove question {field.id} | |
</button> | |
</div> | |
) | |
})} | |
<button type="button" onClick={() => append({ text: '' })}> | |
Add question | |
</button> | |
</> | |
) | |
} | |
export default ContributionInput |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment