Skip to content

Instantly share code, notes, and snippets.

@efleurine
Forked from oukayuka/UserSearchForm.tsx
Created March 28, 2019 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efleurine/459c7c4170642d89f20621cc1377fc9c to your computer and use it in GitHub Desktop.
Save efleurine/459c7c4170642d89f20621cc1377fc9c to your computer and use it in GitHub Desktop.
Formik sample with TypeScript
import { InjectedFormikProps, withFormik } from 'formik';
import * as React from 'react';
import * as Yup from 'yup';
interface FormValues {
login: string;
}
interface FormProps {
login?: string;
}
const InnerForm: React.SFC<InjectedFormikProps<FormProps, FormValues>> = (
props,
) => (
<form onSubmit={props.handleSubmit}>
<input
id="login"
placeholder="User name..."
type="text"
onChange={props.handleChange}
value={props.values.login}
/>
{props.touched.login && props.errors.login &&
<div>{props.errors.login}</div>}
<button
type="submit"
disabled={props.isSubmitting}
>
Submit
</button>
</form>
);
const UserSearchForm = withFormik<FormProps, FormValues>({
mapPropsToValues: () => ({ login: '' }),
validationSchema: Yup.object().shape({
login: Yup.string()
.max(16, 'Please input 16 characters or less')
.required('Please input login name'),
},
),
handleSubmit: (values, { setSubmitting }) => {
setTimeout(
() => {
alert(JSON.stringify(values, null, 2));
setSubmitting(false);
},
1000,
);
},
})(InnerForm);
export default UserSearchForm;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment