-
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
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,28 @@ | ||
import { useForm } from 'react-hook-form' | ||
|
||
import { DevTool } from '@hookform/devtools' | ||
import { instance } from 'api' | ||
|
||
export const Example01 = () => { | ||
const { register, handleSubmit, errors } = useForm() | ||
const { register, handleSubmit, errors, control } = useForm() | ||
|
||
const onSubmit = async () => await instance.post('/created') | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<h1>Example #01 — Submitting the form</h1> | ||
|
||
<input name='firstName' defaultValue='John' ref={register} /> | ||
|
||
<input | ||
name='lastName' | ||
defaultValue='Doe' | ||
ref={register({ required: true })} | ||
/> | ||
{errors.lastName && <span>This field is required</span>} | ||
|
||
<input type='submit' /> | ||
|
||
<DevTool control={control} /> | ||
</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
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
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,105 @@ | ||
import { useForm, FormProvider, useFormContext } from 'react-hook-form' | ||
import { instance } from 'api' | ||
import { DevTool } from '@hookform/devtools' | ||
import { yupResolver } from '@hookform/resolvers/yup' | ||
import * as yup from 'yup' | ||
import { Grid, TextField, Button as MuiButton } from '@material-ui/core' | ||
import { path } from 'ramda' | ||
|
||
const schema = yup.object().shape({ | ||
firstName: yup.string().required(), | ||
lastName: yup.string().required(), | ||
}) | ||
|
||
export const Example06 = () => { | ||
const formProps = useForm({ resolver: yupResolver(schema) }) | ||
const { control, 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)}> | ||
<Grid container spacing={2}> | ||
<Grid item xs={12}> | ||
<h1> | ||
Example #06 — Making it look nice with @material-ui (but is | ||
doesn't work) | ||
</h1> | ||
</Grid> | ||
<Grid item xs={4}> | ||
<Input name='firstName' defaultValue='John' label='First name' /> | ||
</Grid> | ||
<Grid item xs={4}> | ||
<Input name='lastName' defaultValue='Doe' label='Last name' /> | ||
</Grid> | ||
<Grid item xs={4}> | ||
<Button type='submit' /> | ||
</Grid> | ||
</Grid> | ||
<DevTool control={control} /> | ||
</form> | ||
</FormProvider> | ||
) | ||
} | ||
|
||
const Input = ({ name, label, defaultValue = '' }) => { | ||
const { | ||
register, | ||
formState: { isSubmitting }, | ||
errors, | ||
} = useFormContext() | ||
|
||
const disabled = isSubmitting | ||
const helperText = path([name, 'message'], errors) | ||
const error = Boolean(helperText) | ||
|
||
return ( | ||
<> | ||
<TextField | ||
name={name} | ||
label={label} | ||
defaultValue={defaultValue} | ||
ref={register} | ||
disabled={disabled} | ||
helperText={helperText} | ||
error={error} | ||
variant='filled' | ||
fullWidth | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
const Button = ({ | ||
children = 'Submit', | ||
variant = 'outlined', | ||
fullWidth = true, | ||
...props | ||
}) => { | ||
const { | ||
formState: { isSubmitting }, | ||
} = useFormContext() | ||
|
||
return ( | ||
<MuiButton | ||
{...props} | ||
disabled={isSubmitting} | ||
variant={variant} | ||
fullWidth={fullWidth}> | ||
{children} | ||
</MuiButton> | ||
) | ||
} |