From 8a0c8a8fb12d152a4761a7f4ecf32bb8dec6dbbe Mon Sep 17 00:00:00 2001 From: "M.Wang" Date: Thu, 15 Dec 2022 10:28:33 +0800 Subject: [PATCH] fix(vc-slick): #6100 - Fix content responsiveness in carousel component slot. (#6149) * fix(vc-slick): #6100 - Fix content responsiveness in carousel component slot. * feat(_util) add deepCloneElement function --- components/_util/vnode.ts | 22 ++++++++++++++++++++-- components/vc-slick/track.jsx | 10 +++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/components/_util/vnode.ts b/components/_util/vnode.ts index c770abc11..505bb638d 100644 --- a/components/_util/vnode.ts +++ b/components/_util/vnode.ts @@ -3,11 +3,12 @@ import type { VNode, VNodeProps } from 'vue'; import { cloneVNode } from 'vue'; import warning from './warning'; import type { RefObject } from './createRef'; +type NodeProps = Record & + Omit & { ref?: VNodeProps['ref'] | RefObject }; export function cloneElement( vnode: VNode | VNode[], - nodeProps: Record & - Omit & { ref?: VNodeProps['ref'] | RefObject } = {}, + nodeProps: NodeProps = {}, override = true, mergeRef = false, ): VNode { @@ -29,3 +30,20 @@ export function cloneElement( export function cloneVNodes(vnodes, nodeProps = {}, override = true) { return vnodes.map(vnode => cloneElement(vnode, nodeProps, override)); } + +export function deepCloneElement( + vnode: VNode | VNode[], + nodeProps: NodeProps = {}, + override = true, + mergeRef = false, +) { + if (Array.isArray(vnode)) { + return vnode.map(item => deepCloneElement(item, nodeProps, override, mergeRef)); + } else { + const cloned = cloneElement(vnode, nodeProps, override, mergeRef); + if (Array.isArray(cloned.children)) { + cloned.children = deepCloneElement(cloned.children as VNode[]); + } + return cloned; + } +} diff --git a/components/vc-slick/track.jsx b/components/vc-slick/track.jsx index 5616172d9..a8665e401 100644 --- a/components/vc-slick/track.jsx +++ b/components/vc-slick/track.jsx @@ -1,8 +1,8 @@ import { createVNode } from 'vue'; import classnames from '../_util/classNames'; -import { cloneElement } from '../_util/vnode'; import { flattenChildren } from '../_util/props-util'; import { lazyStartIndex, lazyEndIndex, getPreClones } from './utils/innerSliderUtils'; +import { deepCloneElement } from '../_util/vnode'; // given specifications/props for a slide, fetch all the classes that need to be applied to the slide const getSlideClasses = spec => { @@ -84,7 +84,6 @@ const renderSlides = function (spec, children) { const childrenCount = children.length; const startIndex = lazyStartIndex(spec); const endIndex = lazyEndIndex(spec); - children.forEach((elem, index) => { let child; const childOnClickOptions = { @@ -105,7 +104,7 @@ const renderSlides = function (spec, children) { let slideClasses = getSlideClasses({ ...spec, index }); // push a cloned element of the desired slide slides.push( - cloneElement(child, { + deepCloneElement(child, { key: 'original' + getKey(child, index), tabindex: '-1', 'data-index': index, @@ -131,7 +130,7 @@ const renderSlides = function (spec, children) { } slideClasses = getSlideClasses({ ...spec, index: key }); preCloneSlides.push( - cloneElement(child, { + deepCloneElement(child, { key: 'precloned' + getKey(child, key), class: classnames(slideClasses, slideClass), tabindex: '-1', @@ -155,7 +154,7 @@ const renderSlides = function (spec, children) { } slideClasses = getSlideClasses({ ...spec, index: key }); postCloneSlides.push( - cloneElement(child, { + deepCloneElement(child, { key: 'postcloned' + getKey(child, key), tabindex: '-1', 'data-index': key, @@ -182,6 +181,7 @@ const renderSlides = function (spec, children) { const Track = (_, { attrs, slots }) => { const slides = renderSlides(attrs, flattenChildren(slots?.default())); + // const slides = renderSlides(attrs, slots?.default); const { onMouseenter, onMouseover, onMouseleave } = attrs; const mouseEvents = { onMouseenter, onMouseover, onMouseleave }; const trackProps = {