useSearchOptions.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  2. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  3. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  4. import * as React from 'react';
  5. export var SEARCH_MARK = '__rc_cascader_search_mark__';
  6. var defaultFilter = function defaultFilter(search, options, _ref) {
  7. var _ref$label = _ref.label,
  8. label = _ref$label === void 0 ? '' : _ref$label;
  9. return options.some(function (opt) {
  10. return String(opt[label]).toLowerCase().includes(search.toLowerCase());
  11. });
  12. };
  13. var defaultRender = function defaultRender(inputValue, path, prefixCls, fieldNames) {
  14. return path.map(function (opt) {
  15. return opt[fieldNames.label];
  16. }).join(' / ');
  17. };
  18. var useSearchOptions = function useSearchOptions(search, options, fieldNames, prefixCls, config, enableHalfPath) {
  19. var _config$filter = config.filter,
  20. filter = _config$filter === void 0 ? defaultFilter : _config$filter,
  21. _config$render = config.render,
  22. render = _config$render === void 0 ? defaultRender : _config$render,
  23. _config$limit = config.limit,
  24. limit = _config$limit === void 0 ? 50 : _config$limit,
  25. sort = config.sort;
  26. return React.useMemo(function () {
  27. var filteredOptions = [];
  28. if (!search) {
  29. return [];
  30. }
  31. function dig(list, pathOptions) {
  32. var parentDisabled = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  33. list.forEach(function (option) {
  34. // Perf saving when `sort` is disabled and `limit` is provided
  35. if (!sort && limit !== false && limit > 0 && filteredOptions.length >= limit) {
  36. return;
  37. }
  38. var connectedPathOptions = [].concat(_toConsumableArray(pathOptions), [option]);
  39. var children = option[fieldNames.children];
  40. var mergedDisabled = parentDisabled || option.disabled;
  41. // If current option is filterable
  42. if (
  43. // If is leaf option
  44. !children || children.length === 0 ||
  45. // If is changeOnSelect or multiple
  46. enableHalfPath) {
  47. if (filter(search, connectedPathOptions, {
  48. label: fieldNames.label
  49. })) {
  50. var _objectSpread2;
  51. filteredOptions.push(_objectSpread(_objectSpread({}, option), {}, (_objectSpread2 = {
  52. disabled: mergedDisabled
  53. }, _defineProperty(_objectSpread2, fieldNames.label, render(search, connectedPathOptions, prefixCls, fieldNames)), _defineProperty(_objectSpread2, SEARCH_MARK, connectedPathOptions), _defineProperty(_objectSpread2, fieldNames.children, undefined), _objectSpread2)));
  54. }
  55. }
  56. if (children) {
  57. dig(option[fieldNames.children], connectedPathOptions, mergedDisabled);
  58. }
  59. });
  60. }
  61. dig(options, []);
  62. // Do sort
  63. if (sort) {
  64. filteredOptions.sort(function (a, b) {
  65. return sort(a[SEARCH_MARK], b[SEARCH_MARK], search, fieldNames);
  66. });
  67. }
  68. return limit !== false && limit > 0 ? filteredOptions.slice(0, limit) : filteredOptions;
  69. }, [search, options, fieldNames, prefixCls, render, enableHalfPath, filter, sort, limit]);
  70. };
  71. export default useSearchOptions;