fix: 修复 carousel-item 无法嵌套 v-for 渲染

This commit is contained in:
就眠儀式 2022-03-18 12:56:00 +08:00
parent 5701edd631
commit ae96c341f0
7 changed files with 364 additions and 290 deletions

View File

@ -118,6 +118,45 @@ export default {
:::
::: title 动态遍历
:::
::: demo
<template>
<lay-carousel v-model="active">
<lay-carousel-item :id="item.id" v-for="item in arrays">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#79C48C;">{{ item.text }}</div>
</lay-carousel-item>
</lay-carousel>
</template>
<script>
import { ref } from 'vue'
export default {
setup() {
const active = ref("1")
const arrays = ref([
{id: "1", text: ""},
{id: "2", text: ""},
{id: "3", text: ""},
{id: "4", text: ""}
])
return {
active,
arrays
}
}
}
</script>
:::
::: title Carousel 属性
:::

View File

@ -23,6 +23,7 @@
<li>[新增] page 分页组件 v-model 属性, 支持默认页设置。</li>
<li>[新增] date-picker 日期选择组件, 支持年月, 日期, 时间。</li>
<li>[新增] transfer 穿梭框组件 showSearch 开启搜索属性。</li>
<li>[修复] carousel-item 轮播项使用 v-for 无法渲染。</li>
<li>[修复] checkbox 复选框组件, 选中颜色丢失。</li>
<li>[修复] slider 滑块组件, 默认 step 值异常。</li>
<li>[升级] layer-vue 1.3.10 版本。</li>

View File

@ -1,6 +1,5 @@
@import "../../theme/variable.less";
.layui-carousel {
position: relative;
left: 0;
@ -275,4 +274,3 @@
> .layui-this.layui-carousel-right {
opacity: 0;
}

View File

@ -6,7 +6,8 @@ export default {
<script setup lang="ts">
import "./index.less";
import { withDefaults, provide, useSlots, ref, computed } from "vue";
import { withDefaults, provide, useSlots, ref, computed, VNode, Ref, Component, watch, onMounted } from "vue";
import CarouselItem from "../carouselItem"
const slot = useSlots() as any;
const slots = slot.default && (slot.default() as any[]);
@ -17,14 +18,18 @@ const props = withDefaults(
height?: string;
modelValue: string;
anim?: string;
autoplay?: boolean;
arrow?: string;
interval?: number;
indicator?: string;
}>(),
{
width: "100%",
height: "280px",
anim: "default",
autoplay: true,
arrow: "hover",
interval: 3000,
indicator: "inside",
}
);
@ -45,27 +50,55 @@ const change = function (id: any) {
active.value = id;
};
provide("active", active);
const childrens: Ref<VNode[]> = ref([]);
const slotsChange = ref(true);
const prev = function () {
for (var i = 0; i < slots.length; i++) {
if (slots[i].props.id === active.value) {
if (i === 0) {
active.value = slots[slots.length - 1].props.id;
const setItemInstanceBySlot = function (nodeList: VNode[]) {
nodeList?.map((item) => {
let component = item.type as Component;
if (component.name != CarouselItem.name) {
setItemInstanceBySlot(item.children as VNode[]);
} else {
childrens.value.push(item);
}
active.value = slots[i - 1].props.id;
});
};
watch(slotsChange, function () {
childrens.value = [];
setItemInstanceBySlot((slot.default && slot.default()) as VNode[]);
});
provide("active", active);
provide("slotsChange", slotsChange);
//
const prev = function () {
for (var i = 0; i < childrens.value.length; i++) {
// @ts-ignore
if (childrens.value[i].props.id === active.value) {
if (i === 0) {
// @ts-ignore
active.value = childrens.value[slots.length - 1].props.id;
}
// @ts-ignore
active.value = childrens.value[i - 1].props.id;
break;
}
}
};
//
const next = function () {
for (var i = 0; i < slots.length; i++) {
if (slots[i].props.id === active.value) {
if (i === slots.length - 1) {
active.value = slots[0].props.id;
for (var i = 0; i < childrens.value.length; i++) {
// @ts-ignore
if (childrens.value[i].props.id === active.value) {
if (i === childrens.value.length - 1) {
// @ts-ignore
active.value = childrens.value[0].props.id;
}
active.value = slots[i + 1].props.id;
// @ts-ignore
active.value = childrens.value[i + 1].props.id;
break;
}
}
@ -86,7 +119,7 @@ const next = function () {
<div class="layui-carousel-ind">
<ul>
<li
v-for="ss in slots"
v-for="ss in childrens"
:key="ss"
:class="[ss.props.id === active ? 'layui-this' : '']"
@click.stop="change(ss.props.id)"

View File

@ -5,13 +5,16 @@ export default {
</script>
<script setup lang="ts">
import { inject } from "vue";
import { inject, Ref } from "vue";
const props = defineProps<{
id: string;
}>();
const active = inject("active");
const slotsChange: Ref<boolean> = inject("slotsChange") as Ref<boolean>;
slotsChange.value = !slotsChange.value;
</script>
<template>

View File

@ -29,6 +29,7 @@ export interface LayTabProps {
const slot = useSlots();
const slots = slot.default && slot.default();
const childrens: Ref<VNode[]> = ref([]);
const slotsChange = ref(true);
const setItemInstanceBySlot = function (nodeList: VNode[]) {
nodeList?.map((item) => {
@ -53,7 +54,6 @@ const active = computed({
emit("update:modelValue", val);
},
});
const slotsChange = ref(true);
const change = function (id: any) {
// return false, tab