UNPKG

bonree-cascader

Version:

cascade select ui component for react

115 lines (101 loc) 3.93 kB
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; import { fillFieldNames } from "bonree-tree-select/es/utils/valueUtil"; var VALUE_SPLIT = '__RC_CASCADER_SPLIT__'; export var DATA_EMPTY = '__EMPTY__'; export var ALL_KEY = '__RC_CASCADER_ALL__'; export { fillFieldNames }; /** * Convert entity back to path & options * @returns */ export 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 }; } export 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. */ export function connectValue(value) { return (value || []).join(VALUE_SPLIT); } /** * Reverse of `connectValue` */ export 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. */ export 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 = _objectSpread(_objectSpread({}, option), {}, (_objectSpread2 = {}, _defineProperty(_objectSpread2, internalValueField, newValue), _defineProperty(_objectSpread2, "node", option), _objectSpread2)); if (cloneOption[fieldChildren]) { cloneOption[fieldChildren] = injectValue(cloneOption[fieldChildren], newValue); } return cloneOption; }); } return injectValue(options); } export 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(_toConsumableArray(notInValues)); } } }