UNPKG

xdesign-vue-next

Version:

XDesign Component for vue-next

192 lines (188 loc) 7.25 kB
/** * xdesign v1.0.6 * (c) 2023 xdesign * @license MIT */ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { defineComponent, ref, computed, watch, onBeforeUnmount, createVNode, h } from 'vue'; import isFunction from 'lodash/isFunction'; import _HighlightOption from './highlight-option.js'; import log from '../_common/js/log/log.js'; import { usePrefixClass } from '../hooks/useConfig.js'; import { on, off } from '../utils/dom.js'; import isString from 'lodash/isString'; import '../config-provider/useConfig.js'; import 'lodash/cloneDeep'; import '../config-provider/context.js'; import 'lodash/mergeWith'; import 'lodash/merge'; import '../_common/js/global-config/default-config.js'; import '../_common/js/global-config/locale/en_US.js'; import '../_chunks/dep-3a1cce9f.js'; import 'lodash/isArray'; import '../utils/easing.js'; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } var AutoCompleteOptionList = defineComponent({ name: "AutoCompleteOptionList", props: { sizeClassNames: Object, value: String, size: String, options: Array, popupVisible: Boolean, highlightKeyword: Boolean, filterable: Boolean, filter: Function }, emits: ["select"], setup: function setup(props, _ref) { var emit = _ref.emit, slots = _ref.slots, expose = _ref.expose; var active = ref(""); var classPrefix = usePrefixClass(); var classes = computed(function () { return "".concat(classPrefix.value, "-select__list"); }); var optionClasses = computed(function () { return ["".concat(classPrefix.value, "-select-option"), _defineProperty({}, props.sizeClassNames[props.size], props.size)]; }); var tOptions = computed(function () { var options = (props.options || []).map(function (item) { var option = {}; if (isString(item)) { option = { text: item, label: item }; } else { if (item.text && !isString(item.text)) { log.warn("AutoComplete", "`text` must be a string."); } if (!item.text) { if (isString(item.label)) { 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(props.value, option); }); } else if (props.filterable) { var regExp = new RegExp(props.value, "i"); options = options.filter(function (item) { return regExp.test(item.text); }); } return options; }); var onOptionClick = function onOptionClick(e) { var liNode = e.target; while (liNode && liNode.tagName !== "LI") { liNode = liNode.parentNode; } var keyword = liNode.getAttribute("title"); active.value = keyword; emit("select", keyword, { e: e }); }; var onKeyInnerPress = function onKeyInnerPress(e) { if (e.code === "ArrowUp" || e.key === "ArrowUp") { var _tOptions$value$newIn; var index = tOptions.value.findIndex(function (item) { return item.text === active.value; }); var newIndex = index - 1 < 0 ? tOptions.value.length - 1 : index - 1; active.value = (_tOptions$value$newIn = tOptions.value[newIndex]) === null || _tOptions$value$newIn === void 0 ? void 0 : _tOptions$value$newIn.text; } else if (e.code === "ArrowDown" || e.key === "ArrowDown") { var _tOptions$value$_newI; var _index = tOptions.value.findIndex(function (item) { return item.text === active.value; }); var _newIndex = _index + 1 >= tOptions.value.length ? 0 : _index + 1; active.value = (_tOptions$value$_newI = tOptions.value[_newIndex]) === null || _tOptions$value$_newI === void 0 ? void 0 : _tOptions$value$_newI.text; } else if (e.code === "Enter" || e.key === "Enter") { emit("select", active.value, { e: e }); } }; var addKeyboardListener = function addKeyboardListener() { on(document, "keydown", onKeyInnerPress); }; var removeKeyboardListener = function removeKeyboardListener() { off(document, "keydown", onKeyInnerPress); }; expose({ addKeyboardListener: addKeyboardListener, removeKeyboardListener: removeKeyboardListener }); watch(function () { return props.popupVisible; }, function () { if (props.popupVisible) { addKeyboardListener(); } else { removeKeyboardListener(); } }, { immediate: true }); watch(function () { return props.value; }, function () { if (!props.value) { active.value = ""; } }, { immediate: true }); onBeforeUnmount(function () { removeKeyboardListener(); }); return function () { if (!tOptions.value.length) return null; return createVNode("ul", { "class": classes.value }, [tOptions.value.map(function (item) { var cls = _toConsumableArray(optionClasses.value); if (item.text === active.value) { cls.push("".concat(classPrefix.value, "-select-option--hover")); } var labelNode = item.label; if (isFunction(item.label)) { labelNode = item.label(h); } else if (slots.option) { var _slots$option; labelNode = (_slots$option = slots.option) === null || _slots$option === void 0 ? void 0 : _slots$option.call(slots, { option: item }); } var content = labelNode || item.text; return createVNode("li", { "key": item.text, "class": cls, "title": item.text, "onClick": onOptionClick }, [isString(content) && props.highlightKeyword ? createVNode(_HighlightOption, { "content": content, "keyword": props.value }, null) : content]); })]); }; } }); export { AutoCompleteOptionList as default }; //# sourceMappingURL=option-list.js.map