UNPKG

xdesign-vue-next

Version:

XDesign Component for vue-next

620 lines (616 loc) 26.9 kB
/** * xdesign v1.0.6 * (c) 2023 xdesign * @license MIT */ import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties'; import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { defineComponent, toRefs, ref, computed, provide, watch, nextTick, createVNode, mergeProps } from 'vue'; import pick from 'lodash/pick'; import isArray from 'lodash/isArray'; import isFunction from 'lodash/isFunction'; import debounce from 'lodash/debounce'; import cloneDeep from 'lodash/cloneDeep'; import get from 'lodash/get'; import intersection from 'lodash/intersection'; import FakeArrow from '../common-components/fake-arrow.js'; import { SelectInput } from '../select-input/index.js'; import SelectPanel from './select-panel.js'; import props from './props.js'; import { useFormDisabled } from '../form/hooks.js'; import useDefaultValue from '../hooks/useDefaultValue.js'; import useVModel from '../hooks/useVModel.js'; import { useTNodeJSX } from '../hooks/tnode.js'; import { usePrefixClass } from '../hooks/useConfig.js'; import { getSingleContent, getMultipleContent, selectInjectKey, getNewMultipleValue } from './helper.js'; import { useSelectOptions } from './hooks/useSelectOptions.js'; import { useConfig } from '../config-provider/useConfig.js'; import '../select-input/select-input.js'; import '../popup/index.js'; import '../popup/popup.js'; import '@popperjs/core'; import 'lodash/isObject'; import 'lodash/isString'; import '../utils/dom.js'; import '../_chunks/dep-3a1cce9f.js'; import '../utils/easing.js'; import '../_common/js/utils/set-style.js'; import '../popup/container.js'; import '../popup/props.js'; import '../hooks/useResizeObserver.js'; import '../utils/withInstall.js'; 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 'lodash/kebabCase'; import 'lodash/camelCase'; import '../utils/render-tnode.js'; import 'lodash/isEmpty'; import '../select-input/props.js'; import '../select-input/useSingle.js'; import '../input/index.js'; import '../input/input.js'; import '../_chunks/dep-fb0628ce.js'; import '../_chunks/dep-eade0dcf.js'; import '../input/props.js'; import '../input/useInput.js'; import '../form/const.js'; import '../input/useLengthLimit.js'; import '../_common/js/log/log.js'; import '../_common/js/utils/helper.js'; import 'lodash/isNull'; import 'lodash/isUndefined'; import 'lodash/isNumber'; import '../input/useInputEventHandler.js'; import '../input/useInputWidth.js'; import '../input/input-group.js'; import '../input/input-group-props.js'; import '../loading/index.js'; import '../loading/directive.js'; import '../loading/plugin.js'; import '../loading/loading.js'; import '../loading/icon/gradient.js'; import '../_common/js/loading/circle-adapter.js'; import '../loading/props.js'; import '../hooks/useTeleport.js'; import '../select-input/useMultiple.js'; import '../tag-input/index.js'; import '../tag-input/tag-input.js'; import '../tag-input/props.js'; import '../tag-input/hooks/useTagScroll.js'; import '../tag-input/useTagList.js'; import '../tag/index.js'; import '../tag/tag.js'; import '../_chunks/dep-ae5efda0.js'; import '../tag/props.js'; import '../tag/check-tag.js'; import '../tag/check-tag-props.js'; import '../tag-input/hooks/useHover.js'; import '../tag-input/hooks/useDragSorter.js'; import '../select-input/useOverlayInnerStyle.js'; import '@babel/runtime/helpers/typeof'; import 'lodash/omit'; import './option.js'; import './option-props.js'; import '../checkbox/index.js'; import '../checkbox/checkbox.js'; import '../checkbox/props.js'; import '../hooks/useRipple.js'; import '../hooks/useKeepAnimation.js'; import '../utils/set-style.js'; import '../checkbox/constants.js'; import '../checkbox/hooks/useCheckboxLazyLoad.js'; import '../_common/js/utils/observe.js'; import '../checkbox/hooks/useKeyboard.js'; import '../hooks/useDisabled.js'; import 'lodash/isBoolean'; import '../checkbox/group.js'; import '../checkbox/checkbox-group-props.js'; import '../hooks/slot.js'; import './optionGroup.js'; import './option-group-props.js'; import './hooks/usePanelVirtualScroll.js'; import '../hooks/useVirtualScroll.js'; var _excluded = ["overlayClassName"]; function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } 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 _Select = defineComponent({ name: "XSelect", props: _objectSpread({}, props), setup: function setup(props2, _ref) { var slots = _ref.slots; var classPrefix = usePrefixClass(); var disabled = useFormDisabled(); var renderTNodeJSX = useTNodeJSX(); var COMPONENT_NAME = usePrefixClass("select"); var _useConfig = useConfig("select"), globalConfig = _useConfig.globalConfig, t = _useConfig.t; var _toRefs = toRefs(props2), popupVisible = _toRefs.popupVisible, inputValue = _toRefs.inputValue, modelValue = _toRefs.modelValue, value = _toRefs.value; var _useDefaultValue = useDefaultValue(inputValue, props2.defaultInputValue, props2.onInputChange, "inputValue"), _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2), innerInputValue = _useDefaultValue2[0], setInputValue = _useDefaultValue2[1]; var _useVModel = useVModel(value, modelValue, props2.defaultValue, props2.onChange), _useVModel2 = _slicedToArray(_useVModel, 2), orgValue = _useVModel2[0], setOrgValue = _useVModel2[1]; var selectPanelRef = ref(null); var selectInputRef = ref(null); var keys = computed(function () { var _props2$keys, _props2$keys2, _props2$keys3; return { label: ((_props2$keys = props2.keys) === null || _props2$keys === void 0 ? void 0 : _props2$keys.label) || "label", value: ((_props2$keys2 = props2.keys) === null || _props2$keys2 === void 0 ? void 0 : _props2$keys2.value) || "value", disabled: ((_props2$keys3 = props2.keys) === null || _props2$keys3 === void 0 ? void 0 : _props2$keys3.disabled) || "disabled" }; }); var _useSelectOptions = useSelectOptions(props2, keys, innerInputValue), options = _useSelectOptions.options, optionsMap = _useSelectOptions.optionsMap, optionsList = _useSelectOptions.optionsList, optionsCache = _useSelectOptions.optionsCache, displayOptions = _useSelectOptions.displayOptions; var innerValue = computed(function () { if (orgValue.value === void 0) { return props2.multiple ? [] : void 0; } if (props2.valueType === "object") { return !props2.multiple ? orgValue.value[keys.value.value] : orgValue.value.map(function (option) { return option[keys.value.value]; }); } return orgValue.value; }); var setInnerValue = function setInnerValue(newVal, context) { if (props2.valueType === "object") { var _keys$value = keys.value, value2 = _keys$value.value, label = _keys$value.label; var getOption = function getOption(val) { var _ref2; if (val === void 0) { return void 0; } var option = optionsMap.value.get(val); return _ref2 = {}, _defineProperty(_ref2, value2, get(option, value2)), _defineProperty(_ref2, label, get(option, label)), _ref2; }; newVal = props2.multiple ? newVal.map(function (val) { return getOption(val); }) : getOption(newVal); } if (newVal === orgValue.value) return; setOrgValue(newVal, _objectSpread({ selectedOptions: getSelectedOptions(newVal) }, context)); }; var _useDefaultValue3 = useDefaultValue(popupVisible, false, function (visible, context) { var _props2$onPopupVisibl; (_props2$onPopupVisibl = props2.onPopupVisibleChange) === null || _props2$onPopupVisibl === void 0 ? void 0 : _props2$onPopupVisibl.call(props2, visible, context); }, "popupVisible"), _useDefaultValue4 = _slicedToArray(_useDefaultValue3, 2), innerPopupVisible = _useDefaultValue4[0], setInnerPopupVisible = _useDefaultValue4[1]; var placeholderText = computed(function () { var _ref3; return (_ref3 = !props2.multiple && innerPopupVisible.value && getSingleContent(innerValue.value, optionsMap) || props2.placeholder) !== null && _ref3 !== void 0 ? _ref3 : t(globalConfig.value.placeholder); }); var displayText = computed(function () { return props2.multiple ? getMultipleContent(innerValue.value, optionsMap) : getSingleContent(innerValue.value, optionsMap); }); var valueDisplayParams = computed(function () { var val = props2.multiple && isArray(innerValue.value) ? innerValue.value.map(function (value2) { var _optionsMap$value$get; return { value: value2, label: (_optionsMap$value$get = optionsMap.value.get(value2)) === null || _optionsMap$value$get === void 0 ? void 0 : _optionsMap$value$get.label }; }) : innerValue.value; var params = { value: val, onClose: props2.multiple ? function (index) { return removeTag(index); } : function () {} }; if (props2.minCollapsedNum && props2.multiple) { var _val$slice; return _objectSpread(_objectSpread({}, params), {}, { displayValue: val === null || val === void 0 || (_val$slice = val.slice) === null || _val$slice === void 0 ? void 0 : _val$slice.call(val, 0, props2.minCollapsedNum) }); } return params; }); var isFilterable = computed(function () { return Boolean(props2.filterable || globalConfig.value.filterable || isFunction(props2.filter)); }); var removeTag = function removeTag(index, e) { var _props2$onRemove; e && e.stopPropagation(); var selectValue = cloneDeep(innerValue.value); var value2 = selectValue[index]; selectValue.splice(index, 1); setInnerValue(selectValue, { selectedOptions: getSelectedOptions(selectValue), trigger: "tag-remove", e: e }); (_props2$onRemove = props2.onRemove) === null || _props2$onRemove === void 0 ? void 0 : _props2$onRemove.call(props2, { value: value2, data: optionsMap.value.get(value2), e: e }); }; var handleCreate = function handleCreate() { var _props2$onCreate; if (!innerInputValue.value) return; (_props2$onCreate = props2.onCreate) === null || _props2$onCreate === void 0 ? void 0 : _props2$onCreate.call(props2, innerInputValue.value); setInputValue(""); }; var hoverIndex = ref(-1); var handleKeyDown = function handleKeyDown(e) { var _optionsList$value$ne, _optionsList$value$ne2; var optionsListLength = displayOptions.value.length; var newIndex = hoverIndex.value; switch (e.code) { case "ArrowUp": e.preventDefault(); if (hoverIndex.value === -1) { newIndex = 0; } else if (hoverIndex.value === 0) { newIndex = optionsListLength - 1; } else { newIndex--; } if ((_optionsList$value$ne = optionsList.value[newIndex]) !== null && _optionsList$value$ne !== void 0 && _optionsList$value$ne.disabled) { newIndex--; } hoverIndex.value = newIndex; break; case "ArrowDown": e.preventDefault(); if (hoverIndex.value === -1 || hoverIndex.value === optionsListLength - 1) { newIndex = 0; } else { newIndex++; } if ((_optionsList$value$ne2 = optionsList.value[newIndex]) !== null && _optionsList$value$ne2 !== void 0 && _optionsList$value$ne2.disabled) { newIndex++; } hoverIndex.value = newIndex; break; case "Enter": if (hoverIndex.value === -1) break; if (!innerPopupVisible.value) { setInnerPopupVisible(true, { e: e }); break; } if (!props2.multiple) { var selectedOptions = getSelectedOptions(optionsList.value[hoverIndex.value].value); setInnerValue(optionsList.value[hoverIndex.value].value, { option: selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions[0], selectedOptions: getSelectedOptions(optionsList.value[hoverIndex.value].value), trigger: "check", e: e }); setInnerPopupVisible(false, { e: e }); } else { var _optionsList$value$ho; if (hoverIndex.value === -1) return; var optionValue = (_optionsList$value$ho = optionsList.value[hoverIndex.value]) === null || _optionsList$value$ho === void 0 ? void 0 : _optionsList$value$ho.value; if (!optionValue) return; var newValue = getNewMultipleValue(innerValue.value, optionValue); var _selectedOptions = getSelectedOptions(newValue.value); setInnerValue(newValue.value, { option: _selectedOptions.find(function (v) { return v.value == optionValue; }), selectedOptions: _selectedOptions, trigger: newValue.isCheck ? "check" : "uncheck", e: e }); } break; case "Escape": setInnerPopupVisible(false, { e: e }); break; } }; var popupContentRef = computed(function () { var _selectInputRef$value; return (_selectInputRef$value = selectInputRef.value) === null || _selectInputRef$value === void 0 ? void 0 : _selectInputRef$value.popupRef.getOverlay(); }); var optionalList = computed(function () { return optionsList.value.filter(function (item) { return !item.disabled && !item["check-all"] && !item.checkAll; }); }); var getSelectedOptions = function getSelectedOptions() { var selectValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : innerValue.value; return optionsList.value.filter(function (option) { if (option.checkAll) return; if (isArray(selectValue)) return selectValue.includes(option.value); return selectValue === option.value; }); }; var onCheckAllChange = function onCheckAllChange(checked) { if (!props2.multiple) return; var value2 = checked ? optionalList.value.map(function (option) { return option.value; }) : []; setInnerValue(value2, { selectedOptions: getSelectedOptions(value2), trigger: checked ? "check" : "clear" }); }; var intersectionLen = computed(function () { var values = optionalList.value.map(function (item) { return item.value; }); var n = intersection(innerValue.value, values); return n.length; }); var isCheckAll = computed(function () { return intersectionLen.value === optionalList.value.length; }); var indeterminate = computed(function () { return !isCheckAll.value && intersectionLen.value !== 0; }); var SelectProvide = computed(function () { return { max: props2.max, multiple: props2.multiple, hoverIndex: hoverIndex.value, selectValue: innerValue.value, reserveKeyword: props2.reserveKeyword, handleValueChange: setInnerValue, handlerInputChange: setInputValue, handlePopupVisibleChange: setInnerPopupVisible, handleCreate: handleCreate, size: props2.size, popupContentRef: popupContentRef, indeterminate: indeterminate.value, isCheckAll: isCheckAll.value, onCheckAllChange: onCheckAllChange, getSelectedOptions: getSelectedOptions, displayOptions: displayOptions.value }; }); provide(selectInjectKey, SelectProvide); var checkValueInvalid = function checkValueInvalid() { if (!props2.multiple && isArray(orgValue.value)) { setOrgValue(void 0, { selectedOptions: [], trigger: "default" }); } if (props2.multiple && !isArray(orgValue.value)) { setOrgValue([], { selectedOptions: [], trigger: "default" }); } }; var handleSearch = debounce(function (value2, _ref4) { var _props2$onSearch; var e = _ref4.e; (_props2$onSearch = props2.onSearch) === null || _props2$onSearch === void 0 ? void 0 : _props2$onSearch.call(props2, "".concat(value2), { e: e }); }, 300); var addCache = function addCache(val) { if (props2.multiple) { var newCache = []; var _iterator = _createForOfIteratorHelper(val || []), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var item = _step.value; var option = optionsMap.value.get(item); if (option) { newCache.push(option); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } optionsCache.value = Array.from( /* @__PURE__ */new Set([].concat(newCache, _toConsumableArray(optionsCache.value)))); } else { var _option = optionsMap.value.get(val); if (_option) { optionsCache.value = Array.from( /* @__PURE__ */new Set([_option].concat(_toConsumableArray(optionsCache.value)))); } } }; watch(orgValue, function (val) { checkValueInvalid(); nextTick(function () { addCache(val); }); }, { immediate: true }); watch(function () { return props2.multiple; }, function () { checkValueInvalid(); }); watch(innerPopupVisible, function (value2) { if (value2) { hoverIndex.value = -1; } else { if (innerInputValue.value) { setInputValue(""); } } }); var updateScrollTop = function updateScrollTop(content) { var _selectPanelRef$value; if (!selectPanelRef.value) { return; } var firstSelectedNode = (_selectPanelRef$value = selectPanelRef.value) === null || _selectPanelRef$value === void 0 || (_selectPanelRef$value = _selectPanelRef$value.innerRef) === null || _selectPanelRef$value === void 0 ? void 0 : _selectPanelRef$value.querySelector(".".concat(classPrefix.value, "-is-selected")); nextTick(function () { if (firstSelectedNode && content) { var _getComputedStyle = getComputedStyle(firstSelectedNode), paddingBottom = _getComputedStyle.paddingBottom; var _getComputedStyle2 = getComputedStyle(content), marginBottom = _getComputedStyle2.marginBottom; var elementBottomHeight = parseInt(paddingBottom, 10) + parseInt(marginBottom, 10); var updateValue = firstSelectedNode.offsetTop - content.offsetTop - (content.clientHeight - firstSelectedNode.clientHeight) + elementBottomHeight; content.scrollTop = updateValue; } }); }; provide("updateScrollTop", updateScrollTop); return function () { var _ref5 = props2.popupProps || {}, overlayClassName = _ref5.overlayClassName, restPopupProps = _objectWithoutProperties(_ref5, _excluded); return createVNode("div", { "class": "".concat(COMPONENT_NAME.value, "__wrap") }, [createVNode(SelectInput, mergeProps({ autoWidth: props2.autoWidth, readonly: props2.readonly, borderless: props2.borderless, multiple: props2.multiple, clearable: props2.clearable, loading: props2.loading, status: props2.status, tips: props2.tips, minCollapsedNum: props2.minCollapsedNum, autofocus: props2.autofocus, suffix: props2.suffix }, { "ref": selectInputRef, "class": COMPONENT_NAME.value, "value": displayText.value, "disabled": disabled.value, "popupVisible": innerPopupVisible.value, "inputValue": innerPopupVisible.value ? innerInputValue.value : "", "placeholder": "".concat(placeholderText.value), "allowInput": isFilterable.value, "collapsed-items": props2.collapsedItems, "inputProps": _objectSpread(_objectSpread({ size: props2.size, autofocus: props2.autofocus }, props2.inputProps), {}, { onkeydown: handleKeyDown }), "tagInputProps": _objectSpread({ size: props2.size }, props2.tagInputProps), "onTagChange": function onTagChange(val, ctx) { removeTag(ctx.index); }, "tagProps": _objectSpread({}, props2.tagProps), "popupProps": _objectSpread({ overlayClassName: ["".concat(COMPONENT_NAME.value, "__dropdown"), overlayClassName] }, restPopupProps), "label": props2.label, "prefixIcon": props2.prefixIcon, "suffix": props2.suffix, "suffixIcon": function suffixIcon() { if (props2.suffixIcon || slots.suffixIcon) { return renderTNodeJSX("suffixIcon"); } return props2.showArrow && createVNode(FakeArrow, { "overlayClassName": "".concat(COMPONENT_NAME.value, "__right-icon"), "isActive": innerPopupVisible.value }, null); }, "valueDisplay": function valueDisplay() { return renderTNodeJSX("valueDisplay", { params: valueDisplayParams.value }); }, "onPopupVisibleChange": function onPopupVisibleChange(val, context) { setInnerPopupVisible(val, context); }, "onInputChange": function onInputChange(value2, context) { if (value2) { setInnerPopupVisible(true, { e: context.e }); } setInputValue(value2); handleSearch("".concat(value2), { e: context.e }); }, "onClear": function onClear(_ref6) { var _props2$onClear; var e = _ref6.e; setInnerValue(props2.multiple ? [] : void 0, { option: null, selectedOptions: getSelectedOptions(props2.multiple ? [] : void 0), trigger: "clear", e: e }); (_props2$onClear = props2.onClear) === null || _props2$onClear === void 0 ? void 0 : _props2$onClear.call(props2, { e: e }); }, "onEnter": function onEnter(inputValue2, _ref7) { var _props2$onEnter; var e = _ref7.e; (_props2$onEnter = props2.onEnter) === null || _props2$onEnter === void 0 ? void 0 : _props2$onEnter.call(props2, { inputValue: "".concat(innerInputValue.value), e: e, value: innerValue.value }); handleCreate(); }, "onBlur": function onBlur(inputValue2, _ref8) { var _props2$onBlur; var e = _ref8.e; (_props2$onBlur = props2.onBlur) === null || _props2$onBlur === void 0 ? void 0 : _props2$onBlur.call(props2, { e: e, value: innerValue.value }); }, "onFocus": function onFocus(inputValue2, _ref9) { var _props2$onFocus; var e = _ref9.e; (_props2$onFocus = props2.onFocus) === null || _props2$onFocus === void 0 ? void 0 : _props2$onFocus.call(props2, { e: e, value: innerValue.value }); } }, props2.selectInputProps), { label: slots.label, prefixIcon: slots.prefixIcon, suffix: slots.suffix, panel: function panel() { return createVNode(SelectPanel, mergeProps({ "ref": selectPanelRef }, pick(props2, ["size", "multiple", "empty", "loading", "loadingText", "filterable", "creatable", "panelTopContent", "panelBottomContent", "filter", "scroll"]), { "options": options.value, "inputValue": innerInputValue.value }), slots); }, collapsedItems: slots.collapsedItems })]); }; } }); export { _Select as default }; //# sourceMappingURL=select.js.map