legacyUtil.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.convertChildrenToData = convertChildrenToData;
  7. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  8. var _propsUtil = require("../../_util/props-util");
  9. var __rest = void 0 && (void 0).__rest || function (s, e) {
  10. var t = {};
  11. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  12. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  13. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  14. }
  15. return t;
  16. };
  17. function convertNodeToOption(node) {
  18. const _a = node,
  19. {
  20. key,
  21. children
  22. } = _a,
  23. _b = _a.props,
  24. {
  25. value,
  26. disabled
  27. } = _b,
  28. restProps = __rest(_b, ["value", "disabled"]);
  29. const child = children === null || children === void 0 ? void 0 : children.default;
  30. return (0, _extends2.default)({
  31. key,
  32. value: value !== undefined ? value : key,
  33. children: child,
  34. disabled: disabled || disabled === ''
  35. }, restProps);
  36. }
  37. function convertChildrenToData(nodes) {
  38. let optionOnly = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  39. const dd = (0, _propsUtil.flattenChildren)(nodes).map((node, index) => {
  40. var _a;
  41. if (!(0, _propsUtil.isValidElement)(node) || !node.type) {
  42. return null;
  43. }
  44. const {
  45. type: {
  46. isSelectOptGroup
  47. },
  48. key,
  49. children,
  50. props
  51. } = node;
  52. if (optionOnly || !isSelectOptGroup) {
  53. return convertNodeToOption(node);
  54. }
  55. const child = children && children.default ? children.default() : undefined;
  56. const label = (props === null || props === void 0 ? void 0 : props.label) || ((_a = children.label) === null || _a === void 0 ? void 0 : _a.call(children)) || key;
  57. return (0, _extends2.default)((0, _extends2.default)({
  58. key: `__RC_SELECT_GRP__${key === null ? index : String(key)}__`
  59. }, props), {
  60. label,
  61. options: convertChildrenToData(child || [])
  62. });
  63. }).filter(data => data);
  64. return dd;
  65. }