UNPKG

@antmjs/vantui

Version:

一套适用于Taro3及React的vantui组件库

98 lines (96 loc) 4.47 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof3 = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.formatTree = exports.eachTree = exports.convertListToOptions = void 0; var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); 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) { (0, _defineProperty2.default)(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; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof3(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if ((0, _typeof2.default)(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if ((0, _typeof2.default)(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } var formatTree = function formatTree(tree, parent, config) { return tree.map(function (node) { var _config$value = config.value, valueKey = _config$value === void 0 ? 'value' : _config$value, _config$text = config.text, textKey = _config$text === void 0 ? 'text' : _config$text, _config$children = config.children, childrenKey = _config$children === void 0 ? 'children' : _config$children; var value = node[valueKey], text = node[textKey], children = node[childrenKey], others = (0, _objectWithoutProperties2.default)(node, [valueKey, textKey, childrenKey].map(_toPropertyKey)); var newNode = _objectSpread(_objectSpread({ loading: false }, others), {}, { level: parent ? (parent && parent.level || 0) + 1 : 0, value: value, text: text, children: children, _parent: parent }); if (newNode.children && newNode.children.length) { newNode.children = formatTree(newNode.children, newNode, config); } return newNode; }); }; exports.formatTree = formatTree; var eachTree = function eachTree(tree, cb) { var i = 0; var node; // @ts-ignore while (node = tree[i++]) { if (cb(node) === true) { break; } if (node.children && node.children.length) { eachTree(node.children, cb); } } }; exports.eachTree = eachTree; var defaultConvertConfig = { topId: null, idKey: 'id', pidKey: 'pid', sortKey: '' }; var convertListToOptions = function convertListToOptions(list, options) { var mergedOptions = _objectSpread(_objectSpread({}, defaultConvertConfig), options || {}); var topId = mergedOptions.topId, idKey = mergedOptions.idKey, pidKey = mergedOptions.pidKey, sortKey = mergedOptions.sortKey; var result = []; var map = {}; list.forEach(function (node) { node = _objectSpread({}, node); var _node = node, id = _node[idKey], pid = _node[pidKey]; var children = map[pid] = map[pid] || []; // const children = map[pid] || [] if (!result.length && pid === topId) { result = children; } children.push(node); node.children = map[id] || (map[id] = []); }); if (sortKey) { Object.keys(map).forEach(function (i) { if (map[i].length > 1) { map[i].sort(function (a, b) { return a[sortKey] - b[sortKey]; }); } }); } map = null; return result; }; exports.convertListToOptions = convertListToOptions;