@sheinx/hooks 3.5.0-beta.10 → 3.5.0-beta.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
1
- {"version":3,"file":"use-table-tree.d.ts","sourceRoot":"","sources":["use-table-tree.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAoCvD,MAAM,WAAW,iBACf,SAAQ,IAAI,CACV,kBAAkB,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,EACvC,cAAc,GAAG,gBAAgB,GAAG,uBAAuB,GAAG,QAAQ,CACvE;IACD,eAAe,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,IAAI,EAAE,GAAG,EAAE,CAAC;CACb;AAED,eAAO,MAAM,YAAY,UAAW,iBAAiB;;;;;;+BA6BR,GAAG,SAAS,MAAM;iCAjBhB,GAAG,SAAS,MAAM;;CA2ChE,CAAC;AAEF,eAAe,YAAY,CAAC"}
1
+ {"version":3,"file":"use-table-tree.d.ts","sourceRoot":"","sources":["use-table-tree.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AA8CvD,MAAM,WAAW,iBACf,SAAQ,IAAI,CACV,kBAAkB,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,EACvC,cAAc,GAAG,gBAAgB,GAAG,uBAAuB,GAAG,QAAQ,CACvE;IACD,eAAe,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,IAAI,EAAE,GAAG,EAAE,CAAC;CACb;AAED,eAAO,MAAM,YAAY,UAAW,iBAAiB;;;;;;+BA6BR,GAAG,SAAS,MAAM;iCAjBhB,GAAG,SAAS,MAAM;;CAuDhE,CAAC;AAEF,eAAe,YAAY,CAAC"}
@@ -19,35 +19,47 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
19
19
  function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
20
20
  function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
21
21
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
22
- var getExpandData = function getExpandData(data, keys, keygen, treeColumnsName) {
22
+ var getExpandData = function getExpandData(_treeData, keys, keygen, treeColumnsName) {
23
23
  var expandKeys = keys;
24
24
  var expandSet = new Set(expandKeys);
25
- var expandLevel = new Map();
25
+ var unmatchExpandKeys = [];
26
+ var treeExpandLevel = new Map();
26
27
  if (expandSet.size === 0 || !treeColumnsName) {
27
- return [data, expandLevel];
28
+ return {
29
+ treeData: _treeData,
30
+ treeExpandLevel: treeExpandLevel,
31
+ unmatchExpandKeys: unmatchExpandKeys
32
+ };
28
33
  }
29
- var newData = _toConsumableArray(data || []);
34
+ var treeData = _toConsumableArray(_treeData || []);
30
35
  var _loop = function _loop() {
31
36
  if (expandSet.size === 0) return 1; // break
32
- var item = newData[i];
37
+ var item = treeData[i];
33
38
  var key = (0, _render.getKey)(keygen, item, i);
34
- var parentLevel = expandLevel.get(key) || 0;
39
+ var parentLevel = treeExpandLevel.get(key) || 0;
35
40
  var children = (0, _is.isObject)(item) && item[treeColumnsName];
36
- if (expandSet.has(key) && children) {
37
- children.forEach(function (child) {
38
- expandLevel.set((0, _render.getKey)(keygen, child), parentLevel + 1);
39
- });
40
- newData.splice.apply(newData, [i + 1, 0].concat(_toConsumableArray(children)));
41
- expandSet.delete(key);
41
+ if (expandSet.has(key)) {
42
+ if (children && children.length > 0) {
43
+ children.forEach(function (child) {
44
+ treeExpandLevel.set((0, _render.getKey)(keygen, child), parentLevel + 1);
45
+ });
46
+ treeData.splice.apply(treeData, [i + 1, 0].concat(_toConsumableArray(children)));
47
+ expandSet.delete(key);
48
+ } else {
49
+ unmatchExpandKeys.push(key);
50
+ }
42
51
  }
43
52
  };
44
- for (var i = 0; i < newData.length; i++) {
53
+ for (var i = 0; i < treeData.length; i++) {
45
54
  if (_loop()) break;
46
55
  }
47
- return [newData, expandLevel];
56
+ return {
57
+ treeData: treeData,
58
+ treeExpandLevel: treeExpandLevel,
59
+ unmatchExpandKeys: unmatchExpandKeys
60
+ };
48
61
  };
49
62
  var useTableTree = exports.useTableTree = function useTableTree(props) {
50
- var _props$data$filter;
51
63
  var _useState = (0, _react.useState)(props.defaultTreeExpandKeys || []),
52
64
  _useState2 = _slicedToArray(_useState, 2),
53
65
  expandKeysState = _useState2[0],
@@ -88,13 +100,27 @@ var useTableTree = exports.useTableTree = function useTableTree(props) {
88
100
  var _useMemo = (0, _react.useMemo)(function () {
89
101
  return getExpandData(props.data, expandKeys, props.keygen, props.treeColumnsName);
90
102
  }, [props.data, expandKeys, props.treeColumnsName]),
91
- _useMemo2 = _slicedToArray(_useMemo, 2),
92
- treeData = _useMemo2[0],
93
- treeExpandLevel = _useMemo2[1];
94
- var isEmptyTree = ((_props$data$filter = props.data.filter(function (item) {
95
- var _item;
96
- return (_item = item[props.treeColumnsName]) === null || _item === void 0 ? void 0 : _item.length;
97
- })) === null || _props$data$filter === void 0 ? void 0 : _props$data$filter.length) === 0;
103
+ treeData = _useMemo.treeData,
104
+ treeExpandLevel = _useMemo.treeExpandLevel,
105
+ unmatchExpandKeys = _useMemo.unmatchExpandKeys;
106
+ var isEmptyTree = (0, _react.useMemo)(function () {
107
+ var _props$data$filter;
108
+ return ((_props$data$filter = props.data.filter(function (item) {
109
+ var _item;
110
+ return (_item = item[props.treeColumnsName]) === null || _item === void 0 ? void 0 : _item.length;
111
+ })) === null || _props$data$filter === void 0 ? void 0 : _props$data$filter.length) === 0;
112
+ }, [props.data, props.treeColumnsName]);
113
+ (0, _react.useEffect)(function () {
114
+ if (!unmatchExpandKeys.length || !expandKeysState.length) {
115
+ return;
116
+ }
117
+
118
+ // 检查treeData中的每一项,对比expandKeysState,如果expandKeysState有但是children是空的,则需要修正expandKeysState
119
+ var newExpandKeys = expandKeysState.filter(function (key) {
120
+ return !unmatchExpandKeys.includes(key);
121
+ });
122
+ setExpandKeysState(newExpandKeys);
123
+ }, [unmatchExpandKeys, expandKeysState]);
98
124
  return {
99
125
  data: treeData,
100
126
  treeExpandLevel: treeExpandLevel,
@@ -1 +1 @@
1
- {"version":3,"file":"use-table-tree.d.ts","sourceRoot":"","sources":["use-table-tree.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAoCvD,MAAM,WAAW,iBACf,SAAQ,IAAI,CACV,kBAAkB,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,EACvC,cAAc,GAAG,gBAAgB,GAAG,uBAAuB,GAAG,QAAQ,CACvE;IACD,eAAe,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,IAAI,EAAE,GAAG,EAAE,CAAC;CACb;AAED,eAAO,MAAM,YAAY,UAAW,iBAAiB;;;;;;+BA6BR,GAAG,SAAS,MAAM;iCAjBhB,GAAG,SAAS,MAAM;;CA2ChE,CAAC;AAEF,eAAe,YAAY,CAAC"}
1
+ {"version":3,"file":"use-table-tree.d.ts","sourceRoot":"","sources":["use-table-tree.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AA8CvD,MAAM,WAAW,iBACf,SAAQ,IAAI,CACV,kBAAkB,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,EACvC,cAAc,GAAG,gBAAgB,GAAG,uBAAuB,GAAG,QAAQ,CACvE;IACD,eAAe,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,IAAI,EAAE,GAAG,EAAE,CAAC;CACb;AAED,eAAO,MAAM,YAAY,UAAW,iBAAiB;;;;;;+BA6BR,GAAG,SAAS,MAAM;iCAjBhB,GAAG,SAAS,MAAM;;CAuDhE,CAAC;AAEF,eAAe,YAAY,CAAC"}
@@ -13,35 +13,47 @@ import { usePersistFn } from "../../common/use-persist-fn";
13
13
  import { getKey } from "../../utils/render";
14
14
  import { isObject } from "../../utils/is";
15
15
  import { useLatestObj } from "../../common/use-latest-obj";
16
- var getExpandData = function getExpandData(data, keys, keygen, treeColumnsName) {
16
+ var getExpandData = function getExpandData(_treeData, keys, keygen, treeColumnsName) {
17
17
  var expandKeys = keys;
18
18
  var expandSet = new Set(expandKeys);
19
- var expandLevel = new Map();
19
+ var unmatchExpandKeys = [];
20
+ var treeExpandLevel = new Map();
20
21
  if (expandSet.size === 0 || !treeColumnsName) {
21
- return [data, expandLevel];
22
+ return {
23
+ treeData: _treeData,
24
+ treeExpandLevel: treeExpandLevel,
25
+ unmatchExpandKeys: unmatchExpandKeys
26
+ };
22
27
  }
23
- var newData = _toConsumableArray(data || []);
28
+ var treeData = _toConsumableArray(_treeData || []);
24
29
  var _loop = function _loop() {
25
30
  if (expandSet.size === 0) return 1; // break
26
- var item = newData[i];
31
+ var item = treeData[i];
27
32
  var key = getKey(keygen, item, i);
28
- var parentLevel = expandLevel.get(key) || 0;
33
+ var parentLevel = treeExpandLevel.get(key) || 0;
29
34
  var children = isObject(item) && item[treeColumnsName];
30
- if (expandSet.has(key) && children) {
31
- children.forEach(function (child) {
32
- expandLevel.set(getKey(keygen, child), parentLevel + 1);
33
- });
34
- newData.splice.apply(newData, [i + 1, 0].concat(_toConsumableArray(children)));
35
- expandSet.delete(key);
35
+ if (expandSet.has(key)) {
36
+ if (children && children.length > 0) {
37
+ children.forEach(function (child) {
38
+ treeExpandLevel.set(getKey(keygen, child), parentLevel + 1);
39
+ });
40
+ treeData.splice.apply(treeData, [i + 1, 0].concat(_toConsumableArray(children)));
41
+ expandSet.delete(key);
42
+ } else {
43
+ unmatchExpandKeys.push(key);
44
+ }
36
45
  }
37
46
  };
38
- for (var i = 0; i < newData.length; i++) {
47
+ for (var i = 0; i < treeData.length; i++) {
39
48
  if (_loop()) break;
40
49
  }
41
- return [newData, expandLevel];
50
+ return {
51
+ treeData: treeData,
52
+ treeExpandLevel: treeExpandLevel,
53
+ unmatchExpandKeys: unmatchExpandKeys
54
+ };
42
55
  };
43
56
  export var useTableTree = function useTableTree(props) {
44
- var _props$data$filter;
45
57
  var _useState = useState(props.defaultTreeExpandKeys || []),
46
58
  _useState2 = _slicedToArray(_useState, 2),
47
59
  expandKeysState = _useState2[0],
@@ -82,13 +94,27 @@ export var useTableTree = function useTableTree(props) {
82
94
  var _useMemo = useMemo(function () {
83
95
  return getExpandData(props.data, expandKeys, props.keygen, props.treeColumnsName);
84
96
  }, [props.data, expandKeys, props.treeColumnsName]),
85
- _useMemo2 = _slicedToArray(_useMemo, 2),
86
- treeData = _useMemo2[0],
87
- treeExpandLevel = _useMemo2[1];
88
- var isEmptyTree = ((_props$data$filter = props.data.filter(function (item) {
89
- var _item;
90
- return (_item = item[props.treeColumnsName]) === null || _item === void 0 ? void 0 : _item.length;
91
- })) === null || _props$data$filter === void 0 ? void 0 : _props$data$filter.length) === 0;
97
+ treeData = _useMemo.treeData,
98
+ treeExpandLevel = _useMemo.treeExpandLevel,
99
+ unmatchExpandKeys = _useMemo.unmatchExpandKeys;
100
+ var isEmptyTree = useMemo(function () {
101
+ var _props$data$filter;
102
+ return ((_props$data$filter = props.data.filter(function (item) {
103
+ var _item;
104
+ return (_item = item[props.treeColumnsName]) === null || _item === void 0 ? void 0 : _item.length;
105
+ })) === null || _props$data$filter === void 0 ? void 0 : _props$data$filter.length) === 0;
106
+ }, [props.data, props.treeColumnsName]);
107
+ useEffect(function () {
108
+ if (!unmatchExpandKeys.length || !expandKeysState.length) {
109
+ return;
110
+ }
111
+
112
+ // 检查treeData中的每一项,对比expandKeysState,如果expandKeysState有但是children是空的,则需要修正expandKeysState
113
+ var newExpandKeys = expandKeysState.filter(function (key) {
114
+ return !unmatchExpandKeys.includes(key);
115
+ });
116
+ setExpandKeysState(newExpandKeys);
117
+ }, [unmatchExpandKeys, expandKeysState]);
92
118
  return {
93
119
  data: treeData,
94
120
  treeExpandLevel: treeExpandLevel,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sheinx/hooks",
3
- "version": "3.5.0-beta.10",
3
+ "version": "3.5.0-beta.12",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "license": "MIT",