UNPKG

@kadoui/react

Version:

Kadoui primitive components for React

46 lines (45 loc) 1.96 kB
"use client"; import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import { use } from "react"; import { SelectBoxContext } from "./SelectBoxContext"; export default function SelectBoxOptions({ options, multiSelect, optionValue, setOptionValue, ...p }) { const { setInputFocused, inputSearch, setInputSearch } = use(SelectBoxContext); const filteredOptions = []; const otherOptions = []; options.forEach((item) => { if (item.name.toLowerCase().includes(inputSearch.toLowerCase().trim())) { filteredOptions.push(item); } else { otherOptions.push(item); } }); const renderOptions = (options, isOther) => { return options.map((item) => { const isSelected = multiSelect ? optionValue.some((v) => v.value === item.value) : optionValue?.value === item.value; return (_jsx("button", { type: "button", disabled: isOther, "data-state": isSelected, onClick: () => { if (multiSelect) { if (isSelected) { setOptionValue(optionValue.filter((v) => v.value !== item.value)); } else { setOptionValue([...optionValue, item]); } } else { if (isSelected) { setOptionValue(null); } else { setOptionValue(item); setInputSearch(item.name); setInputFocused(false); } } }, ...p, children: item.name }, item.value)); }); }; return (_jsxs(_Fragment, { children: [renderOptions(filteredOptions), renderOptions(otherOptions, true)] })); }