|
| 1 | +import { makeStyles } from "@material-ui/core/styles" |
| 2 | +import TextField from "@material-ui/core/TextField" |
| 3 | +import { FormikContextType, useFormik } from "formik" |
| 4 | +import React from "react" |
| 5 | +import * as Yup from "yup" |
| 6 | +import { getFormHelpers, onChangeTrimmed } from "../Form" |
| 7 | +import { LoadingButton } from "./../Button" |
| 8 | + |
| 9 | +interface AccountFormValues { |
| 10 | + name: string |
| 11 | + email: string |
| 12 | + username: string |
| 13 | +} |
| 14 | + |
| 15 | +export const Language = { |
| 16 | + nameLabel: "Name", |
| 17 | + usernameLabel: "Username", |
| 18 | + emailLabel: "Email", |
| 19 | + emailInvalid: "Please enter a valid email address.", |
| 20 | + emailRequired: "Please enter an email address.", |
| 21 | + updatePreferences: "Update preferences", |
| 22 | +} |
| 23 | + |
| 24 | +const validationSchema = Yup.object({ |
| 25 | + email: Yup.string().trim().email(Language.emailInvalid).required(Language.emailRequired), |
| 26 | + name: Yup.string().trim().optional(), |
| 27 | + username: Yup.string().trim(), |
| 28 | +}) |
| 29 | + |
| 30 | +const useStyles = makeStyles((theme) => ({ |
| 31 | + loginBtnWrapper: { |
| 32 | + marginTop: theme.spacing(6), |
| 33 | + borderTop: `1px solid ${theme.palette.action.disabled}`, |
| 34 | + paddingTop: theme.spacing(3), |
| 35 | + }, |
| 36 | + loginTextField: { |
| 37 | + marginTop: theme.spacing(2), |
| 38 | + }, |
| 39 | + submitBtn: { |
| 40 | + marginTop: theme.spacing(2), |
| 41 | + }, |
| 42 | +})) |
| 43 | + |
| 44 | +export interface AccountFormProps { |
| 45 | + isLoading: boolean |
| 46 | + initialValues: AccountFormValues |
| 47 | + onSubmit: (values: AccountFormValues) => Promise<void> |
| 48 | +} |
| 49 | + |
| 50 | +export const AccountForm: React.FC<AccountFormProps> = ({ isLoading, onSubmit, initialValues }) => { |
| 51 | + const styles = useStyles() |
| 52 | + |
| 53 | + const form: FormikContextType<AccountFormValues> = useFormik<AccountFormValues>({ |
| 54 | + initialValues, |
| 55 | + validationSchema, |
| 56 | + onSubmit, |
| 57 | + }) |
| 58 | + |
| 59 | + return ( |
| 60 | + <> |
| 61 | + <form onSubmit={form.handleSubmit}> |
| 62 | + <TextField |
| 63 | + {...getFormHelpers<AccountFormValues>(form, "name")} |
| 64 | + onChange={onChangeTrimmed(form)} |
| 65 | + autoFocus |
| 66 | + autoComplete="name" |
| 67 | + className={styles.loginTextField} |
| 68 | + fullWidth |
| 69 | + label={Language.nameLabel} |
| 70 | + variant="outlined" |
| 71 | + /> |
| 72 | + <TextField |
| 73 | + {...getFormHelpers<AccountFormValues>(form, "email")} |
| 74 | + onChange={onChangeTrimmed(form)} |
| 75 | + autoFocus |
| 76 | + autoComplete="email" |
| 77 | + className={styles.loginTextField} |
| 78 | + fullWidth |
| 79 | + label={Language.emailLabel} |
| 80 | + variant="outlined" |
| 81 | + /> |
| 82 | + <TextField |
| 83 | + {...getFormHelpers<AccountFormValues>(form, "username")} |
| 84 | + onChange={onChangeTrimmed(form)} |
| 85 | + autoFocus |
| 86 | + autoComplete="username" |
| 87 | + className={styles.loginTextField} |
| 88 | + fullWidth |
| 89 | + label={Language.usernameLabel} |
| 90 | + variant="outlined" |
| 91 | + /> |
| 92 | + |
| 93 | + <div className={styles.submitBtn}> |
| 94 | + <LoadingButton color="primary" loading={isLoading} fullWidth type="submit" variant="contained"> |
| 95 | + {isLoading ? "" : Language.updatePreferences} |
| 96 | + </LoadingButton> |
| 97 | + </div> |
| 98 | + </form> |
| 99 | + </> |
| 100 | + ) |
| 101 | +} |
0 commit comments