Last active
July 31, 2021 03:13
-
-
Save chadmuro/5d03b52ebaea4e3c1b841ed9ec613837 to your computer and use it in GitHub Desktop.
Form with no validation
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 from 'react'; | |
| import { makeStyles } from '@material-ui/core'; | |
| import TextField from '@material-ui/core/TextField'; | |
| import Button from '@material-ui/core/Button'; | |
| import { useForm, Controller } from 'react-hook-form'; | |
| const useStyles = makeStyles(theme => ({ | |
| root: { | |
| display: 'flex', | |
| flexDirection: 'column', | |
| justifyContent: 'center', | |
| alignItems: 'center', | |
| padding: theme.spacing(2), | |
| paddingTop: theme.spacing(4), | |
| '& .MuiTextField-root': { | |
| margin: theme.spacing(1), | |
| width: '300px', | |
| }, | |
| '& .MuiButtonBase-root': { | |
| margin: theme.spacing(2), | |
| }, | |
| }, | |
| link: { | |
| cursor: 'pointer', | |
| }, | |
| })); | |
| const Form = () => { | |
| const classes = useStyles(); | |
| const { handleSubmit, control } = useForm(); | |
| const onSubmit = data => { | |
| console.log(data); | |
| }; | |
| return ( | |
| <form className={classes.root} onSubmit={handleSubmit(onSubmit)}> | |
| <Controller | |
| name="email" | |
| control={control} | |
| defaultValue="" | |
| render={({ field: { onChange, value }, fieldState: { error } }) => ( | |
| <TextField | |
| label="Email" | |
| variant="outlined" | |
| value={value} | |
| onChange={onChange} | |
| error={!!error} | |
| helperText={error ? error.message : null} | |
| type="email" | |
| /> | |
| )} | |
| /> | |
| <Controller | |
| name="password" | |
| control={control} | |
| defaultValue="" | |
| render={({ field: { onChange, value }, fieldState: { error } }) => ( | |
| <TextField | |
| label="Password" | |
| variant="outlined" | |
| value={value} | |
| onChange={onChange} | |
| error={!!error} | |
| helperText={error ? error.message : null} | |
| type="password" | |
| /> | |
| )} | |
| /> | |
| <div> | |
| <Button | |
| variant="outlined" | |
| color="primary" | |
| onClick={() => console.log('closed')} | |
| > | |
| Cancel | |
| </Button> | |
| <Button type="submit" variant="contained" color="primary"> | |
| Sign up | |
| </Button> | |
| </div> | |
| </form> | |
| ); | |
| }; | |
| export default Form; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment