UNPKG

tntd

Version:

tntd是基于 TNT Design 设计体系的 React UI 组件库,主要用于研发企业级中后台产品。

103 lines (102 loc) 3.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLableByValue = exports.flatWithParents = exports.flatTreeData = exports.filterLastestNode = void 0; function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } /** * 给每个节点增加一个parent字段,记录其父节点用于showCheckedStrategy * @param {*} treeData * @param {*} fieldNames * @param {*} parents * @returns */ var _flatWithParents = exports.flatWithParents = function flatWithParents(treeData) { var fieldNames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { title: 'title', value: 'value', children: 'children', disabled: 'disabled' }; var parents = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; var result = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; if (Array.isArray(treeData)) { treeData.forEach(function (i) { result[i[fieldNames['value']]] = parents; if (Array.isArray(i[fieldNames['children']])) { var list = _flatWithParents(i[fieldNames['children']], fieldNames, [].concat(_toConsumableArray(parents), [i[fieldNames['value']]])); result = Object.assign(Object.assign({}, result), list); } else {} }); } return result; }; var filterLastestNode = exports.filterLastestNode = function filterLastestNode(treeNodeWithParents, item, list) { var parents = treeNodeWithParents[item] || {}; if (Array.isArray(parents) && !!parents.length && Array.isArray(list) && !!list.length) { return parents.every(function (el) { return !list.includes(el); }); } return false; }; /** * 扁平树 * @param {*} treeData * @param {*} fieldNames 展开的节点 * @returns */ var _flatTreeData = exports.flatTreeData = function flatTreeData(treeData) { var fieldNames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { title: 'title', value: 'value', children: 'children', disabled: 'disabled' }; var result = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; if (Array.isArray(treeData)) { treeData.forEach(function (i) { i['title'] = i[fieldNames['title']]; i['value'] = i[fieldNames['value']]; i['children'] = i[fieldNames['children']]; result.push(i); if (Array.isArray(i.children) && !!i.children.length) { result = [].concat(_toConsumableArray(result), _toConsumableArray(_flatTreeData(i.children, fieldNames))); } }); } return result; }; var getLableByValue = exports.getLableByValue = function getLableByValue(treeList, value) { if (Array.isArray(treeList)) { var _ref = treeList.find(function (i) { return i.value === value; }) || {}, title = _ref.title; return title || value; } return value; };