feat: Tab 添加 lazy 属性支持配置是否开启懒加载

 Closes: #641
This commit is contained in:
不如摸鱼去 2024-11-19 22:26:19 +08:00
parent fad777dfa8
commit bb5b19325f
3 changed files with 21 additions and 36 deletions

View File

@ -190,8 +190,9 @@ const tab = ref('Design')
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
| -------- | ---------- | ------- | ------ | ------ | -------- |
| name | 标签页名称 | string | - | - | - |
| title | 标题 | string | - | - | - |
| disabled | 禁用 | boolean | - | false | - |
| title | 标题 | string | - | - | - |
| disabled | 禁用 | boolean | - | false | - |
| lazy | 延迟渲染,默认开启,开启`animated`后此选项始终为`false` | boolean | - | true | $LOWEST_VERSION$ |
## Tabs Events

View File

@ -14,7 +14,12 @@ export const tabProps = {
/**
*
*/
disabled: makeBooleanProp(false)
disabled: makeBooleanProp(false),
/**
* tab时才加载内容
* @default true
*/
lazy: makeBooleanProp(true)
}
export type TabProps = ExtractPropTypes<typeof tabProps>

View File

@ -1,6 +1,6 @@
<template>
<view :class="`wd-tab ${customClass}`" :style="customStyle">
<view v-if="painted" class="wd-tab__body" :style="tabBodyStyle">
<view class="wd-tab__body" v-if="shouldBeRender" :style="tabBodyStyle">
<slot />
</view>
</view>
@ -25,24 +25,30 @@ import { tabProps } from './types'
const props = defineProps(tabProps)
const painted = ref<boolean>(false) // tabtabspainted使tab
const isShow = ref<boolean>(false)
const { proxy } = getCurrentInstance() as any
const { parent: tabs, index } = useParent(TABS_KEY)
//
const activeIndex = computed(() => {
return isDef(tabs) ? tabs.state.activeIndex : 0
const active = computed(() => {
return isDef(tabs) ? tabs.state.activeIndex === index.value : false
})
const painted = ref<boolean>(active.value) // tabtabspainted使tab
const tabBodyStyle = computed(() => {
const style: CSSProperties = {}
if (!isShow.value && (!isDef(tabs) || !tabs.props.animated)) {
if (!active.value && (!isDef(tabs) || !tabs.props.animated)) {
style.display = 'none'
}
return objToStyle(style)
})
const shouldBeRender = computed(() => !props.lazy || painted.value || active.value)
watch(active, (val) => {
if (val) painted.value = true
})
watch(
() => props.name,
(newValue) => {
@ -60,18 +66,6 @@ watch(
}
)
watch(
() => activeIndex.value,
(newValue) => {
if (newValue === index.value) {
setShow(true, true)
} else {
setShow(painted.value, false)
}
},
{ deep: true, immediate: true }
)
/**
* @description 检测tab绑定的name是否和其它tab的name冲突
* @param {Object} self 自身
@ -88,21 +82,6 @@ function checkName(self: any) {
}
})
}
/**
* 设置子组件展示
* @param setPainted
* @param setIsShow
*/
function setShow(setPainted: boolean, setIsShow: boolean) {
painted.value = setPainted
isShow.value = setIsShow
}
defineExpose({
setShow,
painted
})
</script>
<style lang="scss" scoped>
@import './index.scss';