Skip to content

Instantly share code, notes, and snippets.

@paulinhapenedo
Created March 3, 2022 23:02
Show Gist options
  • Save paulinhapenedo/7c996eedec283db45ef28bcc9ecdd4fe to your computer and use it in GitHub Desktop.
Save paulinhapenedo/7c996eedec283db45ef28bcc9ecdd4fe to your computer and use it in GitHub Desktop.
Custom select using react-select + react-final-form
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