@douyinfe/semi-ui 2.47.0 → 2.48.0-alpha.0

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.
@@ -20,7 +20,7 @@ import ReactDOM from 'react-dom';
20
20
  import cls from 'classnames';
21
21
  import PropTypes from 'prop-types';
22
22
  import TreeSelectFoundation from '@douyinfe/semi-foundation/lib/es/treeSelect/foundation';
23
- import { convertDataToEntities, flattenTreeData, calcExpandedKeysForValues, calcMotionKeys, findKeysForValues, calcCheckedKeys, calcExpandedKeys, getValueOrKey, normalizeKeyList, calcDisabledKeys, normalizeValue, updateKeys } from '@douyinfe/semi-foundation/lib/es/tree/treeUtil';
23
+ import { convertDataToEntities, flattenTreeData, calcExpandedKeysForValues, calcMotionKeys, findKeysForValues, calcCheckedKeys, calcExpandedKeys, getValueOrKey, normalizeKeyList, calcDisabledKeys, normalizeValue, updateKeys, filterTreeData } from '@douyinfe/semi-foundation/lib/es/tree/treeUtil';
24
24
  import { cssClasses, strings } from '@douyinfe/semi-foundation/lib/es/treeSelect/constants';
25
25
  import { numbers as popoverNumbers } from '@douyinfe/semi-foundation/lib/es/popover/constants';
26
26
  import { FixedSizeList as VirtualList } from 'react-window';
@@ -680,13 +680,27 @@ class TreeSelect extends BaseComponent {
680
680
  searchAutoFocus,
681
681
  searchPosition
682
682
  } = this.props;
683
- if (isVisible === false && Boolean(filterTreeNode)) {
684
- this.foundation.clearInput();
683
+ // inputValue 清空,如果有选中值的话,选中项能够快速回显
684
+ // Clear the inputValue. If there is a selected value, the selected item can be quickly echoed.
685
+ if (isVisible === false && filterTreeNode) {
686
+ this.foundation.clearInputValue();
685
687
  }
686
688
  if (filterTreeNode && searchPosition === strings.SEARCH_POSITION_DROPDOWN && isVisible && searchAutoFocus) {
687
689
  this.foundation.focusInput(true);
688
690
  }
689
691
  };
692
+ this.afterClose = () => {
693
+ // flattenNode 的变化将导致弹出层面板中的选项数目变化
694
+ // 在弹层完全收起之后,再通过 clearInput 重新计算 state 中的 expandedKey, flattenNode
695
+ // 防止在弹出层未收起时弹层面板中选项数目变化导致视觉上出现弹层闪动问题
696
+ // Changes to flattenNode will cause the number of options in the popup panel to change
697
+ // After the pop-up layer is completely closed, recalculate the expandedKey and flattenNode in the state through clearInput.
698
+ // Prevent the pop-up layer from flickering visually due to changes in the number of options in the pop-up panel when the pop-up layer is not collapsed.
699
+ const {
700
+ filterTreeNode
701
+ } = this.props;
702
+ filterTreeNode && this.foundation.clearInput();
703
+ };
690
704
  this.renderTreeNode = (treeNode, ind, style) => {
691
705
  const {
692
706
  data,
@@ -719,6 +733,7 @@ class TreeSelect extends BaseComponent {
719
733
  this.renderNodeList = () => {
720
734
  const {
721
735
  flattenNodes,
736
+ cachedFlattenNodes,
722
737
  motionKeys,
723
738
  motionType,
724
739
  filteredKeys
@@ -734,7 +749,7 @@ class TreeSelect extends BaseComponent {
734
749
  if (!virtualize || _isEmpty(virtualize)) {
735
750
  return /*#__PURE__*/React.createElement(NodeList, {
736
751
  flattenNodes: flattenNodes,
737
- flattenList: this._flattenNodes,
752
+ flattenList: cachedFlattenNodes,
738
753
  motionKeys: motionExpand ? motionKeys : new Set([]),
739
754
  motionType: motionType,
740
755
  // When motionKeys is empty, but filteredKeys is not empty (that is, the search hits), this situation should be distinguished from ordinary motionKeys
@@ -853,6 +868,7 @@ class TreeSelect extends BaseComponent {
853
868
  keyEntities: {},
854
869
  treeData: [],
855
870
  flattenNodes: [],
871
+ cachedFlattenNodes: undefined,
856
872
  selectedKeys: [],
857
873
  checkedKeys: new Set(),
858
874
  halfCheckedKeys: new Set(),
@@ -882,6 +898,7 @@ class TreeSelect extends BaseComponent {
882
898
  };
883
899
  }
884
900
  static getDerivedStateFromProps(props, prevState) {
901
+ var _a;
885
902
  const {
886
903
  prevProps,
887
904
  rePosKey
@@ -899,6 +916,8 @@ class TreeSelect extends BaseComponent {
899
916
  };
900
917
  const needUpdateTreeData = needUpdate('treeData');
901
918
  const needUpdateExpandedKeys = needUpdate('expandedKeys');
919
+ const isExpandControlled = ('expandedKeys' in props);
920
+ const isSearching = Boolean(props.filterTreeNode && prevState.inputValue && prevState.inputValue.length);
902
921
  // TreeNode
903
922
  if (needUpdateTreeData) {
904
923
  treeData = props.treeData;
@@ -917,31 +936,97 @@ class TreeSelect extends BaseComponent {
917
936
  }
918
937
  }
919
938
  const expandAllWhenDataChange = needUpdateTreeData && props.expandAll;
920
- // expandedKeys
921
- if (needUpdateExpandedKeys || prevProps && needUpdate('autoExpandParent')) {
922
- newState.expandedKeys = calcExpandedKeys(props.expandedKeys, keyEntities, props.autoExpandParent || !prevProps);
923
- // only show animation when treeData does not change
924
- if (prevProps && props.motion && !treeData) {
925
- const {
926
- motionKeys,
927
- motionType
928
- } = calcMotionKeys(prevState.expandedKeys, newState.expandedKeys, keyEntities);
929
- newState.motionKeys = new Set(motionKeys);
930
- newState.motionType = motionType;
939
+ if (!isSearching) {
940
+ // expandedKeys
941
+ if (needUpdateExpandedKeys || prevProps && needUpdate('autoExpandParent')) {
942
+ newState.expandedKeys = calcExpandedKeys(props.expandedKeys, keyEntities, props.autoExpandParent || !prevProps);
943
+ // only show animation when treeData does not change
944
+ if (prevProps && props.motion && !treeData) {
945
+ const {
946
+ motionKeys,
947
+ motionType
948
+ } = calcMotionKeys(prevState.expandedKeys, newState.expandedKeys, keyEntities);
949
+ newState.motionKeys = new Set(motionKeys);
950
+ newState.motionType = motionType;
951
+ if (motionType === 'hide') {
952
+ // cache flatten nodes: expandedKeys changed may not be triggered by interaction
953
+ newState.cachedFlattenNodes = cloneDeep(prevState.flattenNodes);
954
+ }
955
+ }
956
+ } else if (!prevProps && (props.defaultExpandAll || props.expandAll) || expandAllWhenDataChange) {
957
+ newState.expandedKeys = new Set(Object.keys(keyEntities));
958
+ } else if (!prevProps && props.defaultExpandedKeys) {
959
+ newState.expandedKeys = calcExpandedKeys(props.defaultExpandedKeys, keyEntities);
960
+ } else if (!prevProps && props.defaultValue) {
961
+ newState.expandedKeys = calcExpandedKeysForValues(normalizeValue(props.defaultValue, withObject, keyMaps), keyEntities, props.multiple, valueEntities);
962
+ } else if (!prevProps && props.value) {
963
+ newState.expandedKeys = calcExpandedKeysForValues(normalizeValue(props.value, withObject, keyMaps), keyEntities, props.multiple, valueEntities);
964
+ } else if (!isExpandControlled && needUpdateTreeData && props.value) {
965
+ // 当 treeData 已经设置具体的值,并且设置了 props.loadData ,则认为 treeData 的更新是因为 loadData 导致的
966
+ // 如果是因为 loadData 导致 treeData改变, 此时在这里重新计算 key 会导致为未选中的展开项目被收起
967
+ // 所以此时不需要重新计算 expandedKeys,因为在点击展开按钮时候已经把被展开的项添加到 expandedKeys 中
968
+ // When treeData has a specific value and props.loadData is set, it is considered that the update of treeData is caused by loadData
969
+ // If the treeData is changed because of loadData, recalculating the key here will cause the unselected expanded items to be collapsed
970
+ // So there is no need to recalculate expandedKeys at this time, because the expanded item has been added to expandedKeys when the expand button is clicked
971
+ if (!(prevState.treeData && ((_a = prevState.treeData) === null || _a === void 0 ? void 0 : _a.length) > 0 && props.loadData)) {
972
+ newState.expandedKeys = calcExpandedKeysForValues(props.value, keyEntities, props.multiple, valueEntities);
973
+ }
974
+ }
975
+ if (!newState.expandedKeys) {
976
+ delete newState.expandedKeys;
977
+ }
978
+ if (treeData || newState.expandedKeys) {
979
+ const flattenNodes = flattenTreeData(treeData || prevState.treeData, newState.expandedKeys || prevState.expandedKeys, keyMaps);
980
+ newState.flattenNodes = flattenNodes;
981
+ }
982
+ } else {
983
+ let filteredState;
984
+ // treeData changed while searching
985
+ if (treeData) {
986
+ // Get filter data
987
+ filteredState = filterTreeData({
988
+ treeData,
989
+ inputValue: prevState.inputValue,
990
+ filterTreeNode: props.filterTreeNode,
991
+ filterProps: props.treeNodeFilterProp,
992
+ showFilteredOnly: props.showFilteredOnly,
993
+ keyEntities: newState.keyEntities,
994
+ prevExpandedKeys: [...prevState.filteredExpandedKeys],
995
+ keyMaps: keyMaps
996
+ });
997
+ newState.flattenNodes = filteredState.flattenNodes;
998
+ newState.motionKeys = new Set([]);
999
+ newState.filteredKeys = filteredState.filteredKeys;
1000
+ newState.filteredShownKeys = filteredState.filteredShownKeys;
1001
+ newState.filteredExpandedKeys = filteredState.filteredExpandedKeys;
1002
+ }
1003
+ // expandedKeys changed while searching
1004
+ if (props.expandedKeys) {
1005
+ newState.filteredExpandedKeys = calcExpandedKeys(props.expandedKeys, keyEntities, props.autoExpandParent || !prevProps);
1006
+ if (prevProps && props.motion) {
1007
+ const prevKeys = prevState ? prevState.filteredExpandedKeys : new Set([]);
1008
+ // only show animation when treeData does not change
1009
+ if (!treeData) {
1010
+ const motionResult = calcMotionKeys(prevKeys, newState.filteredExpandedKeys, keyEntities);
1011
+ let {
1012
+ motionKeys
1013
+ } = motionResult;
1014
+ const {
1015
+ motionType
1016
+ } = motionResult;
1017
+ if (props.showFilteredOnly) {
1018
+ motionKeys = motionKeys.filter(key => prevState.filteredShownKeys.has(key));
1019
+ }
1020
+ if (motionType === 'hide') {
1021
+ // cache flatten nodes: expandedKeys changed may not be triggered by interaction
1022
+ newState.cachedFlattenNodes = cloneDeep(prevState.flattenNodes);
1023
+ }
1024
+ newState.motionKeys = new Set(motionKeys);
1025
+ newState.motionType = motionType;
1026
+ }
1027
+ }
1028
+ newState.flattenNodes = flattenTreeData(treeData || prevState.treeData, newState.filteredExpandedKeys || prevState.filteredExpandedKeys, keyMaps, props.showFilteredOnly && prevState.filteredShownKeys);
931
1029
  }
932
- } else if (!prevProps && (props.defaultExpandAll || props.expandAll) || expandAllWhenDataChange) {
933
- newState.expandedKeys = new Set(Object.keys(keyEntities));
934
- } else if (!prevProps && props.defaultExpandedKeys) {
935
- newState.expandedKeys = calcExpandedKeys(props.defaultExpandedKeys, keyEntities);
936
- } else if (!prevProps && props.defaultValue) {
937
- newState.expandedKeys = calcExpandedKeysForValues(normalizeValue(props.defaultValue, withObject, keyMaps), keyEntities, props.multiple, valueEntities);
938
- } else if (!prevProps && props.value) {
939
- newState.expandedKeys = calcExpandedKeysForValues(normalizeValue(props.value, withObject, keyMaps), keyEntities, props.multiple, valueEntities);
940
- }
941
- // flattenNodes
942
- if (treeData || needUpdateExpandedKeys) {
943
- const flattenNodes = flattenTreeData(treeData || prevState.treeData, newState.expandedKeys || prevState.expandedKeys, keyMaps);
944
- newState.flattenNodes = flattenNodes;
945
1030
  }
946
1031
  // selectedKeys: single mode controlled
947
1032
  const isMultiple = props.multiple;
@@ -1048,7 +1133,9 @@ class TreeSelect extends BaseComponent {
1048
1133
  this.props.onSearch && this.props.onSearch(input, filteredExpandedKeys);
1049
1134
  },
1050
1135
  cacheFlattenNodes: bool => {
1051
- this._flattenNodes = bool ? cloneDeep(this.state.flattenNodes) : null;
1136
+ this.setState({
1137
+ cachedFlattenNodes: bool ? cloneDeep(this.state.flattenNodes) : undefined
1138
+ });
1052
1139
  },
1053
1140
  notifyLoad: (newLoadedKeys, data) => {
1054
1141
  const {
@@ -1193,7 +1280,8 @@ class TreeSelect extends BaseComponent {
1193
1280
  autoAdjustOverflow: autoAdjustOverflow,
1194
1281
  mouseLeaveDelay: mouseLeaveDelay,
1195
1282
  mouseEnterDelay: mouseEnterDelay,
1196
- onVisibleChange: this.handlePopoverClose
1283
+ onVisibleChange: this.handlePopoverClose,
1284
+ afterClose: this.afterClose
1197
1285
  }, selection);
1198
1286
  }
1199
1287
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@douyinfe/semi-ui",
3
- "version": "2.47.0",
3
+ "version": "2.48.0-alpha.0",
4
4
  "description": "A modern, comprehensive, flexible design system and UI library. Connect DesignOps & DevOps. Quickly build beautiful React apps. Maintained by Douyin-fe team.",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/es/index.js",
@@ -20,12 +20,12 @@
20
20
  "@dnd-kit/core": "^6.0.8",
21
21
  "@dnd-kit/sortable": "^7.0.2",
22
22
  "@dnd-kit/utilities": "^3.2.1",
23
- "@douyinfe/semi-animation": "2.47.0",
24
- "@douyinfe/semi-animation-react": "2.47.0",
25
- "@douyinfe/semi-foundation": "2.47.0",
26
- "@douyinfe/semi-icons": "2.47.0",
27
- "@douyinfe/semi-illustrations": "2.47.0",
28
- "@douyinfe/semi-theme-default": "2.47.0",
23
+ "@douyinfe/semi-animation": "2.48.0-alpha.0",
24
+ "@douyinfe/semi-animation-react": "2.48.0-alpha.0",
25
+ "@douyinfe/semi-foundation": "2.48.0-alpha.0",
26
+ "@douyinfe/semi-icons": "2.48.0-alpha.0",
27
+ "@douyinfe/semi-illustrations": "2.48.0-alpha.0",
28
+ "@douyinfe/semi-theme-default": "2.48.0-alpha.0",
29
29
  "async-validator": "^3.5.0",
30
30
  "classnames": "^2.2.6",
31
31
  "copy-text-to-clipboard": "^2.1.1",
@@ -75,7 +75,7 @@
75
75
  ],
76
76
  "author": "",
77
77
  "license": "MIT",
78
- "gitHead": "6ec26559221487884e981fe44daa300af1a336f6",
78
+ "gitHead": "930287380571d83f7a34d99fb13d80b4474adff8",
79
79
  "devDependencies": {
80
80
  "@babel/plugin-proposal-decorators": "^7.15.8",
81
81
  "@babel/plugin-transform-runtime": "^7.15.8",