Created
January 4, 2023 17:57
-
-
Save Qw4z1/6df97c8e2cba41f6528aa52e7d0ecfa2 to your computer and use it in GitHub Desktop.
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 { Field } from "formik"; | |
import Label from "./Label"; | |
import PlaceholderOption from "./PlaceholderOption"; | |
type PickerProps<K> = { | |
placeHolder: string; | |
nameInForm: string; | |
label: string; | |
data?: K[]; | |
mapper: (item: K) => PickerOption; | |
}; | |
type PickerOption = { id: string | number; name: string }; | |
const Picker = <K,>({ | |
placeHolder, | |
label, | |
nameInForm, | |
data, | |
mapper, | |
}: PickerProps<K>) => { | |
return ( | |
<div className="flex w-full flex-col"> | |
<Label htmlFor={nameInForm}>{label}</Label> | |
<Field | |
className="w-full text-pink-500" | |
as="select" | |
name={nameInForm} | |
id={nameInForm} | |
> | |
{[<PlaceholderOption key={0} text={placeHolder} />].concat( | |
data?.map((raw) => { | |
const item = mapper(raw); | |
return ( | |
<option key={item.id} value={item.id}> | |
{item.name} | |
</option> | |
); | |
}) ?? [] | |
)} | |
</Field> | |
</div> | |
); | |
}; | |
export default Picker; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment