useSearchOptions.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = exports.SEARCH_MARK = void 0;
  7. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  8. var _vue = require("vue");
  9. const SEARCH_MARK = exports.SEARCH_MARK = '__rc_cascader_search_mark__';
  10. const defaultFilter = (search, options, _ref) => {
  11. let {
  12. label
  13. } = _ref;
  14. return options.some(opt => String(opt[label]).toLowerCase().includes(search.toLowerCase()));
  15. };
  16. const defaultRender = _ref2 => {
  17. let {
  18. path,
  19. fieldNames
  20. } = _ref2;
  21. return path.map(opt => opt[fieldNames.label]).join(' / ');
  22. };
  23. var _default = (search, options, fieldNames, prefixCls, config, changeOnSelect) => {
  24. return (0, _vue.computed)(() => {
  25. const {
  26. filter = defaultFilter,
  27. render = defaultRender,
  28. limit = 50,
  29. sort
  30. } = config.value;
  31. const filteredOptions = [];
  32. if (!search.value) {
  33. return [];
  34. }
  35. function dig(list, pathOptions) {
  36. list.forEach(option => {
  37. // Perf saving when `sort` is disabled and `limit` is provided
  38. if (!sort && limit > 0 && filteredOptions.length >= limit) {
  39. return;
  40. }
  41. const connectedPathOptions = [...pathOptions, option];
  42. const children = option[fieldNames.value.children];
  43. // If current option is filterable
  44. if (
  45. // If is leaf option
  46. !children || children.length === 0 ||
  47. // If is changeOnSelect
  48. changeOnSelect.value) {
  49. if (filter(search.value, connectedPathOptions, {
  50. label: fieldNames.value.label
  51. })) {
  52. filteredOptions.push((0, _extends2.default)((0, _extends2.default)({}, option), {
  53. [fieldNames.value.label]: render({
  54. inputValue: search.value,
  55. path: connectedPathOptions,
  56. prefixCls: prefixCls.value,
  57. fieldNames: fieldNames.value
  58. }),
  59. [SEARCH_MARK]: connectedPathOptions
  60. }));
  61. }
  62. }
  63. if (children) {
  64. dig(option[fieldNames.value.children], connectedPathOptions);
  65. }
  66. });
  67. }
  68. dig(options.value, []);
  69. // Do sort
  70. if (sort) {
  71. filteredOptions.sort((a, b) => {
  72. return sort(a[SEARCH_MARK], b[SEARCH_MARK], search.value, fieldNames.value);
  73. });
  74. }
  75. return limit > 0 ? filteredOptions.slice(0, limit) : filteredOptions;
  76. });
  77. };
  78. exports.default = _default;