From fdcd6444cb925c80e571595c788ba16ddea91560 Mon Sep 17 00:00:00 2001 From: Vladimir Kharlampidi Date: Wed, 14 Dec 2022 10:46:13 +0300 Subject: [PATCH] feat(virtual): `renderSlide` to support slide outer HTML and HTML element --- src/modules/virtual/virtual.js | 15 ++++++++++----- src/types/modules/virtual.d.ts | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/modules/virtual/virtual.js b/src/modules/virtual/virtual.js index c4f994e2f..8d685cb5d 100644 --- a/src/modules/virtual/virtual.js +++ b/src/modules/virtual/virtual.js @@ -25,6 +25,8 @@ export default function Virtual({ swiper, extendParams, on, emit }) { slidesGrid: [], }; + const tempDOM = document.createElement('div'); + function renderSlide(slide, index) { const params = swiper.params.virtual; if (params.cache && swiper.virtual.cache[index]) { @@ -32,19 +34,22 @@ export default function Virtual({ swiper, extendParams, on, emit }) { } // eslint-disable-next-line let slideEl; - if (params.renderSlide) slideEl = params.renderSlide.call(swiper, slide, index); - else if (swiper.isElement) { + if (params.renderSlide) { + slideEl = params.renderSlide.call(swiper, slide, index); + if (typeof slideEl === 'string') { + tempDOM.innerHTML = slideEl; + slideEl = tempDOM.children[0]; + } + } else if (swiper.isElement) { slideEl = createElement('swiper-slide'); } else { slideEl = createElement('div', swiper.params.slideClass); } + slideEl.setAttribute('data-swiper-slide-index', index); if (!params.renderSlide) { - slideEl.setAttribute('data-swiper-slide-index', index); slideEl.textContent = slide; } - if (!slideEl.getAttribute('data-swiper-slide-index')) - slideEl.setAttribute('data-swiper-slide-index', index); if (params.cache) swiper.virtual.cache[index] = slideEl; return slideEl; } diff --git a/src/types/modules/virtual.d.ts b/src/types/modules/virtual.d.ts index 5b1ef590b..5ffabe1c6 100644 --- a/src/types/modules/virtual.d.ts +++ b/src/types/modules/virtual.d.ts @@ -110,7 +110,7 @@ export interface VirtualOptions { */ addSlidesAfter?: number; /** - * Function to render slide. As an argument it accepts current slide item for `slides` array and index number of the current slide. Function must return an outter HTML of the swiper slide. + * Function to render slide. As an argument it accepts current slide item for `slides` array and index number of the current slide. Function must return an outer HTML of the swiper slide or slide HTML element. * * @default null */