Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 918dd80

Browse files
committed
Add util for form props
1 parent d2741c3 commit 918dd80

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

site/src/components/Form/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
import { FormikContextType } from "formik/dist/types"
2+
13
export * from "./FormCloseButton"
24
export * from "./FormSection"
35
export * from "./FormDropdownField"
46
export * from "./FormTextField"
57
export * from "./FormTitle"
8+
9+
export function getFormHelpers<T>(form: FormikContextType<T>, name: keyof T) {
10+
const touched = form.touched[name]
11+
const errors = form.errors[name]
12+
return {
13+
...form.getFieldProps(name),
14+
id: name,
15+
error: touched && Boolean(errors),
16+
helperText: touched && errors
17+
}
18+
}

site/src/components/SignIn/SignInForm.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Welcome } from "./Welcome"
77
import FormHelperText from "@material-ui/core/FormHelperText"
88
import { LoadingButton } from "./../Button"
99
import TextField from "@material-ui/core/TextField"
10+
import { getFormHelpers } from "../Form"
1011

1112
/**
1213
* BuiltInAuthFormValues describes a form using built-in (email/password)
@@ -69,19 +70,16 @@ export const SignInForm: React.FC<SignInFormProps> = ({ isLoading, authErrorMess
6970
<Welcome />
7071
<form onSubmit={form.handleSubmit}>
7172
<TextField
72-
{...form.getFieldProps("email")}
73+
{...getFormHelpers<BuiltInAuthFormValues>(form, "email")}
7374
autoFocus
7475
autoComplete="email"
7576
className={styles.loginTextField}
76-
error={form.touched.email && Boolean(form.errors.email)}
7777
fullWidth
78-
helperText={form.touched.email && form.errors.email}
79-
id="email"
8078
label={LANGUAGE.emailLabel}
8179
variant="outlined"
8280
/>
8381
<TextField
84-
{...form.getFieldProps("password")}
82+
{...getFormHelpers<BuiltInAuthFormValues>(form, "password")}
8583
autoComplete="current-password"
8684
className={styles.loginTextField}
8785
fullWidth

0 commit comments

Comments
 (0)