UNPKG

xdesign-vue-next

Version:

XDesign Component for vue-next

173 lines (169 loc) 8.29 kB
/** * xdesign v1.0.6 * (c) 2023 xdesign * @license MIT */ import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { ref, computed } from 'vue'; import isArray from 'lodash/isArray'; import get from 'lodash/get'; import isFunction from 'lodash/isFunction'; import { useChildComponentSlots } from '../../hooks/slot.js'; import '@babel/runtime/helpers/typeof'; 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 useSelectOptions = function useSelectOptions(props, keys, inputValue) { var getChildComponentSlots = useChildComponentSlots(); var optionsCache = ref([]); var options = computed(function () { var _props$options; var dynamicIndex = 0; var innerOptions = ((_props$options = props.options) === null || _props$options === void 0 ? void 0 : _props$options.map(function (option) { var getFormatOption = function getFormatOption(option2) { var _keys$value = keys.value, value = _keys$value.value, label = _keys$value.label, disabled = _keys$value.disabled; var res = _objectSpread(_objectSpread({}, option2), {}, { index: dynamicIndex, label: get(option2, label), value: get(option2, value), disabled: get(option2, disabled) }); dynamicIndex++; return res; }; if (option.group && option.children) { return _objectSpread(_objectSpread({}, option), {}, { children: option.children.map(function (child) { return getFormatOption(child); }) }); } return getFormatOption(option); })) || []; var optionsSlots = getChildComponentSlots("Option"); var groupSlots = getChildComponentSlots("OptionGroup"); if (isArray(groupSlots)) { var _iterator = _createForOfIteratorHelper(groupSlots), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var _group$props, _res$, _res$2; var group = _step.value; var groupOption = _objectSpread(_objectSpread({ group: (_group$props = group.props) === null || _group$props === void 0 ? void 0 : _group$props.label }, group.props), {}, { children: [] }); var res = group.children["default"](); if (!(isArray(res) && !!((_res$ = res[0]) !== null && _res$ !== void 0 && _res$.children))) continue; var _iterator2 = _createForOfIteratorHelper(res === null || res === void 0 || (_res$2 = res[0]) === null || _res$2 === void 0 ? void 0 : _res$2.children), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var child = _step2.value; groupOption.children.push(_objectSpread(_objectSpread({}, child.props), {}, { slots: child.children, index: dynamicIndex })); dynamicIndex++; } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } innerOptions.push(groupOption); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } if (isArray(optionsSlots)) { var _iterator3 = _createForOfIteratorHelper(optionsSlots), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var _child = _step3.value; innerOptions.push(_objectSpread(_objectSpread({}, _child.props), {}, { slots: _child.children, index: dynamicIndex })); dynamicIndex++; } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } } return innerOptions; }); var optionsList = computed(function () { var res = []; var getOptionsList = function getOptionsList(options2) { var _iterator4 = _createForOfIteratorHelper(options2), _step4; try { for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) { var option = _step4.value; if (option.group) { getOptionsList(option.children); } else { res.push(option); } } } catch (err) { _iterator4.e(err); } finally { _iterator4.f(); } }; getOptionsList(options.value); return res; }); var optionsMap = computed(function () { var res = /* @__PURE__ */new Map(); optionsCache.value.concat(optionsList.value).forEach(function (option) { res.set(option.value, option); }); return res; }); var displayOptions = computed(function () { if (!inputValue.value || !(props.filterable || isFunction(props.filter))) return options.value; var filterMethods = function filterMethods(option) { var _option$label, _option$label$toLower; if (isFunction(props.filter)) { return props.filter("".concat(inputValue.value), option); } 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(inputValue.value).toLowerCase())) > -1; }; var res = []; options.value.forEach(function (option) { if (option.group && option.children) { res.push(_objectSpread(_objectSpread({}, option), {}, { children: option.children.filter(filterMethods) })); } if (filterMethods(option)) { res.push(option); } }); return res; }); return { options: options, optionsMap: optionsMap, optionsList: optionsList, optionsCache: optionsCache, displayOptions: displayOptions }; }; export { useSelectOptions }; //# sourceMappingURL=useSelectOptions.js.map