@hi-ui/cascader 4.0.0-beta.42 → 4.0.0-beta.45

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.
@@ -53,6 +53,8 @@ var funcUtils = require('@hi-ui/func-utils');
53
53
 
54
54
  var CascaderMenuList = require('./CascaderMenuList.js');
55
55
 
56
+ var Highlighter = require('@hi-ui/highlighter');
57
+
56
58
  function _interopDefaultLegacy(e) {
57
59
  return e && _typeof(e) === 'object' && 'default' in e ? e : {
58
60
  'default': e
@@ -61,6 +63,8 @@ function _interopDefaultLegacy(e) {
61
63
 
62
64
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
63
65
 
66
+ var Highlighter__default = /*#__PURE__*/_interopDefaultLegacy(Highlighter);
67
+
64
68
  var _prefix = classname.getPrefixCls('cascader');
65
69
 
66
70
  var NOOP_ARRAY = [];
@@ -95,14 +99,15 @@ var Cascader = /*#__PURE__*/React.forwardRef(function (props, ref) {
95
99
  overlayClassName = props.overlayClassName,
96
100
  _props$data = props.data,
97
101
  data = _props$data === void 0 ? NOOP_ARRAY : _props$data,
102
+ _props$flattedSearchR = props.flattedSearchResult,
103
+ flattedSearchResult = _props$flattedSearchR === void 0 ? true : _props$flattedSearchR,
98
104
  visible = props.visible,
99
105
  onOpen = props.onOpen,
100
106
  onClose = props.onClose,
101
- rest = tslib.__rest(props, ["prefixCls", "className", "placeholder", "disabled", "clearable", "type", "fieldNames", "expandTrigger", "displayRender", "onSelect", "onLoadChildren", "appearance", "invalid", "filterOption", "searchable", "onSearch", "render", "overlayClassName", "data", "visible", "onOpen", "onClose"]);
107
+ rest = tslib.__rest(props, ["prefixCls", "className", "placeholder", "disabled", "clearable", "type", "fieldNames", "expandTrigger", "displayRender", "onSelect", "onLoadChildren", "appearance", "invalid", "filterOption", "searchable", "onSearch", "render", "overlayClassName", "data", "flattedSearchResult", "visible", "onOpen", "onClose"]);
102
108
 
103
109
  var i18n = localeContext.useLocaleContext();
104
110
  var placeholder = typeAssertion.isUndef(placeholderProp) ? i18n.get('cascader.placeholder') : placeholderProp;
105
- var flatted = type === 'flatted';
106
111
 
107
112
  var _useUncontrolledToggl = useToggle.useUncontrolledToggle({
108
113
  visible: visible,
@@ -131,7 +136,7 @@ var Cascader = /*#__PURE__*/React.forwardRef(function (props, ref) {
131
136
  var highlight = !!searchValue && searchMode === 'upMatch';
132
137
 
133
138
  if (highlight) {
134
- return renderHighlightTitle(searchValue, node, titleRender);
139
+ return flattedSearchResult ? renderHighlightTitles(searchValue, node, titleRender) : renderHighlightTitle(searchValue, node, titleRender);
135
140
  }
136
141
 
137
142
  return typeAssertion.isFunction(titleRender) ? titleRender(node) : true;
@@ -197,11 +202,14 @@ var Cascader = /*#__PURE__*/React.forwardRef(function (props, ref) {
197
202
  }, mergedTitle);
198
203
  };
199
204
 
205
+ var shouldUseSearch = !!searchValue; // 搜索的结果列表也采用 flatted 模式进行展示
206
+
207
+ var flatted = shouldUseSearch ? flattedSearchResult : type === 'flatted';
208
+
200
209
  var _a = useCascader.useCascader(Object.assign(Object.assign({}, rest), {
201
210
  disabled: disabled,
202
211
  fieldNames: fieldNames,
203
- // 搜索的结果列表也采用 flatted 模式进行展示
204
- flatted: flatted || !!searchValue,
212
+ flatted: flatted,
205
213
  onSelect: onSelect,
206
214
  onLoadChildren: onLoadChildren,
207
215
  data: data,
@@ -218,14 +226,17 @@ var Cascader = /*#__PURE__*/React.forwardRef(function (props, ref) {
218
226
  reset = context$1.reset,
219
227
  menuList = context$1.menuList,
220
228
  getItemRequiredProps = context$1.getItemRequiredProps;
221
- var shouldUseSearch = !!searchValue;
222
229
  var showData = React.useMemo(function () {
223
230
  if (shouldUseSearch) {
231
+ if (!flattedSearchResult) {
232
+ return index$1.getFilteredMenuList(menuList, stateInSearch.data);
233
+ }
234
+
224
235
  return typeAssertion.isArrayNonEmpty(stateInSearch.data) ? [stateInSearch.data] : [];
225
236
  }
226
237
 
227
238
  return menuList;
228
- }, [shouldUseSearch, stateInSearch.data, menuList]);
239
+ }, [shouldUseSearch, flattedSearchResult, stateInSearch.data, menuList]);
229
240
  React.useEffect(function () {
230
241
  // 关闭展示后,重置展开要高亮的选项
231
242
  if (!menuVisible) {
@@ -288,6 +299,16 @@ var renderHighlightTitle = function renderHighlightTitle(keyword, option, titleR
288
299
  // 如果 titleRender 返回 `true`,则使用默认 title
289
300
  var title = titleRender ? titleRender(option, keyword) : true;
290
301
  if (title !== true) return title;
302
+ return /*#__PURE__*/React__default["default"].createElement(Highlighter__default["default"], {
303
+ key: option.id,
304
+ keyword: keyword
305
+ }, option.title);
306
+ };
307
+
308
+ var renderHighlightTitles = function renderHighlightTitles(keyword, option, titleRender) {
309
+ // 如果 titleRender 返回 `true`,则使用默认 title
310
+ var title = titleRender ? titleRender(option, keyword) : true;
311
+ if (title !== true) return title;
291
312
 
292
313
  if (typeof option.title !== 'string') {
293
314
  console.info('WARNING: The `option.title` should be `string` when searchable is enabled.');
@@ -110,9 +110,38 @@ function getItemEventData(node, requiredProps) {
110
110
  return Object.assign(Object.assign({}, node), requiredProps);
111
111
  }
112
112
 
113
+ var getFilteredMenuList = function getFilteredMenuList(menuList, searchedData) {
114
+ var result = [];
115
+ searchedData.forEach(function (item) {
116
+ while (item && item.depth >= 0) {
117
+ var depth = item.depth;
118
+ var depthResult = result[depth];
119
+
120
+ if (!depthResult) {
121
+ depthResult = new Map();
122
+ result[depth] = depthResult;
123
+ }
124
+
125
+ depthResult.set(item.id, item);
126
+ item = item.parent;
127
+ }
128
+ });
129
+ return menuList.map(function (depthItems, depth) {
130
+ var depthSavedMp = result[depth];
131
+ if (!depthSavedMp) return depthItems;
132
+ return depthItems.filter(function (item) {
133
+ var depthSavedMp = result[item.depth];
134
+ if (!depthSavedMp) return true;
135
+ if (depthSavedMp.has(item.id)) return true;
136
+ return false;
137
+ });
138
+ });
139
+ };
140
+
113
141
  exports.checkCanLoadChildren = checkCanLoadChildren;
114
142
  exports.flattenTreeData = flattenTreeData;
115
143
  exports.getActiveMenus = getActiveMenus;
116
144
  exports.getActiveNodePaths = getActiveNodePaths;
145
+ exports.getFilteredMenuList = getFilteredMenuList;
117
146
  exports.getFlattedMenus = getFlattedMenus;
118
147
  exports.getItemEventData = getItemEventData;
@@ -15,7 +15,7 @@ import { useUncontrolledToggle } from '@hi-ui/use-toggle';
15
15
  import { useCascader } from './use-cascader.js';
16
16
  import { MockInput } from '@hi-ui/input';
17
17
  import { UpOutlined, DownOutlined } from '@hi-ui/icons';
18
- import { flattenTreeData, getItemEventData } from './utils/index.js';
18
+ import { flattenTreeData, getFilteredMenuList, getItemEventData } from './utils/index.js';
19
19
  import { CascaderProvider } from './context.js';
20
20
  import { getTopDownAncestors, getNodeAncestorsWithMe } from '@hi-ui/tree-utils';
21
21
  import { isUndef, isArrayNonEmpty, isFunction } from '@hi-ui/type-assertion';
@@ -26,6 +26,7 @@ import { useCache } from './hooks/use-cache/lib/esm/index.js';
26
26
  import { useLocaleContext } from '@hi-ui/locale-context';
27
27
  import { callAllFuncs } from '@hi-ui/func-utils';
28
28
  import { CascaderMenuList } from './CascaderMenuList.js';
29
+ import Highlighter from '@hi-ui/highlighter';
29
30
 
30
31
  var _prefix = getPrefixCls('cascader');
31
32
 
@@ -61,14 +62,15 @@ var Cascader = /*#__PURE__*/forwardRef(function (props, ref) {
61
62
  overlayClassName = props.overlayClassName,
62
63
  _props$data = props.data,
63
64
  data = _props$data === void 0 ? NOOP_ARRAY : _props$data,
65
+ _props$flattedSearchR = props.flattedSearchResult,
66
+ flattedSearchResult = _props$flattedSearchR === void 0 ? true : _props$flattedSearchR,
64
67
  visible = props.visible,
65
68
  onOpen = props.onOpen,
66
69
  onClose = props.onClose,
67
- rest = __rest(props, ["prefixCls", "className", "placeholder", "disabled", "clearable", "type", "fieldNames", "expandTrigger", "displayRender", "onSelect", "onLoadChildren", "appearance", "invalid", "filterOption", "searchable", "onSearch", "render", "overlayClassName", "data", "visible", "onOpen", "onClose"]);
70
+ rest = __rest(props, ["prefixCls", "className", "placeholder", "disabled", "clearable", "type", "fieldNames", "expandTrigger", "displayRender", "onSelect", "onLoadChildren", "appearance", "invalid", "filterOption", "searchable", "onSearch", "render", "overlayClassName", "data", "flattedSearchResult", "visible", "onOpen", "onClose"]);
68
71
 
69
72
  var i18n = useLocaleContext();
70
73
  var placeholder = isUndef(placeholderProp) ? i18n.get('cascader.placeholder') : placeholderProp;
71
- var flatted = type === 'flatted';
72
74
 
73
75
  var _useUncontrolledToggl = useUncontrolledToggle({
74
76
  visible: visible,
@@ -97,7 +99,7 @@ var Cascader = /*#__PURE__*/forwardRef(function (props, ref) {
97
99
  var highlight = !!searchValue && searchMode === 'upMatch';
98
100
 
99
101
  if (highlight) {
100
- return renderHighlightTitle(searchValue, node, titleRender);
102
+ return flattedSearchResult ? renderHighlightTitles(searchValue, node, titleRender) : renderHighlightTitle(searchValue, node, titleRender);
101
103
  }
102
104
 
103
105
  return isFunction(titleRender) ? titleRender(node) : true;
@@ -163,11 +165,14 @@ var Cascader = /*#__PURE__*/forwardRef(function (props, ref) {
163
165
  }, mergedTitle);
164
166
  };
165
167
 
168
+ var shouldUseSearch = !!searchValue; // 搜索的结果列表也采用 flatted 模式进行展示
169
+
170
+ var flatted = shouldUseSearch ? flattedSearchResult : type === 'flatted';
171
+
166
172
  var _a = useCascader(Object.assign(Object.assign({}, rest), {
167
173
  disabled: disabled,
168
174
  fieldNames: fieldNames,
169
- // 搜索的结果列表也采用 flatted 模式进行展示
170
- flatted: flatted || !!searchValue,
175
+ flatted: flatted,
171
176
  onSelect: onSelect,
172
177
  onLoadChildren: onLoadChildren,
173
178
  data: data,
@@ -184,14 +189,17 @@ var Cascader = /*#__PURE__*/forwardRef(function (props, ref) {
184
189
  reset = context.reset,
185
190
  menuList = context.menuList,
186
191
  getItemRequiredProps = context.getItemRequiredProps;
187
- var shouldUseSearch = !!searchValue;
188
192
  var showData = useMemo(function () {
189
193
  if (shouldUseSearch) {
194
+ if (!flattedSearchResult) {
195
+ return getFilteredMenuList(menuList, stateInSearch.data);
196
+ }
197
+
190
198
  return isArrayNonEmpty(stateInSearch.data) ? [stateInSearch.data] : [];
191
199
  }
192
200
 
193
201
  return menuList;
194
- }, [shouldUseSearch, stateInSearch.data, menuList]);
202
+ }, [shouldUseSearch, flattedSearchResult, stateInSearch.data, menuList]);
195
203
  useEffect(function () {
196
204
  // 关闭展示后,重置展开要高亮的选项
197
205
  if (!menuVisible) {
@@ -254,6 +262,16 @@ var renderHighlightTitle = function renderHighlightTitle(keyword, option, titleR
254
262
  // 如果 titleRender 返回 `true`,则使用默认 title
255
263
  var title = titleRender ? titleRender(option, keyword) : true;
256
264
  if (title !== true) return title;
265
+ return /*#__PURE__*/React.createElement(Highlighter, {
266
+ key: option.id,
267
+ keyword: keyword
268
+ }, option.title);
269
+ };
270
+
271
+ var renderHighlightTitles = function renderHighlightTitles(keyword, option, titleRender) {
272
+ // 如果 titleRender 返回 `true`,则使用默认 title
273
+ var title = titleRender ? titleRender(option, keyword) : true;
274
+ if (title !== true) return title;
257
275
 
258
276
  if (typeof option.title !== 'string') {
259
277
  console.info('WARNING: The `option.title` should be `string` when searchable is enabled.');
@@ -102,4 +102,32 @@ function getItemEventData(node, requiredProps) {
102
102
  return Object.assign(Object.assign({}, node), requiredProps);
103
103
  }
104
104
 
105
- export { checkCanLoadChildren, flattenTreeData, getActiveMenus, getActiveNodePaths, getFlattedMenus, getItemEventData };
105
+ var getFilteredMenuList = function getFilteredMenuList(menuList, searchedData) {
106
+ var result = [];
107
+ searchedData.forEach(function (item) {
108
+ while (item && item.depth >= 0) {
109
+ var depth = item.depth;
110
+ var depthResult = result[depth];
111
+
112
+ if (!depthResult) {
113
+ depthResult = new Map();
114
+ result[depth] = depthResult;
115
+ }
116
+
117
+ depthResult.set(item.id, item);
118
+ item = item.parent;
119
+ }
120
+ });
121
+ return menuList.map(function (depthItems, depth) {
122
+ var depthSavedMp = result[depth];
123
+ if (!depthSavedMp) return depthItems;
124
+ return depthItems.filter(function (item) {
125
+ var depthSavedMp = result[item.depth];
126
+ if (!depthSavedMp) return true;
127
+ if (depthSavedMp.has(item.id)) return true;
128
+ return false;
129
+ });
130
+ });
131
+ };
132
+
133
+ export { checkCanLoadChildren, flattenTreeData, getActiveMenus, getActiveNodePaths, getFilteredMenuList, getFlattedMenus, getItemEventData };
@@ -61,4 +61,8 @@ export interface CascaderProps extends Omit<PickerProps, 'data' | 'onChange' | '
61
61
  * 设置展现形式
62
62
  */
63
63
  appearance?: HiBaseAppearanceEnum;
64
+ /**
65
+ * 搜索结果拍平展示
66
+ */
67
+ flattedSearchResult?: boolean;
64
68
  }
@@ -16,3 +16,4 @@ export declare const getActiveMenus: (flattedData: FlattedCascaderDataItem[], se
16
16
  export declare const getFlattedMenus: (data: FlattedCascaderDataItem[], filter: (option: FlattedCascaderDataItem) => boolean) => FlattedCascaderDataItem[][];
17
17
  export declare const checkCanLoadChildren: (node: FlattedCascaderDataItem, onLoadChildren?: any) => any;
18
18
  export declare function getItemEventData(node: FlattedCascaderDataItem, requiredProps: CascaderItemRequiredProps): CascaderItemEventData;
19
+ export declare const getFilteredMenuList: (menuList: FlattedCascaderDataItem[][], searchedData: any[]) => FlattedCascaderDataItem[][];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hi-ui/cascader",
3
- "version": "4.0.0-beta.42",
3
+ "version": "4.0.0-beta.45",
4
4
  "description": "A sub-package for @hi-ui/hiui.",
5
5
  "keywords": [],
6
6
  "author": "HIUI <mi-hiui@xiaomi.com>",
@@ -45,22 +45,23 @@
45
45
  "dependencies": {
46
46
  "@hi-ui/array-utils": "^4.0.0-beta.8",
47
47
  "@hi-ui/classname": "^4.0.0-beta.0",
48
- "@hi-ui/core": "^4.0.0-beta.9",
48
+ "@hi-ui/core": "^4.0.0-beta.10",
49
49
  "@hi-ui/env": "^4.0.0-beta.1",
50
50
  "@hi-ui/func-utils": "^4.0.0-beta.13",
51
- "@hi-ui/icons": "^4.0.0-beta.14",
52
- "@hi-ui/input": "^4.0.0-beta.21",
53
- "@hi-ui/locale-context": "^4.0.0-beta.19",
54
- "@hi-ui/picker": "^4.0.0-beta.36",
55
- "@hi-ui/popper": "^4.0.0-beta.19",
56
- "@hi-ui/spinner": "^4.0.0-beta.11",
57
- "@hi-ui/tree-utils": "^4.0.0-beta.5",
51
+ "@hi-ui/highlighter": "^4.0.0-beta.12",
52
+ "@hi-ui/icons": "^4.0.0-beta.15",
53
+ "@hi-ui/input": "^4.0.0-beta.23",
54
+ "@hi-ui/locale-context": "^4.0.0-beta.20",
55
+ "@hi-ui/picker": "^4.0.0-beta.38",
56
+ "@hi-ui/popper": "^4.0.0-beta.20",
57
+ "@hi-ui/spinner": "^4.0.0-beta.12",
58
+ "@hi-ui/tree-utils": "^4.0.0-beta.6",
58
59
  "@hi-ui/type-assertion": "^4.0.0-beta.5",
59
60
  "@hi-ui/use-check-state": "^4.0.0-beta.5",
60
61
  "@hi-ui/use-data-source": "^4.0.0-beta.6",
61
62
  "@hi-ui/use-latest": "^4.0.0-beta.5",
62
- "@hi-ui/use-search-mode": "^4.0.0-beta.27",
63
- "@hi-ui/use-toggle": "^4.0.0-beta.5",
63
+ "@hi-ui/use-search-mode": "^4.0.0-beta.28",
64
+ "@hi-ui/use-toggle": "^4.0.0-beta.6",
64
65
  "@hi-ui/use-uncontrolled-state": "^4.0.0-beta.5"
65
66
  },
66
67
  "peerDependencies": {
@@ -72,5 +73,5 @@
72
73
  "react": "^17.0.1",
73
74
  "react-dom": "^17.0.1"
74
75
  },
75
- "gitHead": "74289b56d18d12073b6f2729f74ec3c457f6b269"
76
+ "gitHead": "ebb4ffb5c70d74377d7f0be703a4810a1cab0204"
76
77
  }