Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ElementChildren.js 917B

1234567891011121314151617181920212223242526272829303132
  1. import React from 'react';
  2. /**
  3. * Iterates through children that are typically specified as `props.children`,
  4. * but only maps over children that are "valid elements".
  5. *
  6. * The mapFunction provided index will be normalised to the components mapped,
  7. * so an invalid component would not increase the index.
  8. *
  9. */
  10. function map(children, func) {
  11. var index = 0;
  12. return React.Children.map(children, function (child) {
  13. return React.isValidElement(child) ? func(child, index++) : child;
  14. });
  15. }
  16. /**
  17. * Iterates through children that are "valid elements".
  18. *
  19. * The provided forEachFunc(child, index) will be called for each
  20. * leaf child with the index reflecting the position relative to "valid components".
  21. */
  22. function forEach(children, func) {
  23. var index = 0;
  24. React.Children.forEach(children, function (child) {
  25. if (React.isValidElement(child)) func(child, index++);
  26. });
  27. }
  28. export { map, forEach };