Download react-hook-form/examples/V7/fieldArrayMinLength.tsx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 2.02 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/examples/V7/fieldArrayMinLength.tsx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/examples/V7/fieldArrayMinLength.tsx
-
curl -L -o fieldArrayMinLength.tsx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/examples/V7/fieldArrayMinLength.tsx
2.02 kB
| import React from 'react'; | |
| import { useForm, useFieldArray } from 'react-hook-form'; | |
| import { object, array, string } from 'yup'; | |
| import { yupResolver } from '@hookform/resolvers/yup'; | |
| const validationSchema = object().shape({ | |
| questions: array() | |
| .of( | |
| object().shape({ | |
| text: string().required('Some text is required'), | |
| }), | |
| ) | |
| .required(), | |
| }); | |
| function App() { | |
| const { | |
| control, | |
| register, | |
| formState: { errors }, | |
| clearErrors, | |
| setValue, | |
| unregister, | |
| handleSubmit, | |
| trigger, | |
| } = useForm({ | |
| mode: 'onChange', | |
| resolver: yupResolver(validationSchema), | |
| }); | |
| const { fields, append, remove } = useFieldArray({ | |
| control, | |
| name: 'questions', | |
| }); | |
| const isInitialRender = React.useRef(true); | |
| const appendQuestion = () => { | |
| append({ | |
| text: '', | |
| }); | |
| if (errors.questions?.type === 'min') { | |
| clearErrors('questions'); // always clear errors when a question is appended | |
| } | |
| }; | |
| React.useEffect(() => { | |
| if (!fields.length && !isInitialRender.current) { | |
| trigger('questions'); | |
| } | |
| if (isInitialRender.current) { | |
| isInitialRender.current = false; | |
| } | |
| }, [fields, register, setValue, unregister, trigger]); | |
| return ( | |
| <form onSubmit={handleSubmit(console.log)}> | |
| <h1>Yup Validation - Field Array</h1> | |
| {fields.map((question, questionIndex) => ( | |
| <div key={question.id}> | |
| <input | |
| ref={register()} | |
| name={`questions[${questionIndex}].text`} | |
| control={control} | |
| defaultValue="" | |
| /> | |
| <button | |
| type="button" | |
| onClick={() => { | |
| remove(questionIndex); | |
| trigger(); | |
| }} | |
| > | |
| Remove question {question.id} | |
| </button> | |
| </div> | |
| ))} | |
| <p>Errors: {JSON.stringify(errors)}</p> | |
| <button type="button" onClick={appendQuestion}> | |
| Add question | |
| </button> | |
| <input type="submit" /> | |
| </form> | |
| ); | |
| } | |