Last active
March 31, 2025 22:59
-
Star
(131)
You must be signed in to star a gist -
Fork
(5)
You must be signed in to fork a gist
-
-
Save hubgit/e394e9be07d95cd5e774989178139ae8 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
I just did this...
onValueChange={(val) => formik.setFieldValue('timezone', val)}