toArray.js 615 B

12345678910111213141516171819
  1. import isFragment from "../React/isFragment";
  2. import React from 'react';
  3. export default function toArray(children) {
  4. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  5. var ret = [];
  6. React.Children.forEach(children, function (child) {
  7. if ((child === undefined || child === null) && !option.keepEmpty) {
  8. return;
  9. }
  10. if (Array.isArray(child)) {
  11. ret = ret.concat(toArray(child));
  12. } else if (isFragment(child) && child.props) {
  13. ret = ret.concat(toArray(child.props.children, option));
  14. } else {
  15. ret.push(child);
  16. }
  17. });
  18. return ret;
  19. }