@hi-ui/cascader 4.0.0-beta.41 → 4.0.0-beta.44

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;
@@ -154,9 +159,12 @@ var Cascader = /*#__PURE__*/React.forwardRef(function (props, ref) {
154
159
  data: cascaderData,
155
160
  flattedData: flattedData,
156
161
  enabled: searchableProp,
157
- exclude: function exclude(option) {
158
- return index$1.checkCanLoadChildren(option, onLoadChildren);
159
- }
162
+ exclude: function exclude(node) {
163
+ return node.disabled;
164
+ } // exclude: (option: FlattedCascaderDataItem) => {
165
+ // return checkCanLoadChildren(option, onLoadChildren)
166
+ // },
167
+
160
168
  });
161
169
 
162
170
  var _useSearchMode = useSearchMode.useSearchMode({
@@ -194,11 +202,14 @@ var Cascader = /*#__PURE__*/React.forwardRef(function (props, ref) {
194
202
  }, mergedTitle);
195
203
  };
196
204
 
205
+ var shouldUseSearch = !!searchValue; // 搜索的结果列表也采用 flatted 模式进行展示
206
+
207
+ var flatted = shouldUseSearch ? flattedSearchResult : type === 'flatted';
208
+
197
209
  var _a = useCascader.useCascader(Object.assign(Object.assign({}, rest), {
198
210
  disabled: disabled,
199
211
  fieldNames: fieldNames,
200
- // 搜索的结果列表也采用 flatted 模式进行展示
201
- flatted: flatted || !!searchValue,
212
+ flatted: flatted,
202
213
  onSelect: onSelect,
203
214
  onLoadChildren: onLoadChildren,
204
215
  data: data,
@@ -215,14 +226,17 @@ var Cascader = /*#__PURE__*/React.forwardRef(function (props, ref) {
215
226
  reset = context$1.reset,
216
227
  menuList = context$1.menuList,
217
228
  getItemRequiredProps = context$1.getItemRequiredProps;
218
- var shouldUseSearch = !!searchValue;
219
229
  var showData = React.useMemo(function () {
220
230
  if (shouldUseSearch) {
231
+ if (!flattedSearchResult) {
232
+ return index$1.getFilteredMenuList(menuList, stateInSearch.data);
233
+ }
234
+
221
235
  return typeAssertion.isArrayNonEmpty(stateInSearch.data) ? [stateInSearch.data] : [];
222
236
  }
223
237
 
224
238
  return menuList;
225
- }, [shouldUseSearch, stateInSearch.data, menuList]);
239
+ }, [shouldUseSearch, flattedSearchResult, stateInSearch.data, menuList]);
226
240
  React.useEffect(function () {
227
241
  // 关闭展示后,重置展开要高亮的选项
228
242
  if (!menuVisible) {
@@ -285,6 +299,16 @@ var renderHighlightTitle = function renderHighlightTitle(keyword, option, titleR
285
299
  // 如果 titleRender 返回 `true`,则使用默认 title
286
300
  var title = titleRender ? titleRender(option, keyword) : true;
287
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;
288
312
 
289
313
  if (typeof option.title !== 'string') {
290
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, checkCanLoadChildren, 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;
@@ -120,9 +122,12 @@ var Cascader = /*#__PURE__*/forwardRef(function (props, ref) {
120
122
  data: cascaderData,
121
123
  flattedData: flattedData,
122
124
  enabled: searchableProp,
123
- exclude: function exclude(option) {
124
- return checkCanLoadChildren(option, onLoadChildren);
125
- }
125
+ exclude: function exclude(node) {
126
+ return node.disabled;
127
+ } // exclude: (option: FlattedCascaderDataItem) => {
128
+ // return checkCanLoadChildren(option, onLoadChildren)
129
+ // },
130
+
126
131
  });
127
132
 
128
133
  var _useSearchMode = useSearchMode({
@@ -160,11 +165,14 @@ var Cascader = /*#__PURE__*/forwardRef(function (props, ref) {
160
165
  }, mergedTitle);
161
166
  };
162
167
 
168
+ var shouldUseSearch = !!searchValue; // 搜索的结果列表也采用 flatted 模式进行展示
169
+
170
+ var flatted = shouldUseSearch ? flattedSearchResult : type === 'flatted';
171
+
163
172
  var _a = useCascader(Object.assign(Object.assign({}, rest), {
164
173
  disabled: disabled,
165
174
  fieldNames: fieldNames,
166
- // 搜索的结果列表也采用 flatted 模式进行展示
167
- flatted: flatted || !!searchValue,
175
+ flatted: flatted,
168
176
  onSelect: onSelect,
169
177
  onLoadChildren: onLoadChildren,
170
178
  data: data,
@@ -181,14 +189,17 @@ var Cascader = /*#__PURE__*/forwardRef(function (props, ref) {
181
189
  reset = context.reset,
182
190
  menuList = context.menuList,
183
191
  getItemRequiredProps = context.getItemRequiredProps;
184
- var shouldUseSearch = !!searchValue;
185
192
  var showData = useMemo(function () {
186
193
  if (shouldUseSearch) {
194
+ if (!flattedSearchResult) {
195
+ return getFilteredMenuList(menuList, stateInSearch.data);
196
+ }
197
+
187
198
  return isArrayNonEmpty(stateInSearch.data) ? [stateInSearch.data] : [];
188
199
  }
189
200
 
190
201
  return menuList;
191
- }, [shouldUseSearch, stateInSearch.data, menuList]);
202
+ }, [shouldUseSearch, flattedSearchResult, stateInSearch.data, menuList]);
192
203
  useEffect(function () {
193
204
  // 关闭展示后,重置展开要高亮的选项
194
205
  if (!menuVisible) {
@@ -251,6 +262,16 @@ var renderHighlightTitle = function renderHighlightTitle(keyword, option, titleR
251
262
  // 如果 titleRender 返回 `true`,则使用默认 title
252
263
  var title = titleRender ? titleRender(option, keyword) : true;
253
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;
254
275
 
255
276
  if (typeof option.title !== 'string') {
256
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.41",
3
+ "version": "4.0.0-beta.44",
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.22",
54
+ "@hi-ui/locale-context": "^4.0.0-beta.20",
55
+ "@hi-ui/picker": "^4.0.0-beta.37",
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": "a5d4d719cac7f0eaf9293668ad436cccb67fa570"
76
+ "gitHead": "43cfc64831263176a31bf951a5add214df340248"
76
77
  }