UNPKG

bonree-cascader

Version:

cascade select ui component for react

146 lines (120 loc) 4.65 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.DATA_EMPTY = exports.ALL_KEY = void 0; exports.connectValue = connectValue; exports.convertOptions = convertOptions; exports.customDisplayValues = customDisplayValues; Object.defineProperty(exports, "fillFieldNames", { enumerable: true, get: function get() { return _valueUtil.fillFieldNames; } }); exports.isLeaf = isLeaf; exports.restoreCompatibleValue = restoreCompatibleValue; exports.splitValue = splitValue; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _objectSpread3 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); var _valueUtil = require("bonree-tree-select/lib/utils/valueUtil"); var VALUE_SPLIT = '__RC_CASCADER_SPLIT__'; var DATA_EMPTY = '__EMPTY__'; exports.DATA_EMPTY = DATA_EMPTY; var ALL_KEY = '__RC_CASCADER_ALL__'; exports.ALL_KEY = ALL_KEY; /** * Convert entity back to path & options * @returns */ function restoreCompatibleValue(entity, fieldNames) { var path = []; var options = []; var current = entity; while (current) { path.unshift(current.data.node[fieldNames.value]); options.unshift(current.data.node); current = current.parent; } return { path: path, options: options }; } function isLeaf(option) { var children = option.children, node = option.node; var leaf = node === null || node === void 0 ? void 0 : node.isLeaf; return leaf !== undefined ? leaf : !(children === null || children === void 0 ? void 0 : children.length); } /** * We will connect path value to a string. e.g. * ['little', 'bamboo'] => 'little__bamboo' * * zombieJ: It's better to deprecate the same key in the nest tree. Maybe next major version. */ function connectValue(value) { return (value || []).join(VALUE_SPLIT); } /** * Reverse of `connectValue` */ function splitValue(str) { return str.split(VALUE_SPLIT); } /** * Fill options with fully value by path to avoid nest entity with same value. * Which means we need another round to get origin node back! * This is slow perf on large list. We should abandon same value in nest in future. */ function convertOptions(options, _ref, internalValueField) { var fieldValue = _ref.value, fieldChildren = _ref.children; function injectValue(list) { var parentValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; return (list || []).map(function (option) { var _objectSpread2; var newValue = option[fieldValue]; if (parentValue) { newValue = connectValue([parentValue, option[fieldValue]]); } else if (typeof newValue === 'number') { // Since we will convert all the value to string, we need get this newValue = String(newValue); } var cloneOption = (0, _objectSpread3.default)((0, _objectSpread3.default)({}, option), {}, (_objectSpread2 = {}, (0, _defineProperty2.default)(_objectSpread2, internalValueField, newValue), (0, _defineProperty2.default)(_objectSpread2, "node", option), _objectSpread2)); if (cloneOption[fieldChildren]) { cloneOption[fieldChildren] = injectValue(cloneOption[fieldChildren], newValue); } return cloneOption; }); } return injectValue(options); } function customDisplayValues(tmpValues, mergedFlattenOptions, isMultiple, showAll, isSelectAll, displayAllValue) { if (isMultiple && showAll && isSelectAll && tmpValues.length > 0) { var optValues = mergedFlattenOptions === null || mergedFlattenOptions === void 0 ? void 0 : mergedFlattenOptions.filter(function (v) { return !v.data.disabled; }).filter(function (v) { return !String(v.key).includes(VALUE_SPLIT); }).map(function (item) { return item.value; }); var hasSelectOptValuesAll = optValues.every(function (k) { return tmpValues.some(function (v) { return v.value === k; }); }); if (tmpValues.length === optValues.length && hasSelectOptValuesAll) { // 只展示父级全部选项 return [displayAllValue]; } else if (tmpValues.length > optValues.length && hasSelectOptValuesAll) { // 展示父级全部选项 + 子级选项 var notInValues = tmpValues.filter(function (v) { return !optValues.includes(v.value); }); return [displayAllValue].concat((0, _toConsumableArray2.default)(notInValues)); } } }