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

133 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-12-01 10:48:16 +00:00
import Icon from '../icon'
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-12-01 10:48:16 +00:00
extraContent: [String, Number, Function],
hideAdd: Boolean,
removeTab: {
default: () => {},
type: Function,
},
createNewTab: {
default: () => {},
type: Function,
},
2017-11-30 11:11:42 +00:00
},
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 10:48:16 +00:00
let { disabled, closable } = child
const { tabKey, tab } = child
2017-12-01 04:21:43 +00:00
// componentOptions.propsData中获取的值disabled没有根据类型初始化, 会出现空字符串
2017-12-01 10:48:16 +00:00
disabled = disabled === '' || disabled
let cls = activeKey === tabKey ? `${prefixCls}-tab-active` : ''
2017-11-22 07:05:53 +00:00
cls += ` ${prefixCls}-tab`
2017-12-01 10:48:16 +00:00
if (disabled) {
2017-11-22 07:05:53 +00:00
cls += ` ${prefixCls}-tab-disabled`
} else {
}
const onClick = () => {
2017-12-01 10:48:16 +00:00
!disabled && this.onTabClick(tabKey)
}
let tabC = typeof tab === 'function' ? child.tab(h, tabKey) : tab
if (this.$parent.type === 'editable-card') {
closable = closable === undefined ? true : closable === '' || closable
const closeIcon = closable ? (
<Icon
type='close'
onClick={e => this.removeTab(tabKey, e)}
/>
) : null
tabC = <div class={closable ? undefined : `${prefixCls}-tab-unclosable`}>
{tabC}
{closeIcon}
</div>
2017-11-22 07:05:53 +00:00
}
2017-12-01 10:48:16 +00:00
2017-11-22 07:05:53 +00:00
rst.push(
<div
role='tab'
2017-12-01 10:48:16 +00:00
aria-disabled={disabled ? 'true' : 'false'}
aria-selected={activeKey === tabKey ? 'true' : 'false'}
2017-11-22 07:05:53 +00:00
class={cls}
2017-12-01 10:48:16 +00:00
key={tabKey}
2017-11-22 07:05:53 +00:00
onClick={onClick}
2017-12-01 10:48:16 +00:00
ref={activeKey === tabKey ? 'activeTab' : undefined}
2017-11-22 07:05:53 +00:00
>
2017-12-01 10:48:16 +00:00
{tabC}
2017-11-22 07:05:53 +00:00
</div>
)
})
return rst
},
getRootNode (contents, createElement) {
const {
2017-12-01 10:48:16 +00:00
prefixCls, onKeyDown, tabBarPosition, hideAdd,
2017-11-22 07:05:53 +00:00
} = this
2017-12-01 10:48:16 +00:00
let extraContent = this.extraContent
const tabsType = this.$parent.type
2017-11-22 07:05:53 +00:00
const cls = {
[`${prefixCls}-bar`]: true,
}
const topOrBottom = (tabBarPosition === 'top' || tabBarPosition === 'bottom')
const tabBarExtraContentStyle = topOrBottom ? { float: 'right' } : {}
let children = contents
2017-12-01 10:48:16 +00:00
extraContent = typeof extraContent === 'function' ? extraContent(createElement) : extraContent
2017-12-07 07:05:33 +00:00
extraContent = extraContent || this.$slots.extraContent
2017-12-01 10:48:16 +00:00
if (tabsType === 'editable-card' && !hideAdd) {
extraContent = (
<span>
<Icon type='plus' class={`${prefixCls}-new-tab`} onClick={this.createNewTab} />
{extraContent}
</span>
)
2017-11-22 07:05:53 +00:00
}
2017-12-01 10:48:16 +00:00
children = [
<div key='extra' class={`${prefixCls}-extra-content`} style={tabBarExtraContentStyle}>
{extraContent}
</div>,
contents,
]
children = topOrBottom ? children : children.reverse()
2017-11-22 07:05:53 +00:00
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>
)
},
},
}