import React, { ChangeEvent } from "react"; import classNames from "classnames"; import { Stack } from "../Stack"; import { Box } from "../Box"; import { Flex } from "../Flex"; import { Portal } from "../Portal"; import { SelectState, OptionInterface, combineReducers, createBaseReducer, multiValueReducer, searchReducer, useSelect, } from "./useSelect"; import { Option, Tag, NoOptions, surfaceClassName, surfaceMultiClassName, selectInputCn, Container, Listbox, ClearButton, Arrow, ComboboxSharedProps, } from "./ui"; const initialState: SelectState = { value: [], isOpen: false, searchValue: "", hoverIndex: 0, }; const reducers = [ createBaseReducer(initialState), searchReducer, multiValueReducer, ]; const rootReducer = combineReducers(reducers); function reducer( state: SelectState = initialState, action: any, ) { const nextState = rootReducer(state, action); return nextState; } export const ComboboxMulti = ({ options, placeholder = "Select…", isCreatable = false, isClearable = false, isSearchable = true, disabled = false, className, onCreate = () => {}, onChange = () => {}, }: ComboboxSharedProps) => { const { state, availableOptions, ref, onKeyPress, add, open, remove, search, close, hover, clear, create, comboboxRef, listboxStyles, } = useSelect({ options, reducer, initialState, onChange, onCreate }); const { isOpen, searchValue, value, hoverIndex } = state; const hasOptions = availableOptions.length > 0; const allowInput = isSearchable || isCreatable; const canCreate = isCreatable && !!searchValue; const canClear = isClearable && value.length > 0; const showOptions = availableOptions.map((opt, index) => { const isSelected = value.find((v) => v.value === opt.value); return ( ); }); const noOptions = ( ); return ( {value.map((val) => { return ( remove(val)}> {val.value} ); })} {allowInput && ( ): void => search(event.currentTarget.value) } value={searchValue} className={selectInputCn} ref={ref} onKeyDown={onKeyPress} placeholder={value.length === 0 ? placeholder : ""} disabled={disabled} /> )} {!allowInput && value.length === 0 && ( {placeholder} )} {isOpen && ( {hasOptions ? showOptions : noOptions} )} ); };