commonUtil.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.VALUE_SPLIT = exports.SHOW_PARENT = exports.SHOW_CHILD = void 0;
  6. exports.fillFieldNames = fillFieldNames;
  7. exports.getFullPathKeys = getFullPathKeys;
  8. exports.isLeaf = isLeaf;
  9. exports.scrollIntoParentView = scrollIntoParentView;
  10. exports.toPathKey = toPathKey;
  11. exports.toPathKeys = toPathKeys;
  12. exports.toPathValueStr = toPathValueStr;
  13. exports.toRawValues = toRawValues;
  14. var _useSearchOptions = require("../hooks/useSearchOptions");
  15. var VALUE_SPLIT = exports.VALUE_SPLIT = '__RC_CASCADER_SPLIT__';
  16. var SHOW_PARENT = exports.SHOW_PARENT = 'SHOW_PARENT';
  17. var SHOW_CHILD = exports.SHOW_CHILD = 'SHOW_CHILD';
  18. /**
  19. * Will convert value to string, and join with `VALUE_SPLIT`
  20. */
  21. function toPathKey(value) {
  22. return value.join(VALUE_SPLIT);
  23. }
  24. /**
  25. * Batch convert value to string, and join with `VALUE_SPLIT`
  26. */
  27. function toPathKeys(value) {
  28. return value.map(toPathKey);
  29. }
  30. function toPathValueStr(pathKey) {
  31. return pathKey.split(VALUE_SPLIT);
  32. }
  33. function fillFieldNames(fieldNames) {
  34. var _ref = fieldNames || {},
  35. label = _ref.label,
  36. value = _ref.value,
  37. children = _ref.children;
  38. var val = value || 'value';
  39. return {
  40. label: label || 'label',
  41. value: val,
  42. key: val,
  43. children: children || 'children'
  44. };
  45. }
  46. function isLeaf(option, fieldNames) {
  47. var _option$isLeaf, _option;
  48. return (_option$isLeaf = option.isLeaf) !== null && _option$isLeaf !== void 0 ? _option$isLeaf : !((_option = option[fieldNames.children]) !== null && _option !== void 0 && _option.length);
  49. }
  50. function scrollIntoParentView(element) {
  51. var parent = element.parentElement;
  52. if (!parent) {
  53. return;
  54. }
  55. var elementToParent = element.offsetTop - parent.offsetTop; // offsetParent may not be parent.
  56. if (elementToParent - parent.scrollTop < 0) {
  57. parent.scrollTo({
  58. top: elementToParent
  59. });
  60. } else if (elementToParent + element.offsetHeight - parent.scrollTop > parent.offsetHeight) {
  61. parent.scrollTo({
  62. top: elementToParent + element.offsetHeight - parent.offsetHeight
  63. });
  64. }
  65. }
  66. function getFullPathKeys(options, fieldNames) {
  67. return options.map(function (item) {
  68. var _item$SEARCH_MARK;
  69. return (_item$SEARCH_MARK = item[_useSearchOptions.SEARCH_MARK]) === null || _item$SEARCH_MARK === void 0 ? void 0 : _item$SEARCH_MARK.map(function (opt) {
  70. return opt[fieldNames.value];
  71. });
  72. });
  73. }
  74. function isMultipleValue(value) {
  75. return Array.isArray(value) && Array.isArray(value[0]);
  76. }
  77. function toRawValues(value) {
  78. if (!value) {
  79. return [];
  80. }
  81. if (isMultipleValue(value)) {
  82. return value;
  83. }
  84. return (value.length === 0 ? [] : [value]).map(function (val) {
  85. return Array.isArray(val) ? val : [val];
  86. });
  87. }