get-children.js 1.3 KB

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