UNPKG

tdesign-react

Version:
178 lines (174 loc) 6.69 kB
/** * tdesign v1.15.1 * (c) 2025 tdesign * @license MIT */ import { _ as _defineProperty } from '../_chunks/dep-cb0a3966.js'; import { _ as _slicedToArray } from '../_chunks/dep-48805ab8.js'; import React, { forwardRef, useState, useRef, useMemo, useImperativeHandle, useEffect } from 'react'; import classNames from 'classnames'; import { escapeRegExp, isFunction } from 'lodash-es'; import '../_chunks/dep-f53c91cd.js'; import useConfig from '../hooks/useConfig.js'; import HighlightOption from './HighlightOption.js'; import { useLocaleReceiver } from '../locale/LocalReceiver.js'; import { on, off } from '../_util/listener.js'; import { l as log } from '../_chunks/dep-b908e1fe.js'; import '../_chunks/dep-eca3a3de.js'; import '../_chunks/dep-026a4c6b.js'; import '../config-provider/ConfigContext.js'; import '../locale/zh_CN.js'; import '../_chunks/dep-e29214cb.js'; import 'dayjs'; import '../_chunks/dep-3c9ab31a.js'; import '../config-provider/index.js'; import '../config-provider/ConfigProvider.js'; import '../config-provider/type.js'; import '../_chunks/dep-3a09424a.js'; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } var OptionsList = /*#__PURE__*/forwardRef(function (props, ref) { var value = props.value, popupVisible = props.popupVisible, onSelect = props.onSelect; var _useConfig = useConfig(), classPrefix = _useConfig.classPrefix; var _useState = useState(""), _useState2 = _slicedToArray(_useState, 2), active = _useState2[0], setActive = _useState2[1]; var activeIndexRef = useRef(-1); var _useLocaleReceiver = useLocaleReceiver("autoComplete"), _useLocaleReceiver2 = _slicedToArray(_useLocaleReceiver, 1), global = _useLocaleReceiver2[0]; var classes = "".concat(classPrefix, "-select__list"); var optionClasses = ["".concat(classPrefix, "-select-option"), _defineProperty({}, props.sizeClassNames[props.size], props.size)]; var tOptions = useMemo(function () { var options = (props.options || []).map(function (item) { var option = {}; if (typeof item === "string") { option = { text: item, label: item }; } else { if (item.text && typeof item.text !== "string") { log.warn("AutoComplete", "`text` must be a string."); } if (!item.text) { if (typeof item.label === "string") { option = _objectSpread(_objectSpread({}, item), {}, { text: item.label }); } else { log.warn("AutoComplete", "one of `label` and `text` must be a existed string."); } } else { option = item; } } return option; }); if (props.filter) { options = options.filter(function (option) { return props.filter(value, option); }); } else if (props.filterable) { var regExp = new RegExp(escapeRegExp(value), "i"); options = options.filter(function (item) { return regExp.test(item.text); }); } return options; }, [props.options, value, props.filterable]); var onOptionClick = function onOptionClick(e) { var liNode = e.target; while (liNode && liNode.tagName !== "LI") { liNode = liNode.parentNode; } var keyword = liNode.getAttribute("title"); setActive(keyword); onSelect(keyword, { e: e }); }; var onKeyInnerPress = function onKeyInnerPress(e) { if (e.code === "Enter" || e.key === "Enter") { var currentIndex = activeIndexRef.current; if (currentIndex === -1) { return; } onSelect(tOptions[activeIndexRef.current].text, { e: e }); } else { var _tOptions$newIndex; var index = activeIndexRef.current; var newIndex; if (e.code === "ArrowUp" || e.key === "ArrowUp") { newIndex = index - 1 < 0 ? tOptions.length - 1 : index - 1; } else if (e.code === "ArrowDown" || e.key === "ArrowDown") { newIndex = index + 1 >= tOptions.length ? 0 : index + 1; } setActive((_tOptions$newIndex = tOptions[newIndex]) === null || _tOptions$newIndex === void 0 ? void 0 : _tOptions$newIndex.text); } }; var addKeyboardListener = function addKeyboardListener() { on(document, "keydown", onKeyInnerPress); }; var removeKeyboardListener = function removeKeyboardListener() { off(document, "keydown", onKeyInnerPress); }; useImperativeHandle(ref, function () { return { addKeyboardListener: addKeyboardListener, removeKeyboardListener: removeKeyboardListener }; }); useEffect(function () { if (popupVisible) { addKeyboardListener(); } else { removeKeyboardListener(); } return function () { removeKeyboardListener(); }; }, [popupVisible]); useEffect(function () { if (!value) { setActive(""); } }, [value]); useEffect(function () { activeIndexRef.current = tOptions.findIndex(function (item) { return item.text === active; }); }, [active, tOptions]); if (!tOptions.length) { return /* @__PURE__ */React.createElement("div", { className: "".concat(classPrefix, "-auto-complete__panel--empty") }, props.empty || global.empty); } return /* @__PURE__ */React.createElement("ul", { className: classes }, tOptions.map(function (item) { var cls = [].concat(optionClasses); if (item.text === active) { cls.push("".concat(classPrefix, "-select-option--hover")); } var content = (isFunction(item.label) ? item.label() : item.label) || item.text; return /* @__PURE__ */React.createElement("li", { key: item.text, className: classNames(cls), title: item.text, onClick: onOptionClick }, typeof content === "string" && props.highlightKeyword ? /* @__PURE__ */React.createElement(HighlightOption, { content: content, keyword: value }) : content); })); }); OptionsList.displayName = "OptionsList"; export { OptionsList as default }; //# sourceMappingURL=OptionList.js.map