|
| 1 | +importFormHelperTextfrom"@material-ui/core/FormHelperText" |
| 2 | +importTextFieldfrom"@material-ui/core/TextField" |
| 3 | +import{FormikContextType,FormikErrors,useFormik}from"formik" |
| 4 | +importReactfrom"react" |
| 5 | +import*asYupfrom"yup" |
| 6 | +import{getFormHelpers,onChangeTrimmed}from"../Form" |
| 7 | +import{Stack}from"../Stack/Stack" |
| 8 | +import{LoadingButton}from"./../Button" |
| 9 | + |
| 10 | +interfaceAccountFormValues{ |
| 11 | +name:string |
| 12 | +email:string |
| 13 | +username:string |
| 14 | +} |
| 15 | + |
| 16 | +exportconstLanguage={ |
| 17 | +nameLabel:"Name", |
| 18 | +usernameLabel:"Username", |
| 19 | +emailLabel:"Email", |
| 20 | +emailInvalid:"Please enter a valid email address.", |
| 21 | +emailRequired:"Please enter an email address.", |
| 22 | +updatePreferences:"Update preferences", |
| 23 | +} |
| 24 | + |
| 25 | +constvalidationSchema=Yup.object({ |
| 26 | +email:Yup.string().trim().email(Language.emailInvalid).required(Language.emailRequired), |
| 27 | +name:Yup.string().optional(), |
| 28 | +username:Yup.string().trim(), |
| 29 | +}) |
| 30 | + |
| 31 | +exporttypeAccountFormErrors=FormikErrors<AccountFormValues> |
| 32 | +exportinterfaceAccountFormProps{ |
| 33 | +isLoading:boolean |
| 34 | +initialValues:AccountFormValues |
| 35 | +onSubmit:(values:AccountFormValues)=>void |
| 36 | +formErrors?:AccountFormErrors |
| 37 | +error?:string |
| 38 | +} |
| 39 | + |
| 40 | +exportconstAccountForm:React.FC<AccountFormProps>=({ |
| 41 | + isLoading, |
| 42 | + onSubmit, |
| 43 | + initialValues, |
| 44 | + formErrors={}, |
| 45 | + error, |
| 46 | +})=>{ |
| 47 | +constform:FormikContextType<AccountFormValues>=useFormik<AccountFormValues>({ |
| 48 | + initialValues, |
| 49 | + validationSchema, |
| 50 | + onSubmit, |
| 51 | +}) |
| 52 | + |
| 53 | +return( |
| 54 | +<> |
| 55 | +<formonSubmit={form.handleSubmit}> |
| 56 | +<Stack> |
| 57 | +<TextField |
| 58 | +{...getFormHelpers<AccountFormValues>(form,"name")} |
| 59 | +autoFocus |
| 60 | +autoComplete="name" |
| 61 | +fullWidth |
| 62 | +label={Language.nameLabel} |
| 63 | +variant="outlined" |
| 64 | +/> |
| 65 | +<TextField |
| 66 | +{...getFormHelpers<AccountFormValues>(form,"email",formErrors.email)} |
| 67 | +onChange={onChangeTrimmed(form)} |
| 68 | +autoComplete="email" |
| 69 | +fullWidth |
| 70 | +label={Language.emailLabel} |
| 71 | +variant="outlined" |
| 72 | +/> |
| 73 | +<TextField |
| 74 | +{...getFormHelpers<AccountFormValues>(form,"username",formErrors.username)} |
| 75 | +onChange={onChangeTrimmed(form)} |
| 76 | +autoComplete="username" |
| 77 | +fullWidth |
| 78 | +label={Language.usernameLabel} |
| 79 | +variant="outlined" |
| 80 | +/> |
| 81 | + |
| 82 | +{error&&<FormHelperTexterror>{error}</FormHelperText>} |
| 83 | + |
| 84 | +<div> |
| 85 | +<LoadingButtoncolor="primary"loading={isLoading}type="submit"variant="contained"> |
| 86 | +{isLoading ?"" :Language.updatePreferences} |
| 87 | +</LoadingButton> |
| 88 | +</div> |
| 89 | +</Stack> |
| 90 | +</form> |
| 91 | +</> |
| 92 | +) |
| 93 | +} |