commonUtil.js 2.2 KB

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