@hi-ui/check-tree-select 4.4.1 → 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,22 @@
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
+
14
+ ## 4.5.0
15
+
16
+ ### Minor Changes
17
+
18
+ - [#2878](https://github.com/XiaoMi/hiui/pull/2878) [`179fd7fef`](https://github.com/XiaoMi/hiui/commit/179fd7fef9d906ecec2c5f02de01b7fa16721c79) Thanks [@yang-x20](https://github.com/yang-x20)! - feat: 新增前置后置内容扩展功能
19
+
3
20
  ## 4.4.1
4
21
 
5
22
  ### Patch Changes
@@ -89,11 +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
- 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"]);
98
+ prefix = _a.prefix,
99
+ suffix = _a.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"]);
97
101
  var i18n = core.useLocaleContext();
98
102
  var placeholder = typeAssertion.isUndef(placeholderProp) ? i18n.get('checkTreeSelect.placeholder') : placeholderProp;
99
103
  var _useUncontrolledToggl = useToggle.useUncontrolledToggle({
@@ -104,6 +108,11 @@ var CheckTreeSelect = /*#__PURE__*/React.forwardRef(function (_a, ref) {
104
108
  }),
105
109
  menuVisible = _useUncontrolledToggl[0],
106
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;
107
116
  /**
108
117
  * 转换对象
109
118
  */
@@ -208,8 +217,9 @@ var CheckTreeSelect = /*#__PURE__*/React.forwardRef(function (_a, ref) {
208
217
  return ret;
209
218
  }, [titleRender, searchValue, searchMode]);
210
219
  var shouldUseSearch = !!searchValue && !hasError;
220
+ var showData = shouldUseSearch ? stateInSearch.data : data;
211
221
  var treeProps = {
212
- data: shouldUseSearch ? stateInSearch.data : data,
222
+ data: filterItems !== null && filterItems !== void 0 ? filterItems : showData,
213
223
  expandedIds: shouldUseSearch ? stateInSearch.expandedIds : expandedIds,
214
224
  onExpand: shouldUseSearch ? function (ids) {
215
225
  return setStateInSearch(function (prev) {
@@ -301,14 +311,70 @@ var CheckTreeSelect = /*#__PURE__*/React.forwardRef(function (_a, ref) {
301
311
  placeholder: placeholder,
302
312
  // @ts-ignore
303
313
  displayRender: displayRender,
304
- suffix: menuVisible ? /*#__PURE__*/React__default["default"].createElement(icons.UpOutlined, null) : /*#__PURE__*/React__default["default"].createElement(icons.DownOutlined, null),
314
+ prefix: prefix,
315
+ suffix: [menuVisible ? /*#__PURE__*/React__default["default"].createElement(icons.UpOutlined, null) : /*#__PURE__*/React__default["default"].createElement(icons.DownOutlined, null), suffix],
305
316
  focused: menuVisible,
306
317
  appearance: appearance,
307
318
  value: value,
308
319
  onChange: onValueChange,
309
320
  data: mergedData,
310
321
  // @ts-ignore
311
- 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
+ }
312
378
  })))
313
379
  }), typeAssertion.isArrayNonEmpty(treeProps.data) ? ( /*#__PURE__*/
314
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,11 +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
- 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"]);
85
+ prefix = _a.prefix,
86
+ suffix = _a.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"]);
84
88
  var i18n = useLocaleContext();
85
89
  var placeholder = isUndef(placeholderProp) ? i18n.get('checkTreeSelect.placeholder') : placeholderProp;
86
90
  var _useUncontrolledToggl = useUncontrolledToggle({
@@ -91,6 +95,11 @@ var CheckTreeSelect = /*#__PURE__*/forwardRef(function (_a, ref) {
91
95
  }),
92
96
  menuVisible = _useUncontrolledToggl[0],
93
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;
94
103
  /**
95
104
  * 转换对象
96
105
  */
@@ -195,8 +204,9 @@ var CheckTreeSelect = /*#__PURE__*/forwardRef(function (_a, ref) {
195
204
  return ret;
196
205
  }, [titleRender, searchValue, searchMode]);
197
206
  var shouldUseSearch = !!searchValue && !hasError;
207
+ var showData = shouldUseSearch ? stateInSearch.data : data;
198
208
  var treeProps = {
199
- data: shouldUseSearch ? stateInSearch.data : data,
209
+ data: filterItems !== null && filterItems !== void 0 ? filterItems : showData,
200
210
  expandedIds: shouldUseSearch ? stateInSearch.expandedIds : expandedIds,
201
211
  onExpand: shouldUseSearch ? function (ids) {
202
212
  return setStateInSearch(function (prev) {
@@ -288,14 +298,70 @@ var CheckTreeSelect = /*#__PURE__*/forwardRef(function (_a, ref) {
288
298
  placeholder: placeholder,
289
299
  // @ts-ignore
290
300
  displayRender: displayRender,
291
- suffix: menuVisible ? /*#__PURE__*/React.createElement(UpOutlined, null) : /*#__PURE__*/React.createElement(DownOutlined, null),
301
+ prefix: prefix,
302
+ suffix: [menuVisible ? /*#__PURE__*/React.createElement(UpOutlined, null) : /*#__PURE__*/React.createElement(DownOutlined, null), suffix],
292
303
  focused: menuVisible,
293
304
  appearance: appearance,
294
305
  value: value,
295
306
  onChange: onValueChange,
296
307
  data: mergedData,
297
308
  // @ts-ignore
298
- 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
+ }
299
365
  })))
300
366
  }), isArrayNonEmpty(treeProps.data) ? ( /*#__PURE__*/
301
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
  */
@@ -149,6 +153,14 @@ export interface CheckTreeSelectProps extends Omit<PickerProps, 'data' | 'onChan
149
153
  * 自定义触发器
150
154
  */
151
155
  customRender?: React.ReactNode | ((option: CheckTreeSelectDataItem[]) => React.ReactNode);
156
+ /**
157
+ * 选择框前置内容
158
+ */
159
+ prefix?: React.ReactNode;
160
+ /**
161
+ * 选择框后置内容
162
+ */
163
+ suffix?: React.ReactNode;
152
164
  }
153
165
  /**
154
166
  * 在 checkedIdsSet 为数据合法的情况下,查找所有的半选中态的节点 ids
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hi-ui/check-tree-select",
3
- "version": "4.4.1",
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",