You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.5 KiB
Vue

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<template>
<view :class="`wd-tab ${customClass}`">
<view v-if="painted" class="wd-tab__body" :style="isShow ? '' : 'display: none;'">
<slot />
</view>
</view>
</template>
<script lang="ts">
export default {
name: 'wd-tab',
options: {
addGlobalClass: true,
virtualHost: true,
styleIsolation: 'shared'
}
}
</script>
<script lang="ts" setup>
import { getCurrentInstance, ref, watch } from 'vue'
import { getType, isDef } from '../common/util'
import { useParent } from '../composables/useParent'
import { TABS_KEY } from '../wd-tabs/types'
import { computed } from 'vue'
interface Props {
customClass?: string
// 唯一标识符
name?: string | number
// tab的label
title?: string
// tab禁用无法点击
disabled?: boolean
}
const props = withDefaults(defineProps<Props>(), {
customClass: '',
disabled: false
})
const painted = ref<boolean>(false) // 初始状态tab不会渲染必须通过tabs来设置painted使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
})
watch(
() => props.name,
(newValue) => {
if (newValue && getType(newValue) !== 'number' && getType(newValue) !== 'string') {
console.error('[wot design] error(wd-tab): the type of name should be number or string')
return
}
if (tabs) {
checkName(proxy)
}
},
{
deep: true,
immediate: true
}
)
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 自身
*/
function checkName(self) {
const { name: myName } = props
if (myName === undefined || myName === null || myName === '') {
return
}
tabs &&
tabs.children.forEach((child: any) => {
if (child.$.uid !== self.$.uid && child.name === myName) {
console.error(`The tab's bound value: ${myName} has been used`)
}
})
}
/**
* 设置子组件展示
* @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';
</style>