valueUtil.js
4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import _typeof from "@babel/runtime/helpers/esm/typeof";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { flattenTreeData } from "rc-tree/es/utils/treeUtil";
import { fillLegacyProps } from './legacyUtil';
export function toArray(value) {
if (Array.isArray(value)) {
return value;
}
return value !== undefined ? [value] : [];
}
export function findValueOption(values, options) {
var optionMap = new Map();
options.forEach(function (flattenItem) {
var data = flattenItem.data;
optionMap.set(data.value, data);
});
return values.map(function (val) {
return fillLegacyProps(optionMap.get(val));
});
}
export function isValueDisabled(value, options) {
var option = findValueOption([value], options)[0];
if (option) {
return option.disabled;
}
return false;
}
export function isCheckDisabled(node) {
return node.disabled || node.disableCheckbox || node.checkable === false;
}
function getLevel(_ref) {
var parent = _ref.parent;
var level = 0;
var current = parent;
while (current) {
current = current.parent;
level += 1;
}
return level;
}
/**
* Before reuse `rc-tree` logic, we need to add key since TreeSelect use `value` instead of `key`.
*/
export function flattenOptions(options) {
// Add missing key
function fillKey(list) {
return (list || []).map(function (node) {
var value = node.value,
key = node.key,
children = node.children;
var clone = _objectSpread(_objectSpread({}, node), {}, {
key: 'key' in node ? key : value
});
if (children) {
clone.children = fillKey(children);
}
return clone;
});
}
var flattenList = flattenTreeData(fillKey(options), true);
return flattenList.map(function (node) {
return {
key: node.data.key,
data: node.data,
level: getLevel(node)
};
});
}
function getDefaultFilterOption(optionFilterProp) {
return function (searchValue, dataNode) {
var value = dataNode[optionFilterProp];
return String(value).toLowerCase().includes(String(searchValue).toLowerCase());
};
}
/** Filter options and return a new options by the search text */
export function filterOptions(searchValue, options, _ref2) {
var optionFilterProp = _ref2.optionFilterProp,
filterOption = _ref2.filterOption;
if (filterOption === false) {
return options;
}
var filterOptionFunc;
if (typeof filterOption === 'function') {
filterOptionFunc = filterOption;
} else {
filterOptionFunc = getDefaultFilterOption(optionFilterProp);
}
function dig(list) {
var keepAll = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
return list.map(function (dataNode) {
var children = dataNode.children;
var match = keepAll || filterOptionFunc(searchValue, fillLegacyProps(dataNode));
var childList = dig(children || [], match);
if (match || childList.length) {
return _objectSpread(_objectSpread({}, dataNode), {}, {
children: childList
});
}
return null;
}).filter(function (node) {
return node;
});
}
return dig(options);
}
export function getRawValueLabeled(values, prevValue, getEntityByValue, getLabelProp) {
var valueMap = new Map();
toArray(prevValue).forEach(function (item) {
if (item && _typeof(item) === 'object' && 'value' in item) {
valueMap.set(item.value, item);
}
});
return values.map(function (val) {
var item = {
value: val
};
var entity = getEntityByValue(val, 'select', true);
var label = entity ? getLabelProp(entity.data) : val;
if (valueMap.has(val)) {
var labeledValue = valueMap.get(val);
item.label = 'label' in labeledValue ? labeledValue.label : label;
if ('halfChecked' in labeledValue) {
item.halfChecked = labeledValue.halfChecked;
}
} else {
item.label = label;
}
return item;
});
}
export function addValue(rawValues, value) {
var values = new Set(rawValues);
values.add(value);
return Array.from(values);
}
export function removeValue(rawValues, value) {
var values = new Set(rawValues);
values.delete(value);
return Array.from(values);
}