@douyinfe/semi-foundation 2.99.2 → 2.99.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,9 +16,13 @@ import {
16
16
  isValid,
17
17
  calcMergeType,
18
18
  getKeysByValuePath,
19
- getKeyByPos
19
+ getKeyByPos,
20
+ KeyMapProps,
21
+ getValueOrKey
20
22
  } from './util';
21
23
  import { strings } from './constants';
24
+
25
+ export type { KeyMapProps };
22
26
  import isEnterPress from '../utils/isEnterPress';
23
27
  import { ESC_KEY } from '../utils/keyCode';
24
28
 
@@ -171,6 +175,7 @@ export interface BasicCascaderProps {
171
175
  virtualizeInSearch?: Virtualize;
172
176
  checkRelation?: string;
173
177
  remote?: boolean;
178
+ keyMaps?: KeyMapProps;
174
179
  onClear?: () => void;
175
180
  triggerRender?: (props: BasicTriggerRenderProps) => any;
176
181
  onListScroll?: (e: any, panel: BasicScrollPanelProps) => void;
@@ -407,8 +412,8 @@ export default class CascaderFoundation extends BaseFoundation<CascaderAdapter,
407
412
  }
408
413
 
409
414
  collectOptions(init = false) {
410
- const { treeData, value, defaultValue } = this.getProps();
411
- const keyEntities = convertDataToEntities(treeData);
415
+ const { treeData, value, defaultValue, keyMaps } = this.getProps();
416
+ const keyEntities = convertDataToEntities(treeData, keyMaps);
412
417
  this._adapter.rePositionDropdown();
413
418
  let cacheValue;
414
419
  /* when mount */
@@ -442,7 +447,8 @@ export default class CascaderFoundation extends BaseFoundation<CascaderAdapter,
442
447
  if (!sugInput) {
443
448
  return [];
444
449
  }
445
- const { treeNodeFilterProp, filterTreeNode, filterLeafOnly, remote } = this.getProps();
450
+ const { treeNodeFilterProp, filterTreeNode, filterLeafOnly, remote, keyMaps } = this.getProps();
451
+ const realFilterProp = treeNodeFilterProp !== 'label' ? treeNodeFilterProp : get(keyMaps, 'label', 'label');
446
452
  const entities = Object.values(keyEntities ?? this.getState('keyEntities')) as BasicEntity[];
447
453
 
448
454
  if (remote) {
@@ -458,7 +464,7 @@ export default class CascaderFoundation extends BaseFoundation<CascaderAdapter,
458
464
  if (_notExist) {
459
465
  return false;
460
466
  }
461
- const filteredPath = this.getItemPropPath(key, treeNodeFilterProp, keyEntities);
467
+ const filteredPath = this.getItemPropPath(key, realFilterProp, keyEntities);
462
468
  return filter(sugInput, data, filterTreeNode, filteredPath);
463
469
  })
464
470
  .filter(item => (filterTreeNode && !filterLeafOnly) || this._isLeaf(item.data))
@@ -531,7 +537,8 @@ export default class CascaderFoundation extends BaseFoundation<CascaderAdapter,
531
537
  const filterable = this._isFilterable();
532
538
  const loadingActive = [...activeKeys].filter(i => loadingKeys.has(i));
533
539
  const normalizedValue = normalizedArr(value);
534
- const valuePath = onChangeWithObject && isObject(normalizedValue[0]) ? normalizedValue.map(i => i.value) : normalizedValue;
540
+ const keyMaps = this.getProp('keyMaps');
541
+ const valuePath = onChangeWithObject && isObject(normalizedValue[0]) ? getValueOrKey(normalizedValue, keyMaps) : normalizedValue;
535
542
  const selectedKeys = getKeysByValuePath(valuePath);
536
543
  let updateStates: Partial<BasicCascaderInnerData> = {};
537
544
 
@@ -1121,7 +1128,8 @@ export default class CascaderFoundation extends BaseFoundation<CascaderAdapter,
1121
1128
  }
1122
1129
 
1123
1130
  getFilteredData() {
1124
- const { treeNodeFilterProp, filterSorter } = this.getProps();
1131
+ const { treeNodeFilterProp, filterSorter, keyMaps } = this.getProps();
1132
+ const realFilterProp = treeNodeFilterProp !== 'label' ? treeNodeFilterProp : get(keyMaps, 'label', 'label');
1125
1133
  const { filteredKeys, keyEntities, inputValue } = this.getStates();
1126
1134
  const filteredList: BasicData[] = [];
1127
1135
  const filteredKeyArr = [...filteredKeys];
@@ -1131,7 +1139,7 @@ export default class CascaderFoundation extends BaseFoundation<CascaderAdapter,
1131
1139
  return;
1132
1140
  }
1133
1141
  const pathData = this.getItemPropPath(key, []);
1134
- const itemSearchPath = pathData.map(item => item[treeNodeFilterProp]);
1142
+ const itemSearchPath = pathData.map(item => item[realFilterProp]);
1135
1143
  const isDisabled = this._isOptionDisabled(key, keyEntities);
1136
1144
  filteredList.push({
1137
1145
  data: item.data,
@@ -1171,7 +1179,8 @@ export default class CascaderFoundation extends BaseFoundation<CascaderAdapter,
1171
1179
 
1172
1180
  handleTagRemoveInTrigger = (pos: string) => {
1173
1181
  const { treeData } = this.getStates();
1174
- const key = getKeyByPos(pos, treeData);
1182
+ const { keyMaps } = this.getProps();
1183
+ const key = getKeyByPos(pos, treeData, keyMaps);
1175
1184
  this.handleTagRemoveByKey(key);
1176
1185
  }
1177
1186
  }
package/cascader/util.ts CHANGED
@@ -1,10 +1,19 @@
1
1
  import {
2
2
  isNull,
3
3
  isUndefined,
4
- isEqual
4
+ isEqual,
5
+ get
5
6
  } from 'lodash';
6
7
  import { strings, VALUE_SPLIT } from './constants';
7
8
 
9
+ export interface KeyMapProps {
10
+ value?: string;
11
+ label?: string;
12
+ disabled?: string;
13
+ children?: string;
14
+ isLeaf?: string;
15
+ }
16
+
8
17
  function getPosition(level: any, index: any) {
9
18
  return `${level}-${index}`;
10
19
  }
@@ -54,23 +63,39 @@ export function filter(sugInput: string, option: any, filterTreeNode: any, filte
54
63
  /**
55
64
  * Traverse all the data by `treeData`.
56
65
  */
57
- function traverseDataNodes(treeNodes: any, callback: any) {
66
+ function traverseDataNodes(treeNodes: any, callback: any, keyMaps?: KeyMapProps) {
67
+ const realValueName = get(keyMaps, 'value', 'value');
68
+ const realChildrenName = get(keyMaps, 'children', 'children');
69
+
58
70
  const processNode = (node: any, ind?: any, parent?: any) => {
59
- const children = node ? node.children : treeNodes;
71
+ const children = node ? node[realChildrenName] : treeNodes;
60
72
  let item: any = null;
61
73
  // Process node if is not root
62
74
  if (node) {
63
- const key = parent ? `${parent.key}${VALUE_SPLIT}${node.value}` : `${node.value}`;
75
+ const nodeValue = node[realValueName];
76
+ const key = parent ? `${parent.key}${VALUE_SPLIT}${nodeValue}` : `${nodeValue}`;
64
77
  const pos = parent ? getPosition(parent.pos, ind) : `${ind}`;
78
+
79
+ // Map original fields to standard field names if keyMaps is provided
80
+ const mappedData = { ...node };
81
+ if (keyMaps) {
82
+ Object.entries(keyMaps).forEach(([standardKey, originalKey]) => {
83
+ const value = node[originalKey as string];
84
+ if (!isUndefined(value)) {
85
+ mappedData[standardKey] = value;
86
+ }
87
+ });
88
+ }
89
+
65
90
  item = {
66
- data: { ...node },
91
+ data: mappedData,
67
92
  ind,
68
93
  key,
69
94
  pos,
70
95
  level: parent ? parent.level + 1 : 0,
71
96
  parentKey: parent ? parent.key : null,
72
97
  path: parent ? [...parent.path, key] : [key],
73
- valuePath: parent ? [...parent.valuePath, node.value] : [node.value]
98
+ valuePath: parent ? [...parent.valuePath, nodeValue] : [nodeValue]
74
99
  };
75
100
 
76
101
  callback(item);
@@ -106,18 +131,20 @@ export function getValuePathByKey(key: string) {
106
131
  return key.split(VALUE_SPLIT);
107
132
  }
108
133
 
109
- export function getKeyByPos(pos: string, treeData: any) {
134
+ export function getKeyByPos(pos: string, treeData: any, keyMaps?: KeyMapProps) {
135
+ const realValueName = get(keyMaps, 'value', 'value');
136
+ const realChildrenName = get(keyMaps, 'children', 'children');
110
137
  const posArr = pos.split('-').map(item => Number(item));
111
138
  let resultData = treeData;
112
- let valuePath = [];
139
+ const valuePath: (string | number)[] = [];
113
140
  posArr.forEach((item, index) => {
114
- resultData = index === 0 ? resultData[item] : resultData?.children?.[item];
115
- valuePath.push(resultData?.value);
141
+ resultData = index === 0 ? resultData[item] : resultData?.[realChildrenName]?.[item];
142
+ valuePath.push(resultData?.[realValueName]);
116
143
  });
117
144
  return getKeyByValuePath(valuePath);
118
145
  }
119
146
 
120
- export function convertDataToEntities(dataNodes: any) {
147
+ export function convertDataToEntities(dataNodes: any, keyMaps?: KeyMapProps) {
121
148
  const keyEntities: any = {};
122
149
 
123
150
  traverseDataNodes(dataNodes, (data: any) => {
@@ -132,10 +159,24 @@ export function convertDataToEntities(dataNodes: any) {
132
159
  entity.parent.children = entity.parent.children || [];
133
160
  entity.parent.children.push(entity);
134
161
  }
135
- });
162
+ }, keyMaps);
136
163
  return keyEntities;
137
164
  }
138
165
 
166
+ /**
167
+ * Get the value from data item using keyMaps mapping.
168
+ * Similar to Tree/TreeSelect's getValueOrKey.
169
+ * When keyMaps maps value to a custom field (e.g., 'id'), use that field;
170
+ * otherwise fall back to 'value'.
171
+ */
172
+ export function getValueOrKey(data: any, keyMaps?: KeyMapProps) {
173
+ const valueName = get(keyMaps, 'value', 'value');
174
+ if (Array.isArray(data)) {
175
+ return data.map(item => get(item, valueName));
176
+ }
177
+ return get(data, valueName);
178
+ }
179
+
139
180
  export function calcMergeType(autoMergeValue: boolean, leafOnly: boolean): string {
140
181
  let mergeType: string;
141
182
  if (leafOnly) {
@@ -1,4 +1,6 @@
1
1
  import BaseFoundation, { DefaultAdapter } from '../base/foundation';
2
+ import { KeyMapProps } from './util';
3
+ export type { KeyMapProps };
2
4
  export interface BasicData {
3
5
  data: BasicCascaderData;
4
6
  disabled: boolean;
@@ -115,6 +117,7 @@ export interface BasicCascaderProps {
115
117
  virtualizeInSearch?: Virtualize;
116
118
  checkRelation?: string;
117
119
  remote?: boolean;
120
+ keyMaps?: KeyMapProps;
118
121
  onClear?: () => void;
119
122
  triggerRender?: (props: BasicTriggerRenderProps) => any;
120
123
  onListScroll?: (e: any, panel: BasicScrollPanelProps) => void;
@@ -56,7 +56,10 @@ class CascaderFoundation extends _foundation.default {
56
56
  const {
57
57
  treeData
58
58
  } = this.getStates();
59
- const key = (0, _util.getKeyByPos)(pos, treeData);
59
+ const {
60
+ keyMaps
61
+ } = this.getProps();
62
+ const key = (0, _util.getKeyByPos)(pos, treeData, keyMaps);
60
63
  this.handleTagRemoveByKey(key);
61
64
  };
62
65
  }
@@ -199,9 +202,10 @@ class CascaderFoundation extends _foundation.default {
199
202
  const {
200
203
  treeData,
201
204
  value,
202
- defaultValue
205
+ defaultValue,
206
+ keyMaps
203
207
  } = this.getProps();
204
- const keyEntities = (0, _util.convertDataToEntities)(treeData);
208
+ const keyEntities = (0, _util.convertDataToEntities)(treeData, keyMaps);
205
209
  this._adapter.rePositionDropdown();
206
210
  let cacheValue;
207
211
  /* when mount */
@@ -238,8 +242,10 @@ class CascaderFoundation extends _foundation.default {
238
242
  treeNodeFilterProp,
239
243
  filterTreeNode,
240
244
  filterLeafOnly,
241
- remote
245
+ remote,
246
+ keyMaps
242
247
  } = this.getProps();
248
+ const realFilterProp = treeNodeFilterProp !== 'label' ? treeNodeFilterProp : (0, _get2.default)(keyMaps, 'label', 'label');
243
249
  const entities = Object.values(keyEntities !== null && keyEntities !== void 0 ? keyEntities : this.getState('keyEntities'));
244
250
  if (remote) {
245
251
  return entities.filter(item => !item._notExist).filter(item => filterTreeNode && !filterLeafOnly || this._isLeaf(item.data)).map(item => item.key);
@@ -253,7 +259,7 @@ class CascaderFoundation extends _foundation.default {
253
259
  if (_notExist) {
254
260
  return false;
255
261
  }
256
- const filteredPath = this.getItemPropPath(key, treeNodeFilterProp, keyEntities);
262
+ const filteredPath = this.getItemPropPath(key, realFilterProp, keyEntities);
257
263
  return (0, _util.filter)(sugInput, data, filterTreeNode, filteredPath);
258
264
  }).filter(item => filterTreeNode && !filterLeafOnly || this._isLeaf(item.data)).map(item => item.key);
259
265
  }
@@ -328,7 +334,8 @@ class CascaderFoundation extends _foundation.default {
328
334
  const filterable = this._isFilterable();
329
335
  const loadingActive = [...activeKeys].filter(i => loadingKeys.has(i));
330
336
  const normalizedValue = (0, _util.normalizedArr)(value);
331
- const valuePath = onChangeWithObject && (0, _isObject2.default)(normalizedValue[0]) ? normalizedValue.map(i => i.value) : normalizedValue;
337
+ const keyMaps = this.getProp('keyMaps');
338
+ const valuePath = onChangeWithObject && (0, _isObject2.default)(normalizedValue[0]) ? (0, _util.getValueOrKey)(normalizedValue, keyMaps) : normalizedValue;
332
339
  const selectedKeys = (0, _util.getKeysByValuePath)(valuePath);
333
340
  let updateStates = {};
334
341
  const selectedKey = selectedKeys.length > 0 ? selectedKeys[0] : undefined;
@@ -954,8 +961,10 @@ class CascaderFoundation extends _foundation.default {
954
961
  getFilteredData() {
955
962
  const {
956
963
  treeNodeFilterProp,
957
- filterSorter
964
+ filterSorter,
965
+ keyMaps
958
966
  } = this.getProps();
967
+ const realFilterProp = treeNodeFilterProp !== 'label' ? treeNodeFilterProp : (0, _get2.default)(keyMaps, 'label', 'label');
959
968
  const {
960
969
  filteredKeys,
961
970
  keyEntities,
@@ -969,7 +978,7 @@ class CascaderFoundation extends _foundation.default {
969
978
  return;
970
979
  }
971
980
  const pathData = this.getItemPropPath(key, []);
972
- const itemSearchPath = pathData.map(item => item[treeNodeFilterProp]);
981
+ const itemSearchPath = pathData.map(item => item[realFilterProp]);
973
982
  const isDisabled = this._isOptionDisabled(key, keyEntities);
974
983
  filteredList.push({
975
984
  data: item.data,
@@ -1,3 +1,10 @@
1
+ export interface KeyMapProps {
2
+ value?: string;
3
+ label?: string;
4
+ disabled?: string;
5
+ children?: string;
6
+ isLeaf?: string;
7
+ }
1
8
  export declare function isValid(val: any): boolean;
2
9
  export declare function normalizedArr(val: any): any[];
3
10
  /**
@@ -8,6 +15,13 @@ export declare function filter(sugInput: string, option: any, filterTreeNode: an
8
15
  export declare function getKeysByValuePath(valuePath: (string | number)[][] | (string | number)[]): string[];
9
16
  export declare function getKeyByValuePath(valuePath: (string | number)[]): string;
10
17
  export declare function getValuePathByKey(key: string): string[];
11
- export declare function getKeyByPos(pos: string, treeData: any): string;
12
- export declare function convertDataToEntities(dataNodes: any): any;
18
+ export declare function getKeyByPos(pos: string, treeData: any, keyMaps?: KeyMapProps): string;
19
+ export declare function convertDataToEntities(dataNodes: any, keyMaps?: KeyMapProps): any;
20
+ /**
21
+ * Get the value from data item using keyMaps mapping.
22
+ * Similar to Tree/TreeSelect's getValueOrKey.
23
+ * When keyMaps maps value to a custom field (e.g., 'id'), use that field;
24
+ * otherwise fall back to 'value'.
25
+ */
26
+ export declare function getValueOrKey(data: any, keyMaps?: KeyMapProps): any;
13
27
  export declare function calcMergeType(autoMergeValue: boolean, leafOnly: boolean): string;
@@ -9,9 +9,11 @@ exports.filter = filter;
9
9
  exports.getKeyByPos = getKeyByPos;
10
10
  exports.getKeyByValuePath = getKeyByValuePath;
11
11
  exports.getKeysByValuePath = getKeysByValuePath;
12
+ exports.getValueOrKey = getValueOrKey;
12
13
  exports.getValuePathByKey = getValuePathByKey;
13
14
  exports.isValid = isValid;
14
15
  exports.normalizedArr = normalizedArr;
16
+ var _get2 = _interopRequireDefault(require("lodash/get"));
15
17
  var _isUndefined2 = _interopRequireDefault(require("lodash/isUndefined"));
16
18
  var _isNull2 = _interopRequireDefault(require("lodash/isNull"));
17
19
  var _constants = require("./constants");
@@ -59,23 +61,37 @@ function filter(sugInput, option, filterTreeNode, filteredPath) {
59
61
  /**
60
62
  * Traverse all the data by `treeData`.
61
63
  */
62
- function traverseDataNodes(treeNodes, callback) {
64
+ function traverseDataNodes(treeNodes, callback, keyMaps) {
65
+ const realValueName = (0, _get2.default)(keyMaps, 'value', 'value');
66
+ const realChildrenName = (0, _get2.default)(keyMaps, 'children', 'children');
63
67
  const processNode = (node, ind, parent) => {
64
- const children = node ? node.children : treeNodes;
68
+ const children = node ? node[realChildrenName] : treeNodes;
65
69
  let item = null;
66
70
  // Process node if is not root
67
71
  if (node) {
68
- const key = parent ? `${parent.key}${_constants.VALUE_SPLIT}${node.value}` : `${node.value}`;
72
+ const nodeValue = node[realValueName];
73
+ const key = parent ? `${parent.key}${_constants.VALUE_SPLIT}${nodeValue}` : `${nodeValue}`;
69
74
  const pos = parent ? getPosition(parent.pos, ind) : `${ind}`;
75
+ // Map original fields to standard field names if keyMaps is provided
76
+ const mappedData = Object.assign({}, node);
77
+ if (keyMaps) {
78
+ Object.entries(keyMaps).forEach(_ref => {
79
+ let [standardKey, originalKey] = _ref;
80
+ const value = node[originalKey];
81
+ if (!(0, _isUndefined2.default)(value)) {
82
+ mappedData[standardKey] = value;
83
+ }
84
+ });
85
+ }
70
86
  item = {
71
- data: Object.assign({}, node),
87
+ data: mappedData,
72
88
  ind,
73
89
  key,
74
90
  pos,
75
91
  level: parent ? parent.level + 1 : 0,
76
92
  parentKey: parent ? parent.key : null,
77
93
  path: parent ? [...parent.path, key] : [key],
78
- valuePath: parent ? [...parent.valuePath, node.value] : [node.value]
94
+ valuePath: parent ? [...parent.valuePath, nodeValue] : [nodeValue]
79
95
  };
80
96
  callback(item);
81
97
  }
@@ -104,18 +120,20 @@ function getKeyByValuePath(valuePath) {
104
120
  function getValuePathByKey(key) {
105
121
  return key.split(_constants.VALUE_SPLIT);
106
122
  }
107
- function getKeyByPos(pos, treeData) {
123
+ function getKeyByPos(pos, treeData, keyMaps) {
124
+ const realValueName = (0, _get2.default)(keyMaps, 'value', 'value');
125
+ const realChildrenName = (0, _get2.default)(keyMaps, 'children', 'children');
108
126
  const posArr = pos.split('-').map(item => Number(item));
109
127
  let resultData = treeData;
110
- let valuePath = [];
128
+ const valuePath = [];
111
129
  posArr.forEach((item, index) => {
112
130
  var _a;
113
- resultData = index === 0 ? resultData[item] : (_a = resultData === null || resultData === void 0 ? void 0 : resultData.children) === null || _a === void 0 ? void 0 : _a[item];
114
- valuePath.push(resultData === null || resultData === void 0 ? void 0 : resultData.value);
131
+ resultData = index === 0 ? resultData[item] : (_a = resultData === null || resultData === void 0 ? void 0 : resultData[realChildrenName]) === null || _a === void 0 ? void 0 : _a[item];
132
+ valuePath.push(resultData === null || resultData === void 0 ? void 0 : resultData[realValueName]);
115
133
  });
116
134
  return getKeyByValuePath(valuePath);
117
135
  }
118
- function convertDataToEntities(dataNodes) {
136
+ function convertDataToEntities(dataNodes, keyMaps) {
119
137
  const keyEntities = {};
120
138
  traverseDataNodes(dataNodes, data => {
121
139
  const {
@@ -130,9 +148,22 @@ function convertDataToEntities(dataNodes) {
130
148
  entity.parent.children = entity.parent.children || [];
131
149
  entity.parent.children.push(entity);
132
150
  }
133
- });
151
+ }, keyMaps);
134
152
  return keyEntities;
135
153
  }
154
+ /**
155
+ * Get the value from data item using keyMaps mapping.
156
+ * Similar to Tree/TreeSelect's getValueOrKey.
157
+ * When keyMaps maps value to a custom field (e.g., 'id'), use that field;
158
+ * otherwise fall back to 'value'.
159
+ */
160
+ function getValueOrKey(data, keyMaps) {
161
+ const valueName = (0, _get2.default)(keyMaps, 'value', 'value');
162
+ if (Array.isArray(data)) {
163
+ return data.map(item => (0, _get2.default)(item, valueName));
164
+ }
165
+ return (0, _get2.default)(data, valueName);
166
+ }
136
167
  function calcMergeType(autoMergeValue, leafOnly) {
137
168
  let mergeType;
138
169
  if (leafOnly) {
@@ -1,3 +1,5 @@
1
+ $horizontal-rate: 0.24; // 水平方向矫正因子 ignore-semi-css-trans
2
+
1
3
  // Color
2
4
  $color-tooltip_arrow-icon-default: unset; // 箭头图标
3
5
  $color-popover-bg-default: var(--semi-color-bg-3); // 默认背景色
@@ -1,4 +1,6 @@
1
1
  import BaseFoundation, { DefaultAdapter } from '../base/foundation';
2
+ import { KeyMapProps } from './util';
3
+ export type { KeyMapProps };
2
4
  export interface BasicData {
3
5
  data: BasicCascaderData;
4
6
  disabled: boolean;
@@ -115,6 +117,7 @@ export interface BasicCascaderProps {
115
117
  virtualizeInSearch?: Virtualize;
116
118
  checkRelation?: string;
117
119
  remote?: boolean;
120
+ keyMaps?: KeyMapProps;
118
121
  onClear?: () => void;
119
122
  triggerRender?: (props: BasicTriggerRenderProps) => any;
120
123
  onListScroll?: (e: any, panel: BasicScrollPanelProps) => void;
@@ -11,7 +11,7 @@ import _get from "lodash/get";
11
11
  import _isEqual from "lodash/isEqual";
12
12
  import BaseFoundation from '../base/foundation';
13
13
  import { findAncestorKeys, calcCheckedKeysForUnchecked, calcCheckedKeysForChecked, calcCheckedKeys, findDescendantKeys, normalizeKeyList } from '../tree/treeUtil';
14
- import { filter, convertDataToEntities, normalizedArr, isValid, calcMergeType, getKeysByValuePath, getKeyByPos } from './util';
14
+ import { filter, convertDataToEntities, normalizedArr, isValid, calcMergeType, getKeysByValuePath, getKeyByPos, getValueOrKey } from './util';
15
15
  import { strings } from './constants';
16
16
  import isEnterPress from '../utils/isEnterPress';
17
17
  import { ESC_KEY } from '../utils/keyCode';
@@ -49,7 +49,10 @@ export default class CascaderFoundation extends BaseFoundation {
49
49
  const {
50
50
  treeData
51
51
  } = this.getStates();
52
- const key = getKeyByPos(pos, treeData);
52
+ const {
53
+ keyMaps
54
+ } = this.getProps();
55
+ const key = getKeyByPos(pos, treeData, keyMaps);
53
56
  this.handleTagRemoveByKey(key);
54
57
  };
55
58
  }
@@ -192,9 +195,10 @@ export default class CascaderFoundation extends BaseFoundation {
192
195
  const {
193
196
  treeData,
194
197
  value,
195
- defaultValue
198
+ defaultValue,
199
+ keyMaps
196
200
  } = this.getProps();
197
- const keyEntities = convertDataToEntities(treeData);
201
+ const keyEntities = convertDataToEntities(treeData, keyMaps);
198
202
  this._adapter.rePositionDropdown();
199
203
  let cacheValue;
200
204
  /* when mount */
@@ -231,8 +235,10 @@ export default class CascaderFoundation extends BaseFoundation {
231
235
  treeNodeFilterProp,
232
236
  filterTreeNode,
233
237
  filterLeafOnly,
234
- remote
238
+ remote,
239
+ keyMaps
235
240
  } = this.getProps();
241
+ const realFilterProp = treeNodeFilterProp !== 'label' ? treeNodeFilterProp : _get(keyMaps, 'label', 'label');
236
242
  const entities = Object.values(keyEntities !== null && keyEntities !== void 0 ? keyEntities : this.getState('keyEntities'));
237
243
  if (remote) {
238
244
  return entities.filter(item => !item._notExist).filter(item => filterTreeNode && !filterLeafOnly || this._isLeaf(item.data)).map(item => item.key);
@@ -246,7 +252,7 @@ export default class CascaderFoundation extends BaseFoundation {
246
252
  if (_notExist) {
247
253
  return false;
248
254
  }
249
- const filteredPath = this.getItemPropPath(key, treeNodeFilterProp, keyEntities);
255
+ const filteredPath = this.getItemPropPath(key, realFilterProp, keyEntities);
250
256
  return filter(sugInput, data, filterTreeNode, filteredPath);
251
257
  }).filter(item => filterTreeNode && !filterLeafOnly || this._isLeaf(item.data)).map(item => item.key);
252
258
  }
@@ -321,7 +327,8 @@ export default class CascaderFoundation extends BaseFoundation {
321
327
  const filterable = this._isFilterable();
322
328
  const loadingActive = [...activeKeys].filter(i => loadingKeys.has(i));
323
329
  const normalizedValue = normalizedArr(value);
324
- const valuePath = onChangeWithObject && _isObject(normalizedValue[0]) ? normalizedValue.map(i => i.value) : normalizedValue;
330
+ const keyMaps = this.getProp('keyMaps');
331
+ const valuePath = onChangeWithObject && _isObject(normalizedValue[0]) ? getValueOrKey(normalizedValue, keyMaps) : normalizedValue;
325
332
  const selectedKeys = getKeysByValuePath(valuePath);
326
333
  let updateStates = {};
327
334
  const selectedKey = selectedKeys.length > 0 ? selectedKeys[0] : undefined;
@@ -947,8 +954,10 @@ export default class CascaderFoundation extends BaseFoundation {
947
954
  getFilteredData() {
948
955
  const {
949
956
  treeNodeFilterProp,
950
- filterSorter
957
+ filterSorter,
958
+ keyMaps
951
959
  } = this.getProps();
960
+ const realFilterProp = treeNodeFilterProp !== 'label' ? treeNodeFilterProp : _get(keyMaps, 'label', 'label');
952
961
  const {
953
962
  filteredKeys,
954
963
  keyEntities,
@@ -962,7 +971,7 @@ export default class CascaderFoundation extends BaseFoundation {
962
971
  return;
963
972
  }
964
973
  const pathData = this.getItemPropPath(key, []);
965
- const itemSearchPath = pathData.map(item => item[treeNodeFilterProp]);
974
+ const itemSearchPath = pathData.map(item => item[realFilterProp]);
966
975
  const isDisabled = this._isOptionDisabled(key, keyEntities);
967
976
  filteredList.push({
968
977
  data: item.data,
@@ -1,3 +1,10 @@
1
+ export interface KeyMapProps {
2
+ value?: string;
3
+ label?: string;
4
+ disabled?: string;
5
+ children?: string;
6
+ isLeaf?: string;
7
+ }
1
8
  export declare function isValid(val: any): boolean;
2
9
  export declare function normalizedArr(val: any): any[];
3
10
  /**
@@ -8,6 +15,13 @@ export declare function filter(sugInput: string, option: any, filterTreeNode: an
8
15
  export declare function getKeysByValuePath(valuePath: (string | number)[][] | (string | number)[]): string[];
9
16
  export declare function getKeyByValuePath(valuePath: (string | number)[]): string;
10
17
  export declare function getValuePathByKey(key: string): string[];
11
- export declare function getKeyByPos(pos: string, treeData: any): string;
12
- export declare function convertDataToEntities(dataNodes: any): any;
18
+ export declare function getKeyByPos(pos: string, treeData: any, keyMaps?: KeyMapProps): string;
19
+ export declare function convertDataToEntities(dataNodes: any, keyMaps?: KeyMapProps): any;
20
+ /**
21
+ * Get the value from data item using keyMaps mapping.
22
+ * Similar to Tree/TreeSelect's getValueOrKey.
23
+ * When keyMaps maps value to a custom field (e.g., 'id'), use that field;
24
+ * otherwise fall back to 'value'.
25
+ */
26
+ export declare function getValueOrKey(data: any, keyMaps?: KeyMapProps): any;
13
27
  export declare function calcMergeType(autoMergeValue: boolean, leafOnly: boolean): string;
@@ -1,3 +1,4 @@
1
+ import _get from "lodash/get";
1
2
  import _isUndefined from "lodash/isUndefined";
2
3
  import _isNull from "lodash/isNull";
3
4
  import { strings, VALUE_SPLIT } from './constants';
@@ -44,23 +45,37 @@ export function filter(sugInput, option, filterTreeNode, filteredPath) {
44
45
  /**
45
46
  * Traverse all the data by `treeData`.
46
47
  */
47
- function traverseDataNodes(treeNodes, callback) {
48
+ function traverseDataNodes(treeNodes, callback, keyMaps) {
49
+ const realValueName = _get(keyMaps, 'value', 'value');
50
+ const realChildrenName = _get(keyMaps, 'children', 'children');
48
51
  const processNode = (node, ind, parent) => {
49
- const children = node ? node.children : treeNodes;
52
+ const children = node ? node[realChildrenName] : treeNodes;
50
53
  let item = null;
51
54
  // Process node if is not root
52
55
  if (node) {
53
- const key = parent ? `${parent.key}${VALUE_SPLIT}${node.value}` : `${node.value}`;
56
+ const nodeValue = node[realValueName];
57
+ const key = parent ? `${parent.key}${VALUE_SPLIT}${nodeValue}` : `${nodeValue}`;
54
58
  const pos = parent ? getPosition(parent.pos, ind) : `${ind}`;
59
+ // Map original fields to standard field names if keyMaps is provided
60
+ const mappedData = Object.assign({}, node);
61
+ if (keyMaps) {
62
+ Object.entries(keyMaps).forEach(_ref => {
63
+ let [standardKey, originalKey] = _ref;
64
+ const value = node[originalKey];
65
+ if (!_isUndefined(value)) {
66
+ mappedData[standardKey] = value;
67
+ }
68
+ });
69
+ }
55
70
  item = {
56
- data: Object.assign({}, node),
71
+ data: mappedData,
57
72
  ind,
58
73
  key,
59
74
  pos,
60
75
  level: parent ? parent.level + 1 : 0,
61
76
  parentKey: parent ? parent.key : null,
62
77
  path: parent ? [...parent.path, key] : [key],
63
- valuePath: parent ? [...parent.valuePath, node.value] : [node.value]
78
+ valuePath: parent ? [...parent.valuePath, nodeValue] : [nodeValue]
64
79
  };
65
80
  callback(item);
66
81
  }
@@ -89,18 +104,20 @@ export function getKeyByValuePath(valuePath) {
89
104
  export function getValuePathByKey(key) {
90
105
  return key.split(VALUE_SPLIT);
91
106
  }
92
- export function getKeyByPos(pos, treeData) {
107
+ export function getKeyByPos(pos, treeData, keyMaps) {
108
+ const realValueName = _get(keyMaps, 'value', 'value');
109
+ const realChildrenName = _get(keyMaps, 'children', 'children');
93
110
  const posArr = pos.split('-').map(item => Number(item));
94
111
  let resultData = treeData;
95
- let valuePath = [];
112
+ const valuePath = [];
96
113
  posArr.forEach((item, index) => {
97
114
  var _a;
98
- resultData = index === 0 ? resultData[item] : (_a = resultData === null || resultData === void 0 ? void 0 : resultData.children) === null || _a === void 0 ? void 0 : _a[item];
99
- valuePath.push(resultData === null || resultData === void 0 ? void 0 : resultData.value);
115
+ resultData = index === 0 ? resultData[item] : (_a = resultData === null || resultData === void 0 ? void 0 : resultData[realChildrenName]) === null || _a === void 0 ? void 0 : _a[item];
116
+ valuePath.push(resultData === null || resultData === void 0 ? void 0 : resultData[realValueName]);
100
117
  });
101
118
  return getKeyByValuePath(valuePath);
102
119
  }
103
- export function convertDataToEntities(dataNodes) {
120
+ export function convertDataToEntities(dataNodes, keyMaps) {
104
121
  const keyEntities = {};
105
122
  traverseDataNodes(dataNodes, data => {
106
123
  const {
@@ -115,9 +132,22 @@ export function convertDataToEntities(dataNodes) {
115
132
  entity.parent.children = entity.parent.children || [];
116
133
  entity.parent.children.push(entity);
117
134
  }
118
- });
135
+ }, keyMaps);
119
136
  return keyEntities;
120
137
  }
138
+ /**
139
+ * Get the value from data item using keyMaps mapping.
140
+ * Similar to Tree/TreeSelect's getValueOrKey.
141
+ * When keyMaps maps value to a custom field (e.g., 'id'), use that field;
142
+ * otherwise fall back to 'value'.
143
+ */
144
+ export function getValueOrKey(data, keyMaps) {
145
+ const valueName = _get(keyMaps, 'value', 'value');
146
+ if (Array.isArray(data)) {
147
+ return data.map(item => _get(item, valueName));
148
+ }
149
+ return _get(data, valueName);
150
+ }
121
151
  export function calcMergeType(autoMergeValue, leafOnly) {
122
152
  let mergeType;
123
153
  if (leafOnly) {
@@ -1,3 +1,5 @@
1
+ $horizontal-rate: 0.24; // 水平方向矫正因子 ignore-semi-css-trans
2
+
1
3
  // Color
2
4
  $color-tooltip_arrow-icon-default: unset; // 箭头图标
3
5
  $color-popover-bg-default: var(--semi-color-bg-3); // 默认背景色
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@douyinfe/semi-foundation",
3
- "version": "2.99.2",
3
+ "version": "2.99.3",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "clean": "rimraf lib",
@@ -14225,8 +14225,8 @@
14225
14225
  }
14226
14226
  },
14227
14227
  "dependencies": {
14228
- "@douyinfe/semi-animation": "2.99.2",
14229
- "@douyinfe/semi-json-viewer-core": "2.99.2",
14228
+ "@douyinfe/semi-animation": "2.99.3",
14229
+ "@douyinfe/semi-json-viewer-core": "2.99.3",
14230
14230
  "@mdx-js/mdx": "^3.0.1",
14231
14231
  "async-validator": "^3.5.0",
14232
14232
  "classnames": "^2.2.6",
@@ -14247,7 +14247,7 @@
14247
14247
  "*.scss",
14248
14248
  "*.css"
14249
14249
  ],
14250
- "gitHead": "59e8cd9364e406b5097b86455646171863e10466",
14250
+ "gitHead": "59a386b5f5a0cbeaa6f7cc14b4f28a67d8062af2",
14251
14251
  "devDependencies": {
14252
14252
  "@babel/plugin-transform-runtime": "^7.15.8",
14253
14253
  "@babel/preset-env": "^7.15.8",
@@ -1,3 +1,5 @@
1
+ $horizontal-rate: 0.24; // 水平方向矫正因子 ignore-semi-css-trans
2
+
1
3
  // Color
2
4
  $color-tooltip_arrow-icon-default: unset; // 箭头图标
3
5
  $color-popover-bg-default: var(--semi-color-bg-3); // 默认背景色