perf(collapse): 删除 collapseItem 的 show 配置, 使用 collapse openKeys 替换

This commit is contained in:
就眠仪式 2021-10-12 12:58:17 +08:00
parent 55da634f7c
commit 16934254ab
3 changed files with 37 additions and 9 deletions

View File

@ -1,10 +1,11 @@
::: demo ::: demo
<template> <template>
<lay-collapse> {{openKeys}}
<lay-collapse-item title="标题" show> 内容 </lay-collapse-item> <lay-collapse :openKeys="openKeys">
<lay-collapse-item title="标题" show> 内容 </lay-collapse-item> <lay-collapse-item title="标题" id="1"> 内容 </lay-collapse-item>
<lay-collapse-item title="标题" show> 内容 </lay-collapse-item> <lay-collapse-item title="标题" id="2"> 内容 </lay-collapse-item>
<lay-collapse-item title="标题" id="3"> 内容 </lay-collapse-item>
</lay-collapse> </lay-collapse>
</template> </template>
@ -14,7 +15,10 @@ import { ref } from 'vue'
export default { export default {
setup() { setup() {
const openKeys = ref(["1","2"])
return { return {
openKeys
} }
} }
} }

View File

@ -4,4 +4,21 @@
</div> </div>
</template> </template>
<script setup name="LayCollapse" lang="ts"></script> <script setup name="LayCollapse"></script>
<script setup lang="ts">
import { withDefaults, defineProps, provide } from 'vue'
const props = withDefaults(
defineProps<{
openKeys: string[]
}>(),
{
openKeys: function () {
return []
},
}
)
provide("openKeys",props.openKeys)
</script>

View File

@ -13,16 +13,23 @@
</template> </template>
<script setup name="LayCollapseItem" lang="ts"> <script setup name="LayCollapseItem" lang="ts">
import { defineProps, Ref, ref } from 'vue' import { defineProps, inject, Ref, ref } from 'vue'
const props = defineProps<{ const props = defineProps<{
show?: boolean id: string
title?: string title: string
}>() }>()
const isShow = ref(props.show) as Ref const openKeys = inject("openKeys") as String[]
const isShow = ref(openKeys.includes(props.id))
const showHandle = function () { const showHandle = function () {
isShow.value = !isShow.value isShow.value = !isShow.value
if(openKeys.indexOf(props.id) != -1) {
openKeys.splice(openKeys.indexOf(props.id),1)
} else {
openKeys.push(props.id)
}
} }
</script> </script>