-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
241 additions
and
6 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useForm } from 'react-hook-form' | ||
|
||
import { instance } from 'api' | ||
|
||
export const Example02 = () => { | ||
const { | ||
register, | ||
handleSubmit, | ||
errors, | ||
formState: { isSubmitting }, | ||
} = useForm() | ||
|
||
const onSubmit = async () => await instance.post('/created') | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<h1>Example #02 — Disabling the form while submitting</h1> | ||
|
||
<input | ||
name='firstname' | ||
defaultValue='John' | ||
ref={register} | ||
disabled={isSubmitting} | ||
/> | ||
|
||
<input | ||
name='lastName' | ||
defaultValue='Doe' | ||
ref={register({ required: true })} | ||
disabled={isSubmitting} | ||
/> | ||
{errors.lastName && <span>This field is required</span>} | ||
|
||
<input type='submit' disabled={isSubmitting} /> | ||
</form> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useForm } from 'react-hook-form' | ||
import { instance } from 'api' | ||
import { yupResolver } from '@hookform/resolvers/yup' | ||
import * as yup from 'yup' | ||
|
||
const schema = yup.object().shape({ | ||
firstName: yup.string().required(), | ||
lastName: yup.string().required(), | ||
}) | ||
|
||
export const Example03 = () => { | ||
const { | ||
register, | ||
handleSubmit, | ||
errors, | ||
formState: { isSubmitting }, | ||
} = useForm({ resolver: yupResolver(schema) }) | ||
|
||
const onSubmit = async () => await instance.post('/created') | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<h1>Example #03 — Schema Validation with `yup`</h1> | ||
|
||
<input | ||
name='firstName' | ||
defaultValue='John' | ||
ref={register} | ||
disabled={isSubmitting} | ||
/> | ||
|
||
<input | ||
name='lastName' | ||
defaultValue='Doe' | ||
ref={register()} | ||
disabled={isSubmitting} | ||
/> | ||
{errors.lastName && <span>{errors.lastName.message}</span>} | ||
|
||
<input type='submit' disabled={isSubmitting} /> | ||
|
||
<pre>{JSON.stringify(errors, null, 2)}</pre> | ||
</form> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useForm, FormProvider, useFormContext } from 'react-hook-form' | ||
import { instance } from 'api' | ||
import { yupResolver } from '@hookform/resolvers/yup' | ||
import * as yup from 'yup' | ||
|
||
const schema = yup.object().shape({ | ||
firstName: yup.string().required(), | ||
lastName: yup.string().required(), | ||
}) | ||
|
||
export const Example04 = () => { | ||
const formProps = useForm({ resolver: yupResolver(schema) }) | ||
const { handleSubmit } = formProps | ||
|
||
const onSubmit = async () => await instance.post('/created') | ||
|
||
return ( | ||
<FormProvider {...formProps}> | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<h1>Example #04 — Create smart form components</h1> | ||
|
||
<Input name='firstName' defaultValue='John' /> | ||
|
||
<Input name='lastName' defaultValue='Doe' /> | ||
|
||
<Button type='submit' /> | ||
</form> | ||
</FormProvider> | ||
) | ||
} | ||
|
||
const Input = ({ name, defaultValue = '' }) => { | ||
const { | ||
register, | ||
formState: { isSubmitting }, | ||
errors, | ||
} = useFormContext() | ||
|
||
return ( | ||
<> | ||
<input | ||
name={name} | ||
defaultValue={defaultValue} | ||
ref={register} | ||
disabled={isSubmitting} | ||
/> | ||
{errors[name] && <span>{errors[name].message}</span>} | ||
</> | ||
) | ||
} | ||
|
||
const Button = ({ children = 'Submit', ...props }) => { | ||
const { | ||
formState: { isSubmitting }, | ||
} = useFormContext() | ||
|
||
return ( | ||
<button {...props} disabled={isSubmitting}> | ||
{children} | ||
</button> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useForm, FormProvider, useFormContext } from 'react-hook-form' | ||
import { instance } from 'api' | ||
import { yupResolver } from '@hookform/resolvers/yup' | ||
import * as yup from 'yup' | ||
|
||
const schema = yup.object().shape({ | ||
firstName: yup.string().required(), | ||
lastName: yup.string().required(), | ||
}) | ||
|
||
export const Example05 = () => { | ||
const formProps = useForm({ resolver: yupResolver(schema) }) | ||
const { handleSubmit, setError } = formProps | ||
|
||
const onSubmit = async () => { | ||
try { | ||
await instance.post('/example05') | ||
} catch ({ response: { data } }) { | ||
if (data.firstName) { | ||
setError('firstName', { message: data.firstName }) | ||
} | ||
|
||
if (data.lastName) { | ||
setError('lastName', { message: data.lastName }) | ||
} | ||
} | ||
} | ||
|
||
return ( | ||
<FormProvider {...formProps}> | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<h1>Example #05 — Dealing with errors from the server</h1> | ||
|
||
<Input name='firstName' defaultValue='John' /> | ||
|
||
<Input name='lastName' defaultValue='Doe' /> | ||
|
||
<Button type='submit' /> | ||
</form> | ||
</FormProvider> | ||
) | ||
} | ||
|
||
const Input = ({ name, defaultValue = '' }) => { | ||
const { | ||
register, | ||
formState: { isSubmitting }, | ||
errors, | ||
} = useFormContext() | ||
|
||
return ( | ||
<> | ||
<input | ||
name={name} | ||
defaultValue={defaultValue} | ||
ref={register} | ||
disabled={isSubmitting} | ||
/> | ||
{errors[name] && <span>{errors[name].message}</span>} | ||
</> | ||
) | ||
} | ||
|
||
const Button = ({ children = 'Submit', ...props }) => { | ||
const { | ||
formState: { isSubmitting }, | ||
} = useFormContext() | ||
|
||
return ( | ||
<button {...props} disabled={isSubmitting}> | ||
{children} | ||
</button> | ||
) | ||
} |
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
b647c9c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: