tdesign-vue
Version:
211 lines (207 loc) • 7.54 kB
JavaScript
/**
* tdesign v1.14.1
* (c) 2025 tdesign
* @license MIT
*/
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, ref, computed, watch, onBeforeUnmount } from '@vue/composition-api';
import { escapeRegExp, isFunction } from 'lodash-es';
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 { useConfig } from '../config-provider/useConfig.js';
import '../config-provider/context.js';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-ba613a02.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-fdb1b253.js';
import 'dayjs';
import '../_common/js/global-config/t.js';
import '@babel/runtime/helpers/slicedToArray';
import 'vue';
import 'raf';
import '../utils/easing.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 AutoCompleteOptionList = defineComponent({
name: "AutoCompleteOptionList",
props: {
sizeClassNames: Object,
value: String,
size: String,
options: Array,
popupVisible: Boolean,
highlightKeyword: Boolean,
filterable: Boolean,
filter: Function,
empty: [String, Function]
},
setup: function setup(props, _ref) {
var emit = _ref.emit;
var active = ref("");
var classPrefix = usePrefixClass();
var _useConfig = useConfig("autoComplete"),
globalConfig = _useConfig.globalConfig;
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 (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(props.value, option);
});
} else if (props.filterable) {
var regExp = new RegExp(escapeRegExp(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 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[newIndex].text;
} else if (e.code === "ArrowDown" || e.key === "ArrowDown") {
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[_newIndex].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);
};
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 {
globalConfig: globalConfig,
classes: classes,
classPrefix: classPrefix,
optionClasses: optionClasses,
active: active,
tOptions: tOptions,
addKeyboardListener: addKeyboardListener,
removeKeyboardListener: removeKeyboardListener,
onOptionClick: onOptionClick
};
},
render: function render() {
var _this = this;
var h = arguments[0];
if (!this.tOptions.length) {
return h("div", {
"class": "".concat(this.classPrefix, "-auto-complete__panel--empty")
}, [this.empty || this.globalConfig.empty]);
}
return h("ul", {
"class": this.classes
}, [this.tOptions.map(function (item) {
var cls = _toConsumableArray(_this.optionClasses);
if (item.text === _this.active) {
cls.push("".concat(_this.classPrefix, "-select-option--hover"));
}
var labelNode = item.label;
if (isFunction(item.label)) {
labelNode = item.label(h);
} else if (_this.$scopedSlots.option) {
var _this$$scopedSlots$op, _this$$scopedSlots;
labelNode = (_this$$scopedSlots$op = (_this$$scopedSlots = _this.$scopedSlots).option) === null || _this$$scopedSlots$op === void 0 ? void 0 : _this$$scopedSlots$op.call(_this$$scopedSlots, {
option: item
});
}
var content = labelNode || item.text;
return h("li", {
"key": item.text,
"class": cls,
"attrs": {
"title": item.text
},
"on": {
"click": _this.onOptionClick
}
}, [typeof content === "string" && _this.highlightKeyword ? h(_HighlightOption, {
"attrs": {
"content": content,
"keyword": _this.value
}
}) : content]);
})]);
}
});
export { AutoCompleteOptionList as default };
//# sourceMappingURL=option-list.js.map