@hi-ui/check-select 4.0.6 → 4.0.8

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.
@@ -0,0 +1,43 @@
1
+ /** @LICENSE
2
+ * @hi-ui/check-select
3
+ * https://github.com/XiaoMi/hiui/tree/master/packages/ui/check-select#readme
4
+ *
5
+ * Copyright (c) HiUI <mi-hiui@xiaomi.com>.
6
+ *
7
+ * This source code is licensed under the MIT license found in the
8
+ * LICENSE file in the root directory of this source tree.
9
+ */
10
+ 'use strict';
11
+
12
+ Object.defineProperty(exports, '__esModule', {
13
+ value: true
14
+ });
15
+
16
+ var React = require('react');
17
+ /** @LICENSE
18
+ * @hi-ui/use-cache
19
+ * https://github.com/XiaoMi/hiui/tree/master/packages/hooks/use-cache#readme
20
+ *
21
+ * Copyright (c) HiUI <mi-hiui@xiaomi.com>.
22
+ *
23
+ * This source code is licensed under the MIT license found in the
24
+ * LICENSE file in the root directory of this source tree.
25
+ */
26
+
27
+ /**
28
+ * A hook using for data cache that compatible with the controlled and uncontrolled modes coexist.
29
+ */
30
+
31
+
32
+ var useCache = function useCache(data) {
33
+ var _useState = React.useState(data),
34
+ internalData = _useState[0],
35
+ setInternalData = _useState[1];
36
+
37
+ React.useEffect(function () {
38
+ setInternalData(data);
39
+ }, [data]);
40
+ return [internalData, setInternalData];
41
+ };
42
+
43
+ exports.useCache = useCache;
@@ -25,6 +25,8 @@ var useCheck = require('@hi-ui/use-check');
25
25
 
26
26
  var useLatest = require('@hi-ui/use-latest');
27
27
 
28
+ var index = require('./hooks/use-cache/lib/esm/index.js');
29
+
28
30
  var useData = require('./hooks/use-data.js');
29
31
 
30
32
  var useFlattenData = require('./hooks/use-flatten-data.js');
@@ -54,8 +56,13 @@ var useCheckSelect = function useCheckSelect(_a) {
54
56
  data: dataProp,
55
57
  children: children
56
58
  });
59
+
60
+ var _useCache = index.useCache(data),
61
+ cacheData = _useCache[0],
62
+ setCacheData = _useCache[1];
63
+
57
64
  var flattedData = useFlattenData.useFlattenData({
58
- data: data,
65
+ data: cacheData,
59
66
  fieldNames: fieldNames
60
67
  });
61
68
  var flattedDataRef = useLatest.useLatestRef(flattedData);
@@ -70,7 +77,23 @@ var useCheckSelect = function useCheckSelect(_a) {
70
77
  var _useState = React.useState([]),
71
78
  checkedItems = _useState[0],
72
79
  setCheckedItems = _useState[1];
73
-
80
+ /**
81
+ * 更新缓存的 data 数据
82
+ * 兼容在搜索异步加载结果的场景时,将选中的项加入到 cacheData 数据中,这样再次打开下拉框时可以显示之前选中的值
83
+ */
84
+
85
+
86
+ var updateCacheData = React.useCallback(function (nextCheckedItems) {
87
+ var newData = [].concat(cacheData);
88
+ nextCheckedItems.forEach(function (item) {
89
+ if (!flattedData.find(function (dataItem) {
90
+ return dataItem.id === item.id;
91
+ })) {
92
+ newData.push('raw' in item ? item.raw : item);
93
+ }
94
+ });
95
+ setCacheData(newData);
96
+ }, [cacheData, flattedData, setCacheData]);
74
97
  var proxyTryChangeValue = React.useCallback(function (value, item, shouldChecked) {
75
98
  var changedItems = item;
76
99
 
@@ -91,8 +114,10 @@ var useCheckSelect = function useCheckSelect(_a) {
91
114
  return 'raw' in item ? item.raw : item;
92
115
  }), nextCheckedItems.map(function (item) {
93
116
  return 'raw' in item ? item.raw : item;
94
- }));
95
- }, [tryChangeValue, onSelectLatest, flattedDataRef, usedItemsRef]);
117
+ })); // 每次更新 value 时,同步更新下 cacheData
118
+
119
+ updateCacheData(nextCheckedItems);
120
+ }, [flattedDataRef, tryChangeValue, updateCacheData, onSelectLatest]);
96
121
 
97
122
  var _useCheckDefault = useCheck.useCheck({
98
123
  disabled: disabled,
@@ -0,0 +1,36 @@
1
+ /** @LICENSE
2
+ * @hi-ui/check-select
3
+ * https://github.com/XiaoMi/hiui/tree/master/packages/ui/check-select#readme
4
+ *
5
+ * Copyright (c) HiUI <mi-hiui@xiaomi.com>.
6
+ *
7
+ * This source code is licensed under the MIT license found in the
8
+ * LICENSE file in the root directory of this source tree.
9
+ */
10
+ import { useState, useEffect } from 'react';
11
+ /** @LICENSE
12
+ * @hi-ui/use-cache
13
+ * https://github.com/XiaoMi/hiui/tree/master/packages/hooks/use-cache#readme
14
+ *
15
+ * Copyright (c) HiUI <mi-hiui@xiaomi.com>.
16
+ *
17
+ * This source code is licensed under the MIT license found in the
18
+ * LICENSE file in the root directory of this source tree.
19
+ */
20
+
21
+ /**
22
+ * A hook using for data cache that compatible with the controlled and uncontrolled modes coexist.
23
+ */
24
+
25
+ var useCache = function useCache(data) {
26
+ var _useState = useState(data),
27
+ internalData = _useState[0],
28
+ setInternalData = _useState[1];
29
+
30
+ useEffect(function () {
31
+ setInternalData(data);
32
+ }, [data]);
33
+ return [internalData, setInternalData];
34
+ };
35
+
36
+ export { useCache };
@@ -13,6 +13,7 @@ import { useUncontrolledState } from '@hi-ui/use-uncontrolled-state';
13
13
  import { uniqBy } from '@hi-ui/array-utils';
14
14
  import { useCheck } from '@hi-ui/use-check';
15
15
  import { useLatestRef, useLatestCallback } from '@hi-ui/use-latest';
16
+ import { useCache } from './hooks/use-cache/lib/esm/index.js';
16
17
  import { useData } from './hooks/use-data.js';
17
18
  import { useFlattenData } from './hooks/use-flatten-data.js';
18
19
  import '@hi-ui/env';
@@ -38,8 +39,13 @@ var useCheckSelect = function useCheckSelect(_a) {
38
39
  data: dataProp,
39
40
  children: children
40
41
  });
42
+
43
+ var _useCache = useCache(data),
44
+ cacheData = _useCache[0],
45
+ setCacheData = _useCache[1];
46
+
41
47
  var flattedData = useFlattenData({
42
- data: data,
48
+ data: cacheData,
43
49
  fieldNames: fieldNames
44
50
  });
45
51
  var flattedDataRef = useLatestRef(flattedData);
@@ -54,7 +60,23 @@ var useCheckSelect = function useCheckSelect(_a) {
54
60
  var _useState = useState([]),
55
61
  checkedItems = _useState[0],
56
62
  setCheckedItems = _useState[1];
63
+ /**
64
+ * 更新缓存的 data 数据
65
+ * 兼容在搜索异步加载结果的场景时,将选中的项加入到 cacheData 数据中,这样再次打开下拉框时可以显示之前选中的值
66
+ */
57
67
 
68
+
69
+ var updateCacheData = useCallback(function (nextCheckedItems) {
70
+ var newData = [].concat(cacheData);
71
+ nextCheckedItems.forEach(function (item) {
72
+ if (!flattedData.find(function (dataItem) {
73
+ return dataItem.id === item.id;
74
+ })) {
75
+ newData.push('raw' in item ? item.raw : item);
76
+ }
77
+ });
78
+ setCacheData(newData);
79
+ }, [cacheData, flattedData, setCacheData]);
58
80
  var proxyTryChangeValue = useCallback(function (value, item, shouldChecked) {
59
81
  var changedItems = item;
60
82
 
@@ -75,8 +97,10 @@ var useCheckSelect = function useCheckSelect(_a) {
75
97
  return 'raw' in item ? item.raw : item;
76
98
  }), nextCheckedItems.map(function (item) {
77
99
  return 'raw' in item ? item.raw : item;
78
- }));
79
- }, [tryChangeValue, onSelectLatest, flattedDataRef, usedItemsRef]);
100
+ })); // 每次更新 value 时,同步更新下 cacheData
101
+
102
+ updateCacheData(nextCheckedItems);
103
+ }, [flattedDataRef, tryChangeValue, updateCacheData, onSelectLatest]);
80
104
 
81
105
  var _useCheckDefault = useCheck({
82
106
  disabled: disabled,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hi-ui/check-select",
3
- "version": "4.0.6",
3
+ "version": "4.0.8",
4
4
  "description": "A sub-package for @hi-ui/hiui.",
5
5
  "keywords": [],
6
6
  "author": "HiUI <mi-hiui@xiaomi.com>",
@@ -50,10 +50,10 @@
50
50
  "@hi-ui/func-utils": "^4.0.1",
51
51
  "@hi-ui/highlighter": "^4.0.5",
52
52
  "@hi-ui/icons": "^4.0.4",
53
- "@hi-ui/input": "^4.0.4",
54
- "@hi-ui/picker": "^4.0.4",
53
+ "@hi-ui/input": "^4.0.5",
54
+ "@hi-ui/picker": "^4.0.5",
55
55
  "@hi-ui/popper": "^4.0.4",
56
- "@hi-ui/tag-input": "^4.0.4",
56
+ "@hi-ui/tag-input": "^4.0.5",
57
57
  "@hi-ui/times": "^4.0.1",
58
58
  "@hi-ui/tree-utils": "^4.0.2",
59
59
  "@hi-ui/type-assertion": "^4.0.1",
@@ -78,5 +78,5 @@
78
78
  "react": "^17.0.1",
79
79
  "react-dom": "^17.0.1"
80
80
  },
81
- "gitHead": "864630e4ef35b8a3c85e56dcb5e2948bf1f9728f"
81
+ "gitHead": "db81f4e6bffc562f26ab0655cb71ec160c5e2c22"
82
82
  }