import React, { forwardRef } from "react"; import clxs from "clsx"; import * as RadixSelect from "@radix-ui/react-select"; import "./Select.scss"; import { tablerChevronDownIcon, tablerChevronUpIcon } from "./icons"; type SelectItems = Record; export type SelectProps = { items: SelectItems; value: T; onChange: (value: T) => void; placeholder?: string; ariaLabel?: string; }; type ConverterFunction = ( items: Record, getLabel: (item: T) => string, ) => SelectItems; export const convertToSelectItems: ConverterFunction = ( items, getLabel, ) => { const result: SelectItems = {}; for (const key in items) { result[key] = getLabel(items[key]); } return result; }; const Select = ({ items, value, onChange, placeholder, ariaLabel, }: SelectProps) => ( {placeholder && } {tablerChevronDownIcon} {tablerChevronUpIcon} {(Object.entries(items) as [T, string][]).map( ([itemValue, itemLabel]) => ( {itemLabel} ), )} {tablerChevronDownIcon} ); type SelectItemProps = React.ComponentProps; const SelectItem = forwardRef( ( { children, className, ...props }: SelectItemProps, forwardedRef: React.ForwardedRef, ) => { return ( {children} ); }, ); export default Select;