Skip to content

Commit

Permalink
feat(react): Allow SwiperSlide children as long as displayName includ…
Browse files Browse the repository at this point in the history
…es SwiperSlide (#5954)

Refactor getChildren to return all child components whose names contains SwiperSlide to allow for custom components in Swiper that do not have a displayName of SwiperSlide.
Adds helper function isChildSwiperSlide(child) to check if react child is a child which contains SwiperSlide in its displayName.
  • Loading branch information
crtl authored Aug 12, 2022
1 parent f2bbae6 commit d1f7582
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/react/get-children.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import React from 'react';

function isChildSwiperSlide(child) {
return child.type && child.type.displayName.includes('SwiperSlide');
}

function processChildren(c) {
const slides = [];
React.Children.toArray(c).forEach((child) => {
if (child.type && child.type.displayName === 'SwiperSlide') {
if (isChildSwiperSlide(child)) {
slides.push(child);
} else if (child.props && child.props.children) {
processChildren(child.props.children).forEach((slide) => slides.push(slide));
Expand All @@ -23,7 +27,7 @@ function getChildren(c) {
};

React.Children.toArray(c).forEach((child) => {
if (child.type && child.type.displayName === 'SwiperSlide') {
if (isChildSwiperSlide(child)) {
slides.push(child);
} else if (child.props && child.props.slot && slots[child.props.slot]) {
slots[child.props.slot].push(child);
Expand Down

0 comments on commit d1f7582

Please sign in to comment.