import React, { FC } from 'react'; import { useForm, Controller } from 'react-hook-form'; import { createStyles, makeStyles, Theme } from '@material-ui/core/styles'; import { TextField, Button } from '@material-ui/core'; import { DevTool } from '@hookform/devtools'; const useStyles = makeStyles((theme: Theme) => createStyles({ root: { '& > *': { margin: theme.spacing(0), }, }, form: { width: '100%', // Fix IE 11 issue. marginTop: theme.spacing(1), }, submit: { margin: theme.spacing(3, 0, 2), }, }), ); // TODO: real time form validation export const SignInForm: FC = () => { interface FormData { email: string; password: string; } const defaultValues: FormData = { email: '', password: '', }; const { control, register, errors, handleSubmit } = useForm<FormData>({ mode: 'onChange', reValidateMode: 'onChange', defaultValues, }); const onSubmit: any = (values: FormData) => { // Send data }; const classes = useStyles(); return ( <> <form className={classes.form} onSubmit={handleSubmit(onSubmit)} data-testid="Form" > <Controller name="email" control={control} defaultValues rules={{ required: true, pattern: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i, }} render={() => ( <TextField variant="outlined" margin="normal" required fullWidth id="email" label="Email Address" name="email" autoComplete="email" autoFocus error={Boolean(errors.email)} helperText={errors.email && 'Incorrect entry.'} /> )} /> <Controller name="password" control={control} defaultValues rules={{ required: true, min: 8, max: 60, }} render={() => ( <TextField variant="outlined" margin="normal" required fullWidth name="password" label="Password" type="password" id="password" autoComplete="password" autoFocus error={Boolean(errors.password)} helperText={errors.password && 'Incorrect entry.'} /> )} /> <Button type="submit" fullWidth variant="contained" color="primary" data-testid="Submit" className={classes.submit} > Sign In </Button> </form> <DevTool control={control} /> {/* set up the dev tool */} </> ); };