useSearchOptions.js 2.9 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 { computed } from 'vue';
  5. export var SEARCH_MARK = '__rc_cascader_search_mark__';
  6. var defaultFilter = function defaultFilter(search, options, _ref) {
  7. var label = _ref.label;
  8. return options.some(function (opt) {
  9. return String(opt[label]).toLowerCase().includes(search.toLowerCase());
  10. });
  11. };
  12. var defaultRender = function defaultRender(_ref2) {
  13. var path = _ref2.path,
  14. fieldNames = _ref2.fieldNames;
  15. return path.map(function (opt) {
  16. return opt[fieldNames.label];
  17. }).join(' / ');
  18. };
  19. export default (function (search, options, fieldNames, prefixCls, config, changeOnSelect) {
  20. return computed(function () {
  21. var _config$value = config.value,
  22. _config$value$filter = _config$value.filter,
  23. filter = _config$value$filter === void 0 ? defaultFilter : _config$value$filter,
  24. _config$value$render = _config$value.render,
  25. render = _config$value$render === void 0 ? defaultRender : _config$value$render,
  26. _config$value$limit = _config$value.limit,
  27. limit = _config$value$limit === void 0 ? 50 : _config$value$limit,
  28. sort = _config$value.sort;
  29. var filteredOptions = [];
  30. if (!search.value) {
  31. return [];
  32. }
  33. function dig(list, pathOptions) {
  34. list.forEach(function (option) {
  35. // Perf saving when `sort` is disabled and `limit` is provided
  36. if (!sort && limit > 0 && filteredOptions.length >= limit) {
  37. return;
  38. }
  39. var connectedPathOptions = [].concat(_toConsumableArray(pathOptions), [option]);
  40. var children = option[fieldNames.value.children];
  41. // If current option is filterable
  42. if (
  43. // If is leaf option
  44. !children || children.length === 0 ||
  45. // If is changeOnSelect
  46. changeOnSelect.value) {
  47. if (filter(search.value, connectedPathOptions, {
  48. label: fieldNames.value.label
  49. })) {
  50. var _objectSpread2;
  51. filteredOptions.push(_objectSpread(_objectSpread({}, option), {}, (_objectSpread2 = {}, _defineProperty(_objectSpread2, fieldNames.value.label, render({
  52. inputValue: search.value,
  53. path: connectedPathOptions,
  54. prefixCls: prefixCls.value,
  55. fieldNames: fieldNames.value
  56. })), _defineProperty(_objectSpread2, SEARCH_MARK, connectedPathOptions), _objectSpread2)));
  57. }
  58. }
  59. if (children) {
  60. dig(option[fieldNames.value.children], connectedPathOptions);
  61. }
  62. });
  63. }
  64. dig(options.value, []);
  65. // Do sort
  66. if (sort) {
  67. filteredOptions.sort(function (a, b) {
  68. return sort(a[SEARCH_MARK], b[SEARCH_MARK], search.value, fieldNames.value);
  69. });
  70. }
  71. return limit > 0 ? filteredOptions.slice(0, limit) : filteredOptions;
  72. });
  73. });