Skip to content

Commit 8a0c8a8

Browse files
authored
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
1 parent 7ea18a8 commit 8a0c8a8

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

components/_util/vnode.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import type { VNode, VNodeProps } from 'vue';
33
import { cloneVNode } from 'vue';
44
import warning from './warning';
55
import type { RefObject } from './createRef';
6+
type NodeProps = Record<string, any> &
7+
Omit<VNodeProps, 'ref'> & { ref?: VNodeProps['ref'] | RefObject };
68

79
export function cloneElement<T, U>(
810
vnode: VNode<T, U> | VNode<T, U>[],
9-
nodeProps: Record<string, any> &
10-
Omit<VNodeProps, 'ref'> & { ref?: VNodeProps['ref'] | RefObject } = {},
11+
nodeProps: NodeProps = {},
1112
override = true,
1213
mergeRef = false,
1314
): VNode<T, U> {
@@ -29,3 +30,20 @@ export function cloneElement<T, U>(
2930
export function cloneVNodes(vnodes, nodeProps = {}, override = true) {
3031
return vnodes.map(vnode => cloneElement(vnode, nodeProps, override));
3132
}
33+
34+
export function deepCloneElement<T, U>(
35+
vnode: VNode<T, U> | VNode<T, U>[],
36+
nodeProps: NodeProps = {},
37+
override = true,
38+
mergeRef = false,
39+
) {
40+
if (Array.isArray(vnode)) {
41+
return vnode.map(item => deepCloneElement(item, nodeProps, override, mergeRef));
42+
} else {
43+
const cloned = cloneElement(vnode, nodeProps, override, mergeRef);
44+
if (Array.isArray(cloned.children)) {
45+
cloned.children = deepCloneElement(cloned.children as VNode<T, U>[]);
46+
}
47+
return cloned;
48+
}
49+
}

components/vc-slick/track.jsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { createVNode } from 'vue';
22
import classnames from '../_util/classNames';
3-
import { cloneElement } from '../_util/vnode';
43
import { flattenChildren } from '../_util/props-util';
54
import { lazyStartIndex, lazyEndIndex, getPreClones } from './utils/innerSliderUtils';
5+
import { deepCloneElement } from '../_util/vnode';
66

77
// given specifications/props for a slide, fetch all the classes that need to be applied to the slide
88
const getSlideClasses = spec => {
@@ -84,7 +84,6 @@ const renderSlides = function (spec, children) {
8484
const childrenCount = children.length;
8585
const startIndex = lazyStartIndex(spec);
8686
const endIndex = lazyEndIndex(spec);
87-
8887
children.forEach((elem, index) => {
8988
let child;
9089
const childOnClickOptions = {
@@ -105,7 +104,7 @@ const renderSlides = function (spec, children) {
105104
let slideClasses = getSlideClasses({ ...spec, index });
106105
// push a cloned element of the desired slide
107106
slides.push(
108-
cloneElement(child, {
107+
deepCloneElement(child, {
109108
key: 'original' + getKey(child, index),
110109
tabindex: '-1',
111110
'data-index': index,
@@ -131,7 +130,7 @@ const renderSlides = function (spec, children) {
131130
}
132131
slideClasses = getSlideClasses({ ...spec, index: key });
133132
preCloneSlides.push(
134-
cloneElement(child, {
133+
deepCloneElement(child, {
135134
key: 'precloned' + getKey(child, key),
136135
class: classnames(slideClasses, slideClass),
137136
tabindex: '-1',
@@ -155,7 +154,7 @@ const renderSlides = function (spec, children) {
155154
}
156155
slideClasses = getSlideClasses({ ...spec, index: key });
157156
postCloneSlides.push(
158-
cloneElement(child, {
157+
deepCloneElement(child, {
159158
key: 'postcloned' + getKey(child, key),
160159
tabindex: '-1',
161160
'data-index': key,
@@ -182,6 +181,7 @@ const renderSlides = function (spec, children) {
182181

183182
const Track = (_, { attrs, slots }) => {
184183
const slides = renderSlides(attrs, flattenChildren(slots?.default()));
184+
// const slides = renderSlides(attrs, slots?.default);
185185
const { onMouseenter, onMouseover, onMouseleave } = attrs;
186186
const mouseEvents = { onMouseenter, onMouseover, onMouseleave };
187187
const trackProps = {

0 commit comments

Comments
 (0)