Skip to content

Instantly share code, notes, and snippets.

@Qw4z1
Created January 4, 2023 17:57
Show Gist options
  • Save Qw4z1/6df97c8e2cba41f6528aa52e7d0ecfa2 to your computer and use it in GitHub Desktop.
Save Qw4z1/6df97c8e2cba41f6528aa52e7d0ecfa2 to your computer and use it in GitHub Desktop.
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