@hi-ui/check-tree-select 4.5.0 → 4.6.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @hi-ui/check-tree-select
2
2
 
3
+ ## 4.6.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#2952](https://github.com/XiaoMi/hiui/pull/2952) [`f7137e3db`](https://github.com/XiaoMi/hiui/commit/f7137e3dbff8a3a2735f9e7d8a09fc942c8a8a86) Thanks [@xiamiao1121](https://github.com/xiamiao1121)! - feat: 新增支持过滤已选择项
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [[`bc306526c`](https://github.com/XiaoMi/hiui/commit/bc306526c948cb2b1ecd73b63e903d941c78dd9a), [`7bdd549d0`](https://github.com/XiaoMi/hiui/commit/7bdd549d0fcb6a6b903742333e352e184723aa3b)]:
12
+ - @hi-ui/tree@4.6.0
13
+
3
14
  ## 4.5.0
4
15
 
5
16
  ### Minor Changes
@@ -89,13 +89,15 @@ var CheckTreeSelect = /*#__PURE__*/React.forwardRef(function (_a, ref) {
89
89
  itemHeight = _a.itemHeight,
90
90
  height = _a.height,
91
91
  showCheckAll = _a.showCheckAll,
92
+ _a$showOnlyShowChecke = _a.showOnlyShowChecked,
93
+ showOnlyShowChecked = _a$showOnlyShowChecke === void 0 ? false : _a$showOnlyShowChecke,
92
94
  tagInputProps = _a.tagInputProps,
93
95
  _a$size = _a.size,
94
96
  size = _a$size === void 0 ? 'md' : _a$size,
95
97
  customRender = _a.customRender,
96
98
  prefix = _a.prefix,
97
99
  suffix = _a.suffix,
98
- rest = tslib.__rest(_a, ["prefixCls", "role", "className", "data", "dataSource", "disabled", "visible", "onOpen", "onClose", "fieldNames", "checkedMode", "defaultExpandAll", "expandedIds", "defaultExpandedIds", "onExpand", "defaultValue", "value", "onChange", "searchable", "searchMode", "onLoadChildren", "render", "filterOption", "onSearch", "clearable", "invalid", "displayRender", "placeholder", "appearance", "virtual", "itemHeight", "height", "showCheckAll", "tagInputProps", "size", "customRender", "prefix", "suffix"]);
100
+ rest = tslib.__rest(_a, ["prefixCls", "role", "className", "data", "dataSource", "disabled", "visible", "onOpen", "onClose", "fieldNames", "checkedMode", "defaultExpandAll", "expandedIds", "defaultExpandedIds", "onExpand", "defaultValue", "value", "onChange", "searchable", "searchMode", "onLoadChildren", "render", "filterOption", "onSearch", "clearable", "invalid", "displayRender", "placeholder", "appearance", "virtual", "itemHeight", "height", "showCheckAll", "showOnlyShowChecked", "tagInputProps", "size", "customRender", "prefix", "suffix"]);
99
101
  var i18n = core.useLocaleContext();
100
102
  var placeholder = typeAssertion.isUndef(placeholderProp) ? i18n.get('checkTreeSelect.placeholder') : placeholderProp;
101
103
  var _useUncontrolledToggl = useToggle.useUncontrolledToggle({
@@ -106,6 +108,11 @@ var CheckTreeSelect = /*#__PURE__*/React.forwardRef(function (_a, ref) {
106
108
  }),
107
109
  menuVisible = _useUncontrolledToggl[0],
108
110
  menuVisibleAction = _useUncontrolledToggl[1];
111
+ var _useState = React.useState(null),
112
+ filterItems = _useState[0],
113
+ setFilterItems = _useState[1];
114
+ var expandedViewRef = React.useRef('normal');
115
+ var activeExpandable = showOnlyShowChecked && !!filterItems && menuVisible;
109
116
  /**
110
117
  * 转换对象
111
118
  */
@@ -210,8 +217,9 @@ var CheckTreeSelect = /*#__PURE__*/React.forwardRef(function (_a, ref) {
210
217
  return ret;
211
218
  }, [titleRender, searchValue, searchMode]);
212
219
  var shouldUseSearch = !!searchValue && !hasError;
220
+ var showData = shouldUseSearch ? stateInSearch.data : data;
213
221
  var treeProps = {
214
- data: shouldUseSearch ? stateInSearch.data : data,
222
+ data: filterItems !== null && filterItems !== void 0 ? filterItems : showData,
215
223
  expandedIds: shouldUseSearch ? stateInSearch.expandedIds : expandedIds,
216
224
  onExpand: shouldUseSearch ? function (ids) {
217
225
  return setStateInSearch(function (prev) {
@@ -311,7 +319,62 @@ var CheckTreeSelect = /*#__PURE__*/React.forwardRef(function (_a, ref) {
311
319
  onChange: onValueChange,
312
320
  data: mergedData,
313
321
  // @ts-ignore
314
- invalid: invalid
322
+ invalid: invalid,
323
+ // onExpand={() => {
324
+ // // setViewSelected(true)
325
+ // menuVisibleAction.on()
326
+ // }}
327
+ onClick: function onClick(evt) {
328
+ if (!showOnlyShowChecked) return;
329
+ evt.preventDefault();
330
+ if (filterItems) {
331
+ setFilterItems(null);
332
+ }
333
+ if (menuVisible) {
334
+ if (expandedViewRef.current === 'normal') {
335
+ menuVisibleAction.off();
336
+ }
337
+ } else {
338
+ menuVisibleAction.on();
339
+ }
340
+ expandedViewRef.current = 'normal';
341
+ },
342
+ expandable: showOnlyShowChecked,
343
+ activeExpandable: activeExpandable,
344
+ onExpand: function onExpand(evt) {
345
+ if (!showOnlyShowChecked) return;
346
+ // 阻止冒泡触发外层 onClick
347
+ evt.preventDefault();
348
+ evt.stopPropagation();
349
+ // 选中数据
350
+ setFilterItems(function () {
351
+ var filterFunc = function filterFunc(node) {
352
+ var _a;
353
+ if (parsedCheckedIds.includes(node.id)) {
354
+ return true;
355
+ }
356
+ if (node.children && ((_a = node.children) === null || _a === void 0 ? void 0 : _a.length) > 0) {
357
+ return node.children.some(function (child) {
358
+ return filterFunc(child);
359
+ });
360
+ }
361
+ return false;
362
+ };
363
+ // filterTree 过滤树结构,将不包含value中的节点分支过滤掉
364
+ // 返回过滤后的树结构
365
+ var treeData = treeUtils.filterTree(data, filterFunc);
366
+ return treeData;
367
+ });
368
+ // 展开/关闭操作
369
+ if (menuVisible) {
370
+ if (expandedViewRef.current !== 'normal') {
371
+ menuVisibleAction.off();
372
+ }
373
+ } else {
374
+ menuVisibleAction.on();
375
+ }
376
+ expandedViewRef.current = 'onlyChecked';
377
+ }
315
378
  })))
316
379
  }), typeAssertion.isArrayNonEmpty(treeProps.data) ? ( /*#__PURE__*/
317
380
  // 只做渲染,不做逻辑处理(比如搜索过滤后,check操作的是对过滤后的data操作,这是不符合预期的)
@@ -8,14 +8,14 @@
8
8
  * LICENSE file in the root directory of this source tree.
9
9
  */
10
10
  import { __rest } from 'tslib';
11
- import React, { forwardRef, useCallback, useMemo } from 'react';
11
+ import React, { forwardRef, useState, useRef, useCallback, useMemo } from 'react';
12
12
  import { getPrefixCls, cx } from '@hi-ui/classname';
13
13
  import { __DEV__ } from '@hi-ui/env';
14
14
  import { useUncontrolledToggle } from '@hi-ui/use-toggle';
15
15
  import { Tree } from '@hi-ui/tree';
16
16
  import { useUncontrolledState } from '@hi-ui/use-uncontrolled-state';
17
17
  import { Picker } from '@hi-ui/picker';
18
- import { baseFlattenTree } from '@hi-ui/tree-utils';
18
+ import { baseFlattenTree, filterTree } from '@hi-ui/tree-utils';
19
19
  import { isUndef, isArrayNonEmpty } from '@hi-ui/type-assertion';
20
20
  import { uniqBy } from '@hi-ui/array-utils';
21
21
  import { Highlighter } from '@hi-ui/highlighter';
@@ -76,13 +76,15 @@ var CheckTreeSelect = /*#__PURE__*/forwardRef(function (_a, ref) {
76
76
  itemHeight = _a.itemHeight,
77
77
  height = _a.height,
78
78
  showCheckAll = _a.showCheckAll,
79
+ _a$showOnlyShowChecke = _a.showOnlyShowChecked,
80
+ showOnlyShowChecked = _a$showOnlyShowChecke === void 0 ? false : _a$showOnlyShowChecke,
79
81
  tagInputProps = _a.tagInputProps,
80
82
  _a$size = _a.size,
81
83
  size = _a$size === void 0 ? 'md' : _a$size,
82
84
  customRender = _a.customRender,
83
85
  prefix = _a.prefix,
84
86
  suffix = _a.suffix,
85
- rest = __rest(_a, ["prefixCls", "role", "className", "data", "dataSource", "disabled", "visible", "onOpen", "onClose", "fieldNames", "checkedMode", "defaultExpandAll", "expandedIds", "defaultExpandedIds", "onExpand", "defaultValue", "value", "onChange", "searchable", "searchMode", "onLoadChildren", "render", "filterOption", "onSearch", "clearable", "invalid", "displayRender", "placeholder", "appearance", "virtual", "itemHeight", "height", "showCheckAll", "tagInputProps", "size", "customRender", "prefix", "suffix"]);
87
+ rest = __rest(_a, ["prefixCls", "role", "className", "data", "dataSource", "disabled", "visible", "onOpen", "onClose", "fieldNames", "checkedMode", "defaultExpandAll", "expandedIds", "defaultExpandedIds", "onExpand", "defaultValue", "value", "onChange", "searchable", "searchMode", "onLoadChildren", "render", "filterOption", "onSearch", "clearable", "invalid", "displayRender", "placeholder", "appearance", "virtual", "itemHeight", "height", "showCheckAll", "showOnlyShowChecked", "tagInputProps", "size", "customRender", "prefix", "suffix"]);
86
88
  var i18n = useLocaleContext();
87
89
  var placeholder = isUndef(placeholderProp) ? i18n.get('checkTreeSelect.placeholder') : placeholderProp;
88
90
  var _useUncontrolledToggl = useUncontrolledToggle({
@@ -93,6 +95,11 @@ var CheckTreeSelect = /*#__PURE__*/forwardRef(function (_a, ref) {
93
95
  }),
94
96
  menuVisible = _useUncontrolledToggl[0],
95
97
  menuVisibleAction = _useUncontrolledToggl[1];
98
+ var _useState = useState(null),
99
+ filterItems = _useState[0],
100
+ setFilterItems = _useState[1];
101
+ var expandedViewRef = useRef('normal');
102
+ var activeExpandable = showOnlyShowChecked && !!filterItems && menuVisible;
96
103
  /**
97
104
  * 转换对象
98
105
  */
@@ -197,8 +204,9 @@ var CheckTreeSelect = /*#__PURE__*/forwardRef(function (_a, ref) {
197
204
  return ret;
198
205
  }, [titleRender, searchValue, searchMode]);
199
206
  var shouldUseSearch = !!searchValue && !hasError;
207
+ var showData = shouldUseSearch ? stateInSearch.data : data;
200
208
  var treeProps = {
201
- data: shouldUseSearch ? stateInSearch.data : data,
209
+ data: filterItems !== null && filterItems !== void 0 ? filterItems : showData,
202
210
  expandedIds: shouldUseSearch ? stateInSearch.expandedIds : expandedIds,
203
211
  onExpand: shouldUseSearch ? function (ids) {
204
212
  return setStateInSearch(function (prev) {
@@ -298,7 +306,62 @@ var CheckTreeSelect = /*#__PURE__*/forwardRef(function (_a, ref) {
298
306
  onChange: onValueChange,
299
307
  data: mergedData,
300
308
  // @ts-ignore
301
- invalid: invalid
309
+ invalid: invalid,
310
+ // onExpand={() => {
311
+ // // setViewSelected(true)
312
+ // menuVisibleAction.on()
313
+ // }}
314
+ onClick: function onClick(evt) {
315
+ if (!showOnlyShowChecked) return;
316
+ evt.preventDefault();
317
+ if (filterItems) {
318
+ setFilterItems(null);
319
+ }
320
+ if (menuVisible) {
321
+ if (expandedViewRef.current === 'normal') {
322
+ menuVisibleAction.off();
323
+ }
324
+ } else {
325
+ menuVisibleAction.on();
326
+ }
327
+ expandedViewRef.current = 'normal';
328
+ },
329
+ expandable: showOnlyShowChecked,
330
+ activeExpandable: activeExpandable,
331
+ onExpand: function onExpand(evt) {
332
+ if (!showOnlyShowChecked) return;
333
+ // 阻止冒泡触发外层 onClick
334
+ evt.preventDefault();
335
+ evt.stopPropagation();
336
+ // 选中数据
337
+ setFilterItems(function () {
338
+ var filterFunc = function filterFunc(node) {
339
+ var _a;
340
+ if (parsedCheckedIds.includes(node.id)) {
341
+ return true;
342
+ }
343
+ if (node.children && ((_a = node.children) === null || _a === void 0 ? void 0 : _a.length) > 0) {
344
+ return node.children.some(function (child) {
345
+ return filterFunc(child);
346
+ });
347
+ }
348
+ return false;
349
+ };
350
+ // filterTree 过滤树结构,将不包含value中的节点分支过滤掉
351
+ // 返回过滤后的树结构
352
+ var treeData = filterTree(data, filterFunc);
353
+ return treeData;
354
+ });
355
+ // 展开/关闭操作
356
+ if (menuVisible) {
357
+ if (expandedViewRef.current !== 'normal') {
358
+ menuVisibleAction.off();
359
+ }
360
+ } else {
361
+ menuVisibleAction.on();
362
+ }
363
+ expandedViewRef.current = 'onlyChecked';
364
+ }
302
365
  })))
303
366
  }), isArrayNonEmpty(treeProps.data) ? ( /*#__PURE__*/
304
367
  // 只做渲染,不做逻辑处理(比如搜索过滤后,check操作的是对过滤后的data操作,这是不符合预期的)
@@ -137,6 +137,10 @@ export interface CheckTreeSelectProps extends Omit<PickerProps, 'data' | 'onChan
137
137
  * 是否开启全选功能,需要对数据全量操作。异步数据场景暂不支持,可自行渲染弹层底部实现
138
138
  */
139
139
  showCheckAll?: boolean;
140
+ /**
141
+ * 是否开启查看仅已选功能
142
+ */
143
+ showOnlyShowChecked?: boolean;
140
144
  /**
141
145
  * TagInput 参数设置
142
146
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hi-ui/check-tree-select",
3
- "version": "4.5.0",
3
+ "version": "4.6.0",
4
4
  "description": "A sub-package for @hi-ui/hiui.",
5
5
  "keywords": [],
6
6
  "author": "HiUI <mi-hiui@xiaomi.com>",
@@ -53,7 +53,7 @@
53
53
  "@hi-ui/picker": "^4.1.6",
54
54
  "@hi-ui/popper": "^4.1.5",
55
55
  "@hi-ui/tag-input": "^4.1.1",
56
- "@hi-ui/tree": "^4.5.10",
56
+ "@hi-ui/tree": "^4.6.0",
57
57
  "@hi-ui/tree-utils": "^4.1.6",
58
58
  "@hi-ui/type-assertion": "^4.0.4",
59
59
  "@hi-ui/use-check": "^4.0.5",