-
-
Save tigerza117/eb06faf18aa919c03841c292886e070b to your computer and use it in GitHub Desktop.
Use react-select with Formik
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const options = [ | |
{ value: 'foo', label: 'Foo' }, | |
{ value: 'bar', label: 'Bar' }, | |
] | |
return <Field name={'example'} component={SelectField} options={options} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FieldProps } from 'formik' | |
import React from 'react' | |
import Select, { Option, ReactSelectProps } from 'react-select' | |
export const SelectField: React.SFC<ReactSelectProps & FieldProps> = ({ | |
options, | |
field, | |
form, | |
}) => ( | |
<Select | |
options={options} | |
name={field.name} | |
value={options ? options.find(option => option.value === field.value) : ''} | |
onChange={(option: Option) => form.setFieldValue(field.name, option.value)} | |
onBlur={field.onBlur} | |
/> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment