import React, { useMemo, useState } from 'react'; import Icon from './Icon'; export type SelectionOption = { label:string, value: string } type SelectFieldProps = { defaultLabel: string, options: SelectionOption[], selected: string[], multiple?: boolean, updateField: Function, minWidth?: number, maxHeight?: number|string, rounded?: string, flags?: boolean, emptyMsg?: string } const SelectField = (props: SelectFieldProps) => { const { options, selected, defaultLabel = 'Select an Option', multiple = true, updateField, minWidth = 180, maxHeight = 96, rounded = 'rounded-3xl', flags = false, emptyMsg = '' } = props; const [showOptions, setShowOptions] = useState(false); const [filterInput, setFilterInput] = useState(''); const [filterdOptions, setFilterdOptions] = useState([]); const selectedLabels = useMemo(() => { return options.reduce((acc:string[], item:SelectionOption) :string[] => { return selected.includes(item.value) ? [...acc, item.label] : [...acc]; }, []); }, [selected, options]); const selectItem = (option:SelectionOption) => { let updatedSelect = [option.value]; if (multiple && Array.isArray(selected)) { if (selected.includes(option.value)) { updatedSelect = selected.filter((x) => x !== option.value); } else { updatedSelect = [...selected, option.value]; } } updateField(updatedSelect); if (!multiple) { setShowOptions(false); } }; const filterOptions = (event:React.FormEvent) => { setFilterInput(event.currentTarget.value); const filteredItems:SelectionOption[] = []; const userVal = event.currentTarget.value.toLowerCase(); options.forEach((option:SelectionOption) => { if (flags ? option.label.toLowerCase().startsWith(userVal) : option.label.toLowerCase().includes(userVal)) { filteredItems.push(option); } }); setFilterdOptions(filteredItems); }; return (
setShowOptions(!showOptions)}> {selected.length > 0 ? (selectedLabels.slice(0, 2).join(', ')) : defaultLabel} {multiple && selected.length > 2 && {(selected.length)}}
{showOptions && (
{options.length > 20 && (
)}
    {(options.length > 20 && filterdOptions.length > 0 && filterInput ? filterdOptions : options).map((opt) => { const itemActive = selected.includes(opt.value); return (
  • selectItem(opt)} > {multiple && ( )} {flags && } {opt.label}
  • ); })}
{emptyMsg && options.length === 0 &&

{emptyMsg}

}
)}
); }; export default SelectField;