Created
March 3, 2022 23:02
-
-
Save paulinhapenedo/7c996eedec283db45ef28bcc9ecdd4fe to your computer and use it in GitHub Desktop.
Custom select using react-select + react-final-form
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 React from "react"; | |
import { FieldRenderProps } from "react-final-form"; | |
import Select, { ValueType } from "react-select"; | |
type Option = { | |
label: string; | |
value: string; | |
}; | |
// as seen here https://stackoverflow.com/a/66746766/5763750 | |
export const CustomSelect = ({ input, options, ...rest }: FieldRenderProps<string, HTMLElement>) => { | |
const handleChange = (option: ValueType<Option, false>) => { | |
input.onChange(option?.value); | |
}; | |
return ( | |
<Select | |
{...input} | |
{...rest} | |
onChange={handleChange} | |
options={options} | |
value={options ? options.find((option: Option) => option.value === input.value) : ""} | |
/> | |
); | |
}; | |
export default CustomSelect; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment