get-children.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import React from 'react';
  2. function processChildren(c) {
  3. const slides = [];
  4. React.Children.toArray(c).forEach(child => {
  5. if (child.type && child.type.displayName === 'SwiperSlide') {
  6. slides.push(child);
  7. } else if (child.props && child.props.children) {
  8. processChildren(child.props.children).forEach(slide => slides.push(slide));
  9. }
  10. });
  11. return slides;
  12. }
  13. function getChildren(c) {
  14. const slides = [];
  15. const slots = {
  16. 'container-start': [],
  17. 'container-end': [],
  18. 'wrapper-start': [],
  19. 'wrapper-end': []
  20. };
  21. React.Children.toArray(c).forEach(child => {
  22. if (child.type && child.type.displayName === 'SwiperSlide') {
  23. slides.push(child);
  24. } else if (child.props && child.props.slot && slots[child.props.slot]) {
  25. slots[child.props.slot].push(child);
  26. } else if (child.props && child.props.children) {
  27. const foundSlides = processChildren(child.props.children);
  28. if (foundSlides.length > 0) {
  29. foundSlides.forEach(slide => slides.push(slide));
  30. } else {
  31. slots['container-end'].push(child);
  32. }
  33. } else {
  34. slots['container-end'].push(child);
  35. }
  36. });
  37. return {
  38. slides,
  39. slots
  40. };
  41. }
  42. export { getChildren };