Skip to content

Instantly share code, notes, and snippets.

@tigerza117
Forked from hubgit/SelectField.tsx
Created May 19, 2023 07:21
Show Gist options
  • Save tigerza117/eb06faf18aa919c03841c292886e070b to your computer and use it in GitHub Desktop.
Save tigerza117/eb06faf18aa919c03841c292886e070b to your computer and use it in GitHub Desktop.
Use react-select with Formik
const options = [
{ value: 'foo', label: 'Foo' },
{ value: 'bar', label: 'Bar' },
]
return <Field name={'example'} component={SelectField} options={options} />
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