@trail-ui/react
Version:
621 lines (618 loc) • 22.3 kB
JavaScript
import {
Overlay,
Positions
} from "./chunk-DFBZCBP4.mjs";
import {
Input,
Label
} from "./chunk-DYJZEB7Z.mjs";
import {
Text
} from "./chunk-VIVC5TFC.mjs";
// src/select/customSelect.tsx
import { multiselect } from "@trail-ui/theme";
import React, {
useState,
useRef,
useEffect,
forwardRef,
useMemo
} from "react";
import { CheckIcon, ChevronDownIcon, ErrorIcon } from "@trail-ui/icons";
import { Button } from "react-aria-components";
import { clsx } from "@trail-ui/shared-utils";
import { jsx, jsxs } from "react/jsx-runtime";
var Keys = {
Backspace: "Backspace",
Clear: "Clear",
Down: "ArrowDown",
End: "End",
Enter: "Enter",
Escape: "Escape",
Home: "Home",
Left: "ArrowLeft",
PageDown: "PageDown",
PageUp: "PageUp",
Right: "ArrowRight",
Space: " ",
Tab: "Tab",
Up: "ArrowUp"
};
var MenuActions = {
Close: "Close",
CloseSelect: "CloseSelect",
First: "First",
Last: "Last",
Next: "Next",
Open: "Open",
Previous: "Previous",
Select: "Select",
Space: "Space",
Type: "Type"
};
function getActionFromKey(key, menuOpen) {
if (!menuOpen && key === Keys.Down)
return MenuActions.Open;
if (!menuOpen && key === Keys.Enter)
return MenuActions.Open;
if (key === Keys.Down)
return MenuActions.Next;
if (key === Keys.Up)
return MenuActions.Previous;
if (key === Keys.Home)
return MenuActions.First;
if (key === Keys.End)
return MenuActions.Last;
if (key === Keys.Escape)
return MenuActions.Close;
if (key === Keys.Enter)
return MenuActions.CloseSelect;
if (key === Keys.Backspace || key === Keys.Clear || key.length === 1)
return MenuActions.Type;
return void 0;
}
var CustomSelect = forwardRef(
({
options,
label,
labelKey,
valueKey,
id = "select",
onChange,
onTriggerBlur,
onBlur,
classNames,
errorMessage,
description,
errorIcon = /* @__PURE__ */ jsx(
ErrorIcon,
{
className: "h-4 w-4 text-red-800 dark:text-red-600",
role: "img",
"aria-label": "Error",
"aria-hidden": false
}
),
errorId,
isRequired,
isDisabled,
isInvalid,
placeholder = "Select items",
value: selectedValue,
form,
name,
maxOptionsBeforeConversionToComboBox = 6,
isCombobox,
position = Positions.bottom,
popover = { maxHeight: 240 },
onKeyDown,
onInputChange,
isLoading = false,
allowCustomInput = false,
renderCustomLabel,
...otherprops
}, ref) => {
const [isOpen, setIsOpen] = useState(false);
const [searchValue, setSearchValue] = useState("");
const [activeIndex, setActiveIndex] = useState(-1);
const [value, setValue] = useState({ label: "", value: "" });
const listboxRef = useRef(null);
const inputRef = useRef(null);
const localRef = useRef(null);
const buttonRef = useRef(null);
const optionRefs = useRef([]);
const noSearchResultsRef = useRef(null);
const hiddenInputRef = useRef(null);
const refElementForOverlay = useRef(null);
const slots = useMemo(() => multiselect(), []);
const isComponentCombobox = useMemo(() => {
if (isCombobox !== void 0) {
return isCombobox;
} else {
return options.length > maxOptionsBeforeConversionToComboBox;
}
}, [isCombobox, options, maxOptionsBeforeConversionToComboBox]);
function getOptionLabel(option, isView) {
if (isView && renderCustomLabel && option.value) {
return renderCustomLabel == null ? void 0 : renderCustomLabel(option);
}
return (option == null ? void 0 : option[labelKey != null ? labelKey : "label"]) || "";
}
const getOptionValue = (option) => {
return (option == null ? void 0 : option[valueKey != null ? valueKey : "value"]) || "";
};
const resetIndex = () => setActiveIndex(-1);
const filteredOptions = useMemo(() => {
const optList = [];
if (allowCustomInput) {
if (searchValue && options.findIndex(
(o) => getOptionLabel(o, false).toLowerCase() === searchValue.toLowerCase()
) === -1) {
optList.push({ label: searchValue, value: searchValue, isItalic: true });
} else if (selectedValue && options.findIndex((o) => o.value === selectedValue) === -1) {
optList.push({ label: selectedValue, value: selectedValue, isItalic: true });
}
}
return [
...optList,
...options.filter((option) => {
return getOptionLabel(option, false).toString().toLowerCase().includes(searchValue.toLowerCase());
})
];
}, [searchValue, options, selectedValue]);
const toggleDropdown = () => setIsOpen((prevState) => !prevState);
const toggleItem = (option) => {
if (isDisabled)
return;
let newOption = option;
if (option.value === selectedValue) {
newOption = { label: "", value: "" };
}
onChange(newOption.value);
setSearchValue("");
toggleDropdown();
if (inputRef.current) {
inputRef.current.focus();
inputRef.current.value = getOptionLabel(newOption, false);
}
if (buttonRef.current) {
buttonRef.current.focus();
}
if (hiddenInputRef.current) {
hiddenInputRef.current.value = getOptionLabel(newOption, false);
}
};
const handleInputChange = (e) => {
if (isDisabled)
return;
setSearchValue(e.target.value);
if (!isOpen)
toggleDropdown();
if (activeIndex === -1) {
setActiveIndex(0);
}
};
function getUpdatedIndex(current, action) {
switch (action) {
case MenuActions.First:
return 0;
case MenuActions.Last:
return filteredOptions.length;
case MenuActions.Previous:
return (activeIndex - 1 + filteredOptions.length) % filteredOptions.length;
case MenuActions.Next:
return (activeIndex + 1) % filteredOptions.length;
default:
return current;
}
}
const handleKeyDown = (e) => {
var _a, _b, _c, _d, _e, _f;
const action = getActionFromKey(e.key, isOpen);
if (isDisabled) {
e.preventDefault();
e.stopPropagation();
return;
}
if (e.key === Keys.Tab) {
if (isOpen) {
toggleDropdown();
resetIndex();
setSearchValue("");
}
return;
}
if (!action)
return;
if (MenuActions.Type !== action)
e.preventDefault();
switch (action) {
case MenuActions.Next:
case MenuActions.Last:
case MenuActions.First:
case MenuActions.Previous:
const nextIndex = getUpdatedIndex(activeIndex, action);
setActiveIndex(nextIndex);
(_a = noSearchResultsRef.current) == null ? void 0 : _a.focus();
(_c = (_b = optionRefs.current[nextIndex]) == null ? void 0 : _b.current) == null ? void 0 : _c.focus();
break;
case MenuActions.Type:
if (inputRef.current)
inputRef.current.focus();
break;
case MenuActions.CloseSelect:
if (activeIndex >= 0) {
(_d = optionRefs.current[activeIndex].current) == null ? void 0 : _d.click();
}
break;
case MenuActions.Close:
handleOnClick({ detail: 1 });
setSearchValue("");
resetIndex();
(_e = inputRef.current) == null ? void 0 : _e.focus();
(_f = buttonRef.current) == null ? void 0 : _f.focus();
break;
case MenuActions.Open:
handleOnClick({ detail: 1 });
break;
}
};
const handleOnClick = (e) => {
if (!isDisabled && (e == null ? void 0 : e.detail) === 1) {
toggleDropdown();
if (isOpen) {
resetIndex();
}
}
};
function mergeRefs(...refs) {
return (value2) => {
for (const ref2 of refs) {
if (!ref2)
continue;
if (typeof ref2 === "function") {
ref2(value2);
} else {
ref2.current = value2;
}
}
};
}
function onComponentBlur(e) {
var _a;
const selectedOption = filteredOptions.find((o) => o.value === selectedValue);
if (e.relatedTarget && !((_a = localRef.current) == null ? void 0 : _a.contains(e.relatedTarget))) {
if (selectedOption) {
if (inputRef.current) {
inputRef.current.value = getOptionLabel(selectedOption, false);
}
if (hiddenInputRef.current) {
hiddenInputRef.current.value = getOptionLabel(selectedOption, false);
}
}
onBlur == null ? void 0 : onBlur(e);
}
}
useEffect(() => {
const handleClickOutside = (event) => {
if (localRef.current && !localRef.current.contains(event.target)) {
setIsOpen(false);
}
};
document.addEventListener("mousedown", handleClickOutside, true);
return () => document.removeEventListener("mousedown", handleClickOutside, true);
}, []);
useEffect(() => {
const index = options.findIndex((item) => item.value === selectedValue);
const newValue = index === -1 ? allowCustomInput ? { label: selectedValue, value: selectedValue, isItalic: true } : { label: "", value: "" } : options[index];
setValue(newValue);
if (inputRef.current) {
inputRef.current.value = getOptionLabel(newValue, false);
}
if (hiddenInputRef.current) {
hiddenInputRef.current.value = getOptionLabel(newValue, false);
}
}, [selectedValue, options]);
useEffect(() => {
var _a;
if (isOpen) {
optionRefs.current = filteredOptions.map(
(_, i) => optionRefs.current[i] || React.createRef()
);
if (filteredOptions.length > 1 && !isCombobox) {
setActiveIndex(0);
}
}
(_a = buttonRef.current) == null ? void 0 : _a.setAttribute("role", "combobox");
}, [isOpen]);
useEffect(() => {
var _a;
if (filteredOptions.length > 0) {
resetIndex();
}
if (isRequired) {
(_a = buttonRef.current) == null ? void 0 : _a.setAttribute("aria-required", String(isRequired));
}
}, [filteredOptions]);
useEffect(() => {
var _a, _b;
if (isOpen && activeIndex >= 0) {
requestAnimationFrame(() => {
var _a2, _b2;
(_b2 = (_a2 = optionRefs.current[activeIndex]) == null ? void 0 : _a2.current) == null ? void 0 : _b2.scrollIntoView({ block: "nearest" });
});
}
if (isOpen)
(_a = buttonRef.current) == null ? void 0 : _a.setAttribute("aria-activedescendant", `${id}-option-${activeIndex}`);
else
(_b = buttonRef.current) == null ? void 0 : _b.removeAttribute("aria-activedescendant");
}, [activeIndex]);
useEffect(() => {
var _a;
(_a = buttonRef.current) == null ? void 0 : _a.setAttribute("data-invalid", `${isInvalid}`);
}, [isInvalid, options]);
return /* @__PURE__ */ jsxs(
"div",
{
ref: localRef,
className: slots.base({ class: classNames == null ? void 0 : classNames.base }),
onBlur: onComponentBlur,
children: [
/* @__PURE__ */ jsx(
Label,
{
id: `${id}-label`,
isDisabled,
requiredHint: isRequired,
className: slots.label({ class: classNames == null ? void 0 : classNames.label }),
children: label
}
),
/* @__PURE__ */ jsx(
"input",
{
ref: hiddenInputRef,
type: "hidden",
name,
value: getOptionValue(value),
form,
required: isRequired
}
),
/* @__PURE__ */ jsxs(
"div",
{
ref: refElementForOverlay,
className: slots.inputWrapper({ className: classNames == null ? void 0 : classNames.inputWrapper }),
children: [
isComponentCombobox ? /* @__PURE__ */ jsx(
Input,
{
id,
ref: mergeRefs(inputRef, ref),
autoComplete: "off",
"aria-busy": isLoading,
"aria-autocomplete": "list",
"aria-labelledby": `${id}-label`,
onBlur: onTriggerBlur,
className: slots.input({ class: classNames == null ? void 0 : classNames.input }),
onChange: (e) => {
handleInputChange(e);
onInputChange == null ? void 0 : onInputChange(e);
},
onKeyDown: (e) => {
handleKeyDown(e);
onKeyDown == null ? void 0 : onKeyDown(e);
},
onClick: handleOnClick,
placeholder: !getOptionValue(value) ? placeholder : "",
"aria-expanded": isOpen,
"aria-controls": isOpen ? "select-combobox" : void 0,
"aria-activedescendant": isOpen && activeIndex >= 0 ? `${id}-option-${activeIndex}` : void 0,
disabled: isDisabled,
"aria-invalid": isInvalid,
"aria-required": isRequired,
role: "combobox",
"aria-describedby": errorId || "",
"aria-haspopup": "listbox",
...otherprops
}
) : /* @__PURE__ */ jsx(
Button,
{
id,
ref: mergeRefs(buttonRef, ref),
onKeyDown: (e) => {
handleKeyDown(e);
onKeyDown == null ? void 0 : onKeyDown(e);
},
onClick: handleOnClick,
onBlur: onTriggerBlur,
className: clsx(
slots.input({ class: classNames == null ? void 0 : classNames.input }),
"text-left",
value.value ? "text-neutral-900" : "text-neutral-600"
),
"aria-expanded": isOpen,
"aria-controls": isOpen ? "select-listbox" : void 0,
"aria-activedescendant": isOpen && activeIndex >= 0 ? `${id}-option-${activeIndex}` : void 0,
"aria-invalid": isInvalid,
"aria-required": isRequired,
"aria-describedby": errorId || "",
"aria-labelledby": `${id}-label`,
isDisabled,
"aria-haspopup": "listbox",
...otherprops,
children: getOptionLabel(value, true) || placeholder
}
),
/* @__PURE__ */ jsx(
ChevronDownIcon,
{
height: 24,
width: 24,
onKeyDown: handleKeyDown,
onClick: (e) => {
var _a, _b;
handleOnClick(e);
(_a = inputRef.current) == null ? void 0 : _a.focus();
(_b = buttonRef.current) == null ? void 0 : _b.focus();
},
className: `${isOpen ? "rotate-180" : ""} cursor-pointer`
}
)
]
}
),
errorMessage ? /* @__PURE__ */ jsxs(
Text,
{
id: errorId,
slot: "errorMessage",
elementType: "div",
className: slots.errorMessage({
class: classNames == null ? void 0 : classNames.errorMessage
}),
children: [
errorIcon,
/* @__PURE__ */ jsx("span", { children: errorMessage })
]
}
) : description ? /* @__PURE__ */ jsx(
Text,
{
slot: "description",
elementType: "div",
className: slots.description({ class: classNames == null ? void 0 : classNames.description }),
children: /* @__PURE__ */ jsx("span", { children: description })
}
) : null,
isOpen && /* @__PURE__ */ jsx(
Overlay,
{
referenceElement: refElementForOverlay,
position,
style: popover,
offset: { x: 8, y: 8 },
children: /* @__PURE__ */ jsx(
"ul",
{
ref: listboxRef,
role: "listbox",
id: `select-${isComponentCombobox ? "combobox " : "listbox"}`,
"aria-label": label,
"aria-busy": isLoading,
className: slots.popover({ className: classNames == null ? void 0 : classNames.popover }),
children: isLoading ? /* @__PURE__ */ jsx(
"li",
{
id: `${id}-option-loading`,
className: clsx(
slots.listboxItem({
className: classNames == null ? void 0 : classNames.listboxItem
})
),
children: /* @__PURE__ */ jsx(
"span",
{
className: slots.listBoxItemLabel({
className: classNames == null ? void 0 : classNames.listBoxItemLabel
}),
children: "Loading ..."
}
)
}
) : filteredOptions.length > 0 ? filteredOptions.map((option, index) => {
const isActive = index === activeIndex;
return /* @__PURE__ */ jsxs(
"li",
{
ref: optionRefs.current[index],
role: "option",
id: `${id}-option-${index}`,
"aria-selected": getOptionValue(value) === getOptionValue(option),
tabIndex: isActive ? 0 : -1,
className: clsx(
slots.listboxItem({
className: classNames == null ? void 0 : classNames.listboxItem
}),
(option == null ? void 0 : option.isItalic) ? "italic" : "",
`${isActive ? "border-purple-600 bg-purple-50" : "border-transparent"} ${getOptionValue(value) === getOptionValue(option) ? "font-medium" : ""}`
),
onClick: () => toggleItem(option),
onKeyDown: handleKeyDown,
onMouseEnter: () => {
var _a;
setActiveIndex(index);
(_a = optionRefs.current[index].current) == null ? void 0 : _a.focus();
},
children: [
/* @__PURE__ */ jsx(
"span",
{
className: slots.listBoxItemLabel({
className: classNames == null ? void 0 : classNames.listBoxItemLabel
}),
children: getOptionLabel(option, true)
}
),
getOptionValue(value) === getOptionValue(option) && /* @__PURE__ */ jsx("span", { className: "flex items-center pl-1.5", children: /* @__PURE__ */ jsx(CheckIcon, { className: "h-5 w-5 text-purple-600" }) })
]
},
getOptionValue(option)
);
}) : filteredOptions.length === 0 && options.length === 0 ? /* @__PURE__ */ jsx(
"li",
{
id: `${id}-option-no-data`,
className: clsx(
slots.listboxItem({
className: classNames == null ? void 0 : classNames.listboxItem
})
),
children: /* @__PURE__ */ jsx(
"span",
{
className: slots.listBoxItemLabel({
className: classNames == null ? void 0 : classNames.listBoxItemLabel
}),
children: "No Data"
}
)
}
) : filteredOptions.length === 0 && options.length !== 0 ? /* @__PURE__ */ jsx(
"li",
{
id: `${id}-option-no-data`,
ref: noSearchResultsRef,
className: clsx(
slots.listboxItem({
className: classNames == null ? void 0 : classNames.listboxItem
})
),
tabIndex: 0,
children: /* @__PURE__ */ jsx(
"span",
{
className: slots.listBoxItemLabel({
className: classNames == null ? void 0 : classNames.listBoxItemLabel
}),
children: "No Search Results"
}
)
}
) : ""
}
)
}
)
]
}
);
}
);
CustomSelect.displayName = "CustomSelectComponent";
var customSelect_default = CustomSelect;
export {
customSelect_default
};