ant-design-vue/components/tabs/TabBarMixin.js

97 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-11-22 07:05:53 +00:00
export default {
2017-11-30 11:11:42 +00:00
props: {
prefixCls: {
default: 'ant-tabs',
type: String,
},
tabBarPosition: {
default: 'top',
type: String,
},
disabled: Boolean,
onKeyDown: {
default: () => {},
type: Function,
},
onTabClick: {
default: () => {},
type: Function,
},
activeKey: String,
panels: Array,
},
2017-11-22 07:05:53 +00:00
methods: {
2017-12-01 04:21:43 +00:00
getTabs (h) {
2017-11-22 07:05:53 +00:00
const { panels: children, activeKey, prefixCls } = this
const rst = []
children.forEach((child) => {
if (!child) {
return
}
2017-12-01 04:21:43 +00:00
// componentOptions.propsData中获取的值disabled没有根据类型初始化, 会出现空字符串
child.disabled = child.disabled === '' || child.disabled
const key = child.tabKey
2017-11-22 07:05:53 +00:00
let cls = activeKey === key ? `${prefixCls}-tab-active` : ''
cls += ` ${prefixCls}-tab`
if (child.disabled) {
cls += ` ${prefixCls}-tab-disabled`
} else {
}
const onClick = () => {
!child.disabled && this.onTabClick(key)
}
2017-11-22 07:45:43 +00:00
// const ref = {}
// if (activeKey === key) {
// ref.ref = this.saveRef('activeTab')
// }
2017-11-22 07:05:53 +00:00
rst.push(
<div
role='tab'
aria-disabled={child.disabled ? 'true' : 'false'}
aria-selected={activeKey === key ? 'true' : 'false'}
class={cls}
key={key}
onClick={onClick}
2017-11-22 07:45:43 +00:00
ref={activeKey === key ? 'activeTab' : undefined}
2017-11-22 07:05:53 +00:00
>
2017-12-01 04:21:43 +00:00
{typeof child.tab === 'function' ? child.tab(h, key) : child.tab}
2017-11-22 07:05:53 +00:00
</div>
)
})
return rst
},
getRootNode (contents, createElement) {
const {
prefixCls, onKeyDown, tabBarPosition, $slots,
} = this
const cls = {
[`${prefixCls}-bar`]: true,
}
const topOrBottom = (tabBarPosition === 'top' || tabBarPosition === 'bottom')
const tabBarExtraContentStyle = topOrBottom ? { float: 'right' } : {}
let children = contents
if ($slots.default) {
children = [
2017-12-01 04:21:43 +00:00
<div key='extra' class={`${prefixCls}-extra-content`} style={tabBarExtraContentStyle}>
2017-11-22 07:05:53 +00:00
{$slots.default}
</div>,
contents,
]
children = topOrBottom ? children : children.reverse()
}
return (
<div
role='tablist'
class={cls}
tabIndex='0'
2017-11-22 07:45:43 +00:00
ref='root'
2017-11-22 07:05:53 +00:00
onKeydown={onKeyDown}
>
{children}
</div>
)
},
},
}