@douyinfe/semi-ui 2.17.0-beta.0 → 2.17.0-beta.1

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.
@@ -1,4 +1,4 @@
1
- import React, { useState } from 'react';
1
+ import React, { useState, useCallback, useEffect } from 'react';
2
2
  import CustomTrigger from './CustomTrigger';
3
3
  import { Button, Typography, Toast, Cascader } from '../../index';
4
4
 
@@ -262,6 +262,96 @@ const treedataWithNodeLabel = [
262
262
  }
263
263
  ];
264
264
 
265
+ export const issue703 = () => {
266
+ const initialData = [
267
+ {
268
+ label: 'Node1',
269
+ value: '0-0',
270
+ },
271
+ {
272
+ label: 'Node2',
273
+ value: '0-1',
274
+ },
275
+ {
276
+ label: 'Node3',
277
+ value: '0-2',
278
+ isLeaf: true
279
+ },
280
+ ];
281
+ const [data, setData] = useState(initialData);
282
+
283
+ const updateTreeData = (list, value, children) => {
284
+ return list.map(node => {
285
+ if (node.value === value) {
286
+ return { ...node, children };
287
+ }
288
+ if (node.children) {
289
+ return { ...node, children: updateTreeData(node.children, value, children) };
290
+ }
291
+ return node;
292
+ });
293
+ };
294
+
295
+ const onLoadData = selectedOpt => {
296
+ const targetOpt = selectedOpt[selectedOpt.length - 1];
297
+ const { label, value } = targetOpt;
298
+ return new Promise(resolve => {
299
+ if (targetOpt.children) {
300
+ resolve();
301
+ return;
302
+ }
303
+
304
+ setTimeout(() => {
305
+ setData(origin =>
306
+ updateTreeData(origin, value, [
307
+ {
308
+ label: `${label}-1`,
309
+ value: `${label}-1`,
310
+ isLeaf: selectedOpt.length > 1
311
+ },
312
+ {
313
+ label: `${label}-2`,
314
+ value: `${label}-2`,
315
+ isLeaf: selectedOpt.length > 1
316
+ },
317
+ ]),
318
+ );
319
+ resolve();
320
+ }, 1000);
321
+ });
322
+ };
323
+
324
+ const [v,setV]=useState([['0-0'], ['0-1', 'Node2-2']]);
325
+ useEffect(()=>{
326
+ console.log('data change');
327
+ setTimeout(()=>setV([['0-0'], ['0-1', 'Node2-2', 'Node2-2-2']]),0);
328
+ },[data])
329
+
330
+ return (
331
+ <>
332
+ <div>treeData和value动态更新,value中的值在treeData中存在则能够正确显示</div>
333
+ <Cascader
334
+ multiple
335
+ onChange={(a)=>console.log(a)}
336
+ value={v}
337
+ style={{ width: 300 }}
338
+ treeData={data}
339
+ loadData={onLoadData}
340
+ placeholder="Please select"
341
+ />
342
+ <div>非受控,动态更新treeData</div>
343
+ <Cascader
344
+ multiple
345
+ onChange={(a)=>console.log(a)}
346
+ style={{ width: 300 }}
347
+ treeData={data}
348
+ loadData={onLoadData}
349
+ placeholder="Please select"
350
+ />
351
+ </>
352
+ );
353
+ };
354
+
265
355
  export const _Cascader = () => {
266
356
  return (
267
357
  <div>
@@ -394,6 +394,34 @@ class Cascader extends BaseComponent<CascaderProps, CascaderState> {
394
394
  const treeDataHasChange = prevProps && prevProps.treeData !== props.treeData;
395
395
  return firstInProps || treeDataHasChange;
396
396
  };
397
+ const getRealKeys = (realValue: Value, keyEntities: Entities) => {
398
+ // normallizedValue is used to save the value in two-dimensional array format
399
+ let normallizedValue: SimpleValueType[][] = [];
400
+ if (Array.isArray(realValue)) {
401
+ normallizedValue = Array.isArray(realValue[0])
402
+ ? (realValue as SimpleValueType[][])
403
+ : ([realValue] as SimpleValueType[][]);
404
+ } else {
405
+ if (realValue !== undefined) {
406
+ normallizedValue = [[realValue]];
407
+ }
408
+ }
409
+ // formatValuePath is used to save value of valuePath
410
+ const formatValuePath: (string | number)[][] = [];
411
+ normallizedValue.forEach((valueItem: SimpleValueType[]) => {
412
+ const formatItem: (string | number)[] = onChangeWithObject ?
413
+ (valueItem as CascaderData[]).map(i => i?.value) :
414
+ valueItem as (string | number)[];
415
+ formatValuePath.push(formatItem);
416
+ });
417
+ // formatKeys is used to save key of value
418
+ const formatKeys: any[] = [];
419
+ formatValuePath.forEach(v => {
420
+ const formatKeyItem = findKeysForValues(v, keyEntities);
421
+ !isEmpty(formatKeyItem) && formatKeys.push(formatKeyItem);
422
+ });
423
+ return formatKeys;
424
+ };
397
425
  const needUpdateTreeData = needUpdate('treeData') || needUpdateData();
398
426
  const needUpdateValue = needUpdate('value') || (isEmpty(prevProps) && defaultValue);
399
427
  if (multiple) {
@@ -408,34 +436,15 @@ class Cascader extends BaseComponent<CascaderProps, CascaderState> {
408
436
  let realKeys: Array<string> | Set<string> = prevState.checkedKeys;
409
437
  // when data was updated
410
438
  if (needUpdateValue) {
411
- // normallizedValue is used to save the value in two-dimensional array format
412
- let normallizedValue: SimpleValueType[][] = [];
413
439
  const realValue = needUpdate('value') ? value : defaultValue;
414
- // eslint-disable-next-line max-depth
415
- if (Array.isArray(realValue)) {
416
- normallizedValue = Array.isArray(realValue[0])
417
- ? (realValue as SimpleValueType[][])
418
- : ([realValue] as SimpleValueType[][]);
419
- } else {
420
- if (realValue !== undefined) {
421
- normallizedValue = [[realValue]];
422
- }
440
+ realKeys = getRealKeys(realValue, keyEntities);
441
+ } else {
442
+ // needUpdateValue is false
443
+ // if treeData is updated & Cascader is controlled, realKeys should be recalculated
444
+ if (needUpdateTreeData && 'value' in props) {
445
+ const realValue = value;
446
+ realKeys = getRealKeys(realValue, keyEntities);
423
447
  }
424
- // formatValuePath is used to save value of valuePath
425
- const formatValuePath: (string | number)[][] = [];
426
- normallizedValue.forEach((valueItem: SimpleValueType[]) => {
427
- const formatItem: (string | number)[] = onChangeWithObject ?
428
- (valueItem as CascaderData[]).map(i => i?.value) :
429
- valueItem as (string | number)[];
430
- formatValuePath.push(formatItem);
431
- });
432
- // formatKeys is used to save key of value
433
- const formatKeys: any[] = [];
434
- formatValuePath.forEach(v => {
435
- const formatKeyItem = findKeysForValues(v, keyEntities);
436
- !isEmpty(formatKeyItem) && formatKeys.push(formatKeyItem);
437
- });
438
- realKeys = formatKeys;
439
448
  }
440
449
  if (isSet(realKeys)) {
441
450
  realKeys = [...realKeys];
@@ -54334,6 +54334,37 @@ class cascader_Cascader extends baseComponent_BaseComponent {
54334
54334
  return firstInProps || treeDataHasChange;
54335
54335
  };
54336
54336
 
54337
+ const getRealKeys = (realValue, keyEntities) => {
54338
+ // normallizedValue is used to save the value in two-dimensional array format
54339
+ let normallizedValue = [];
54340
+
54341
+ if (is_array_default()(realValue)) {
54342
+ normallizedValue = is_array_default()(realValue[0]) ? realValue : [realValue];
54343
+ } else {
54344
+ if (realValue !== undefined) {
54345
+ normallizedValue = [[realValue]];
54346
+ }
54347
+ } // formatValuePath is used to save value of valuePath
54348
+
54349
+
54350
+ const formatValuePath = [];
54351
+
54352
+ for_each_default()(normallizedValue).call(normallizedValue, valueItem => {
54353
+ const formatItem = onChangeWithObject ? map_default()(valueItem).call(valueItem, i => i === null || i === void 0 ? void 0 : i.value) : valueItem;
54354
+ formatValuePath.push(formatItem);
54355
+ }); // formatKeys is used to save key of value
54356
+
54357
+
54358
+ const formatKeys = [];
54359
+
54360
+ for_each_default()(formatValuePath).call(formatValuePath, v => {
54361
+ const formatKeyItem = util_findKeysForValues(v, keyEntities);
54362
+ !isEmpty_default()(formatKeyItem) && formatKeys.push(formatKeyItem);
54363
+ });
54364
+
54365
+ return formatKeys;
54366
+ };
54367
+
54337
54368
  const needUpdateTreeData = needUpdate('treeData') || needUpdateData();
54338
54369
  const needUpdateValue = needUpdate('value') || isEmpty_default()(prevProps) && defaultValue;
54339
54370
 
@@ -54350,35 +54381,15 @@ class cascader_Cascader extends baseComponent_BaseComponent {
54350
54381
  let realKeys = prevState.checkedKeys; // when data was updated
54351
54382
 
54352
54383
  if (needUpdateValue) {
54353
- // normallizedValue is used to save the value in two-dimensional array format
54354
- let normallizedValue = [];
54355
- const realValue = needUpdate('value') ? value : defaultValue; // eslint-disable-next-line max-depth
54356
-
54357
- if (is_array_default()(realValue)) {
54358
- normallizedValue = is_array_default()(realValue[0]) ? realValue : [realValue];
54359
- } else {
54360
- if (realValue !== undefined) {
54361
- normallizedValue = [[realValue]];
54362
- }
54363
- } // formatValuePath is used to save value of valuePath
54364
-
54365
-
54366
- const formatValuePath = [];
54367
-
54368
- for_each_default()(normallizedValue).call(normallizedValue, valueItem => {
54369
- const formatItem = onChangeWithObject ? map_default()(valueItem).call(valueItem, i => i === null || i === void 0 ? void 0 : i.value) : valueItem;
54370
- formatValuePath.push(formatItem);
54371
- }); // formatKeys is used to save key of value
54372
-
54373
-
54374
- const formatKeys = [];
54375
-
54376
- for_each_default()(formatValuePath).call(formatValuePath, v => {
54377
- const formatKeyItem = util_findKeysForValues(v, keyEntities);
54378
- !isEmpty_default()(formatKeyItem) && formatKeys.push(formatKeyItem);
54379
- });
54380
-
54381
- realKeys = formatKeys;
54384
+ const realValue = needUpdate('value') ? value : defaultValue;
54385
+ realKeys = getRealKeys(realValue, keyEntities);
54386
+ } else {
54387
+ // needUpdateValue is false
54388
+ // if treeData is updated & Cascader is controlled, realKeys should be recalculated
54389
+ if (needUpdateTreeData && 'value' in props) {
54390
+ const realValue = value;
54391
+ realKeys = getRealKeys(realValue, keyEntities);
54392
+ }
54382
54393
  }
54383
54394
 
54384
54395
  if (isSet_default()(realKeys)) {
@@ -68707,7 +68718,7 @@ class empty_Empty extends baseComponent_BaseComponent {
68707
68718
  darkModeImage
68708
68719
  } = this.props;
68709
68720
  const alt = typeof description === 'string' ? description : 'empty';
68710
- const imgSrc = this.state.mode && darkModeImage ? darkModeImage : image;
68721
+ const imgSrc = this.state.mode === 'dark' && darkModeImage ? darkModeImage : image;
68711
68722
  let imageNode = null;
68712
68723
 
68713
68724
  if (typeof imgSrc === 'string') {