UNPKG

tdesign-vue

Version:
366 lines (362 loc) 15.5 kB
/** * tdesign v1.12.1 * (c) 2025 tdesign * @license MIT */ import { h as helper } from '../_chunks/dep-323b993c.js'; import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { defineComponent, toRefs, inject, computed, onMounted, onBeforeUnmount } from '@vue/composition-api'; import { isFunction } from 'lodash-es'; import { useTNodeJSX } from '../hooks/tnode.js'; import { renderTNodeJSXDefault } from '../utils/render-tnode.js'; import { useConfig, usePrefixClass } from '../config-provider/useConfig.js'; import _Option from './option.js'; import _OptionGroup from './optionGroup.js'; import useVirtualScroll from '../hooks/useVirtualScroll.js'; import { flattenOptions as _flattenOptions } from './util.js'; import '../hooks/render-tnode.js'; import '@babel/runtime/helpers/readOnlyError'; import '@babel/runtime/helpers/typeof'; import 'vue'; 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-c44a474d.js'; import '../_chunks/dep-d639fbd7.js'; import 'dayjs'; import '../_chunks/dep-3c66615e.js'; import '../config-provider/type.js'; import '../_common/js/global-config/t.js'; import '@babel/runtime/helpers/slicedToArray'; import '../utils/ripple.js'; import '../_common/js/utils/setStyle.js'; import '../config.js'; import '../config-provider/config-receiver.js'; import './option-props.js'; import '../checkbox/index.js'; import '../checkbox/group.js'; import '../utils/helper.js'; import '@babel/runtime/helpers/objectWithoutProperties'; import '../checkbox/checkbox.js'; import '../checkbox/props.js'; import '../hooks/index.js'; import '../hooks/slots.js'; import '../hooks/useCommonClassName.js'; import '../hooks/useConfig.js'; import '../hooks/useDefaultValue.js'; import '../hooks/useDestroyOnClose.js'; import '../hooks/useElementLazyRender.js'; import '../_common/js/utils/observe.js'; import '../hooks/useFormDisabled.js'; import '../hooks/useGlobalIcon.js'; import '../hooks/useLazyLoad.js'; import '../hooks/useResizeObserver.js'; import '../hooks/useVirtualScrollNew.js'; import '../hooks/useVModel.js'; import '../hooks/useImagePreviewUrl.js'; import '../_common/js/upload/utils.js'; import '../_common/js/log/log.js'; import '../checkbox/constants.js'; import '../checkbox/store.js'; import '@babel/runtime/helpers/classCallCheck'; import '@babel/runtime/helpers/createClass'; import '../checkbox/hooks/useKeyboardEvent.js'; import '../_common/js/common.js'; import '../checkbox/checkbox-group-props.js'; import '../utils/withInstall.js'; import './style/css.js'; import '../checkbox/type.js'; import './option-group-props.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 sizeClassMap = { small: "s", medium: "m", large: "l" }; var SelectPanel = defineComponent({ name: "TSelectPanel", components: { TOption: _Option, TOptionGroup: _OptionGroup }, props: ["inputValue", "panelTopContent", "panelBottomContent", "size", "options", "empty", "filter", "loading", "loadingText", "multiple", "scroll", "creatable", "filterable"], setup: function setup(props) { var _props$scroll5; var _toRefs = toRefs(props), options = _toRefs.options, inputValue = _toRefs.inputValue; var renderTNode = useTNodeJSX(); var _useConfig = useConfig("select"), t = _useConfig.t, global = _useConfig.global; var selectProvider = inject("tSelect"); var COMPONENT_NAME = usePrefixClass("select"); var panelContentRef = computed(function () { return selectProvider.getOverlayElm(); }); var _ref = props.scroll || {}, type = _ref.type, _ref$rowHeight = _ref.rowHeight, rowHeight = _ref$rowHeight === void 0 ? 28 : _ref$rowHeight, _ref$bufferSize = _ref.bufferSize, bufferSize = _ref$bufferSize === void 0 ? 20 : _ref$bufferSize, _ref$isFixedRowHeight = _ref.isFixedRowHeight, isFixedRowHeight = _ref$isFixedRowHeight === void 0 ? false : _ref$isFixedRowHeight, _ref$threshold = _ref.threshold, threshold = _ref$threshold === void 0 ? 100 : _ref$threshold; var displayOptions = computed(function () { if (!inputValue.value || !(props.filterable || isFunction(props.filter)) || selectProvider.isRemoteSearch) return options.value; var filterMethods = function filterMethods(option) { var _option$label, _option$label$toLower; if (isFunction(props.filter)) { return props.filter("".concat(props.inputValue), option); } if ((option === null || option === void 0 ? void 0 : option.checkAll) === true) return true; return ((_option$label = option.label) === null || _option$label === void 0 || (_option$label$toLower = _option$label.toLowerCase) === null || _option$label$toLower === void 0 ? void 0 : _option$label$toLower.call(_option$label).indexOf("".concat(props.inputValue).toLowerCase())) > -1; }; var res = []; var groupIndex = isCreateOptionShown.value ? 1 : 0; props.options.forEach(function (option) { if (option.group && option.children) { res.push(_objectSpread(_objectSpread({}, option), {}, { children: option.children.filter(filterMethods).reduce(function (pre, cur) { pre.push(_objectSpread(_objectSpread({}, cur), {}, { index: groupIndex })); groupIndex += 1; return pre; }, []) })); } else if (filterMethods(option)) { res.push(_objectSpread(_objectSpread({}, option), {}, { index: groupIndex + res.length })); } }); return res; }); var getDisplayOptions = function getDisplayOptions() { var arr = []; if (isCreateOptionShown.value) { arr.push({ label: String(inputValue.value), value: String(inputValue.value), isCreated: true }); } arr.push.apply(arr, _toConsumableArray(displayOptions.value)); return arr; }; var isCreateOptionShown = computed(function () { return !!(props.creatable && props.filterable && props.inputValue && _flattenOptions(options.value).find(function (v) { return v.label === props.inputValue; }) === void 0); }); var isEmpty = computed(function () { return !(displayOptions.value.length > 0); }); var isVirtual = computed(function () { var _props$scroll, _props$options, _props$scroll2; return ((_props$scroll = props.scroll) === null || _props$scroll === void 0 ? void 0 : _props$scroll.type) === "virtual" && ((_props$options = props.options) === null || _props$options === void 0 ? void 0 : _props$options.length) > (((_props$scroll2 = props.scroll) === null || _props$scroll2 === void 0 ? void 0 : _props$scroll2.threshold) || 100); }); var _ref2 = type === "virtual" ? useVirtualScroll({ container: panelContentRef, data: displayOptions, fixedHeight: isFixedRowHeight, lineHeight: rowHeight, bufferSize: bufferSize, threshold: threshold }) : {}, _ref2$trs = _ref2.trs, trs = _ref2$trs === void 0 ? null : _ref2$trs, _ref2$visibleData = _ref2.visibleData, visibleData = _ref2$visibleData === void 0 ? null : _ref2$visibleData, _ref2$handleScroll = _ref2.handleScroll, handleVirtualScroll = _ref2$handleScroll === void 0 ? null : _ref2$handleScroll, _ref2$scrollHeight = _ref2.scrollHeight, scrollHeight = _ref2$scrollHeight === void 0 ? null : _ref2$scrollHeight, _ref2$translateY = _ref2.translateY, translateY = _ref2$translateY === void 0 ? null : _ref2$translateY, _ref2$handleRowMounte = _ref2.handleRowMounted, handleRowMounted = _ref2$handleRowMounte === void 0 ? null : _ref2$handleRowMounte; var lastScrollY = -1; var onInnerVirtualScroll = function onInnerVirtualScroll(e) { if (!isVirtual.value) { return; } var target = e.target || e.srcElement; var top = target.scrollTop; if (Math.abs(lastScrollY - top) > 5) { handleVirtualScroll(); lastScrollY = top; } else { lastScrollY = -1; } }; onMounted(function () { var _props$scroll3; if (((_props$scroll3 = props.scroll) === null || _props$scroll3 === void 0 ? void 0 : _props$scroll3.type) === "virtual") { var _selectProvider$getOv; (_selectProvider$getOv = selectProvider.getOverlayElm()) === null || _selectProvider$getOv === void 0 || _selectProvider$getOv.addEventListener("scroll", onInnerVirtualScroll); } }); onBeforeUnmount(function () { var _props$scroll4; if (((_props$scroll4 = props.scroll) === null || _props$scroll4 === void 0 ? void 0 : _props$scroll4.type) === "virtual") { var _selectProvider$getOv2; (_selectProvider$getOv2 = selectProvider.getOverlayElm()) === null || _selectProvider$getOv2 === void 0 || _selectProvider$getOv2.removeEventListener("scroll", onInnerVirtualScroll); } }); return { t: t, global: global, isEmpty: isEmpty, renderTNode: renderTNode, selectProvider: selectProvider, isCreateOptionShown: isCreateOptionShown, trs: trs, isVirtual: isVirtual, onInnerVirtualScroll: onInnerVirtualScroll, visibleData: visibleData, scrollHeight: scrollHeight, translateY: translateY, scrollType: (_props$scroll5 = props.scroll) === null || _props$scroll5 === void 0 ? void 0 : _props$scroll5.type, handleRowMounted: handleRowMounted, bufferSize: bufferSize, threshold: threshold, displayOptions: displayOptions, getDisplayOptions: getDisplayOptions, componentName: COMPONENT_NAME }; }, methods: { renderEmptyContent: function renderEmptyContent() { var h = this.$createElement; if (this.empty && typeof this.empty === "string") { return h("div", { "class": "".concat(this.componentName, "__empty") }, [this.empty]); } return renderTNodeJSXDefault(this, "empty", h("div", { "class": "".concat(this.componentName, "__empty") }, [this.t(this.global.empty)])); }, renderLoadingContent: function renderLoadingContent() { var h = this.$createElement; if (this.loadingText && typeof this.loadingText === "string") { return h("div", { "class": "".concat(this.componentName, "__loading-tips") }, [this.loadingText]); } return renderTNodeJSXDefault(this, "loadingText", h("div", { "class": "".concat(this.componentName, "__loading-tips") }, [this.t(this.global.loadingText)])); }, renderCreateOption: function renderCreateOption() { var h = this.$createElement; var on = this.isVirtual ? { onRowMounted: this.handleRowMounted } : {}; return h("ul", { "class": ["".concat(this.componentName, "__create-option"), "".concat(this.componentName, "__list")] }, [h("t-option", helper([{ "attrs": { "multiple": this.multiple, "index": 0, "isCreatedOption": true, "value": this.inputValue, "label": this.inputValue, "trs": this.trs, "scrollType": this.scrollType, "isVirtual": this.isVirtual, "bufferSize": this.bufferSize }, "class": "".concat(this.componentName, "__create-option--special") }, { "on": on }]))]); }, renderOptionsContent: function renderOptionsContent(options) { var _this = this; var h = this.$createElement; var on = this.isVirtual ? { onRowMounted: this.handleRowMounted } : {}; return h("ul", { "class": "".concat(this.componentName, "__list") }, [options.map(function (item, index) { if (item.children) { return h("t-option-group", { "attrs": { "label": item.group, "divider": item.divider } }, [_this.renderOptionsContent(item.children)]); } var scrollProps = _this.isVirtual ? { rowIndex: item.$index, trs: _this.trs, scrollType: _this.scrollType, isVirtual: _this.isVirtual, bufferSize: _this.bufferSize } : { key: index }; return h("t-option", helper([{ "class": item["class"], "style": item.style, "props": _objectSpread({}, _objectSpread(_objectSpread({}, item), scrollProps)), "attrs": { "multiple": _this.multiple }, "scopedSlots": { "default": item.slots }, "key": "".concat(item.$index || "", "_").concat(index) }, { "on": on }])); })]); }, renderPanelContent: function renderPanelContent() { var innerStyle = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var h = this.$createElement; var loading = this.loading; return h("div", { "class": ["".concat(this.componentName, "__dropdown-inner"), "".concat(this.componentName, "__dropdown-inner--size-").concat(sizeClassMap[this.size])], "style": innerStyle }, [this.renderTNode("panelTopContent"), this.isCreateOptionShown && this.renderCreateOption(), loading && this.renderLoadingContent(), !loading && this.isEmpty && !this.isCreateOptionShown && this.renderEmptyContent(), !loading && !this.isEmpty && this.renderOptionsContent(this.isVirtual && this.visibleData ? this.visibleData : this.displayOptions), this.renderTNode("panelBottomContent")]); } }, render: function render() { var h = arguments[0]; if (this.isVirtual) { var cursorTranslate = "translate(0, ".concat(this.scrollHeight, "px)"); var cursorTranslateStyle = { position: "absolute", width: "1px", height: "1px", transition: "transform 0.2s", transform: cursorTranslate, "-ms-transform": cursorTranslate, "-moz-transform": cursorTranslate, "-webkit-transform": cursorTranslate }; var translate = "translate(0, ".concat(this.translateY, "px)"); var virtualStyle = { transform: translate, "-ms-transform": translate, "-moz-transform": translate, "-webkit-transform": translate }; return h("div", [h("div", { "style": _objectSpread({}, cursorTranslateStyle) }), this.renderPanelContent(virtualStyle)]); } return this.renderPanelContent(); } }); export { SelectPanel as default }; //# sourceMappingURL=select-panel.js.map