ant-design-vue/components/tabs/index.vue

163 lines
4.3 KiB
Vue
Raw Normal View History

2017-11-30 19:11:42 +08:00
<script>
import Tabs from './Tabs'
import isFlexSupported from '../_util/isFlexSupported'
export default {
props: {
prefixCls: { type: String, default: 'ant-tabs' },
activeKey: String,
defaultActiveKey: String,
hideAdd: { type: Boolean, default: false },
tabBarStyle: Object,
2017-12-01 18:48:16 +08:00
tabBarExtraContent: [String, Number, Function],
destroyInactiveTabPane: { type: Boolean, default: false },
2017-11-30 19:11:42 +08:00
type: {
validator (value) {
return ['line', 'card', 'editable-card'].includes(value)
},
},
tabPosition: {
validator (value) {
return ['top', 'right', 'bottom', 'left'].includes(value)
},
},
size: {
validator (value) {
return ['default', 'small'].includes(value)
},
},
2017-12-01 18:48:16 +08:00
animated: { type: [Boolean, Object], default: undefined },
},
model: {
prop: 'activeKey',
event: 'change',
2017-11-30 19:11:42 +08:00
},
methods: {
createNewTab (targetKey) {
2017-12-01 18:48:16 +08:00
this.$emit('edit', targetKey, 'add')
2017-11-30 19:11:42 +08:00
},
removeTab (targetKey, e) {
e.stopPropagation()
if (!targetKey) {
return
}
2017-12-01 18:48:16 +08:00
this.$emit('edit', targetKey, 'remove')
2017-11-30 19:11:42 +08:00
},
handleChange (activeKey) {
2017-12-01 12:21:43 +08:00
this.$emit('change', activeKey)
2017-11-30 19:11:42 +08:00
},
2017-12-01 18:48:16 +08:00
onTabClick (val) {
this.$emit('tabClick', val)
},
onPrevClick (val) {
this.$emit('prevClick', val)
},
onNextClick (val) {
this.$emit('nextClick', val)
},
2017-11-30 19:11:42 +08:00
},
mounted () {
const NO_FLEX = ' no-flex'
const tabNode = this.$el
if (tabNode && !isFlexSupported() && tabNode.className.indexOf(NO_FLEX) === -1) {
tabNode.className += NO_FLEX
}
},
2017-12-01 18:48:16 +08:00
render (createElement) {
2017-11-30 19:11:42 +08:00
const {
prefixCls,
size,
type = 'line',
tabPosition,
tabBarStyle,
2017-12-01 18:48:16 +08:00
hideAdd,
2017-11-30 19:11:42 +08:00
onTabClick,
onPrevClick,
onNextClick,
2017-12-01 18:48:16 +08:00
animated,
destroyInactiveTabPane = false,
activeKey,
defaultActiveKey,
2017-12-07 15:05:33 +08:00
$slots,
2017-12-01 18:48:16 +08:00
} = this
2017-12-07 15:05:33 +08:00
let { tabBarExtraContent } = this.$props
2017-11-30 19:11:42 +08:00
let { inkBarAnimated, tabPaneAnimated } = typeof animated === 'object' ? { // eslint-disable-line
2017-12-01 18:48:16 +08:00
inkBarAnimated: !!animated.inkBar, tabPaneAnimated: !!animated.tabPane,
2017-11-30 19:11:42 +08:00
} : {
2017-12-01 18:48:16 +08:00
inkBarAnimated: animated === undefined || animated, tabPaneAnimated: animated === undefined || animated,
2017-11-30 19:11:42 +08:00
}
// card tabs should not have animation
if (type !== 'line') {
2017-12-01 18:48:16 +08:00
tabPaneAnimated = animated === undefined ? false : tabPaneAnimated
2017-11-30 19:11:42 +08:00
}
const cls = {
[`${prefixCls}-mini`]: size === 'small' || size,
[`${prefixCls}-vertical`]: tabPosition === 'left' || tabPosition === 'right',
[`${prefixCls}-card`]: type.indexOf('card') >= 0,
[`${prefixCls}-${type}`]: true,
[`${prefixCls}-no-animation`]: !tabPaneAnimated,
}
2017-12-08 11:46:53 +08:00
tabBarExtraContent = tabBarExtraContent === undefined && $slots.tabBarExtraContent
2017-12-12 11:08:56 +08:00
? $slots.tabBarExtraContent : tabBarExtraContent
tabBarExtraContent = typeof tabBarExtraContent === 'function'
? tabBarExtraContent(createElement) : tabBarExtraContent
2017-12-08 11:46:53 +08:00
$slots.default && $slots.default.forEach(({ componentOptions, key: tabKey }) => {
if (componentOptions && componentOptions.propsData.tab === undefined) {
componentOptions.propsData.tab = $slots[`tab_${tabKey}`]
2017-12-12 11:08:56 +08:00
? $slots[`tab_${tabKey}`]
2017-12-08 11:46:53 +08:00
: null
}
2017-12-07 15:05:33 +08:00
})
2017-11-30 19:11:42 +08:00
const tabBarProps = {
inkBarAnimated,
onTabClick,
onPrevClick,
onNextClick,
style: tabBarStyle,
2017-12-01 18:48:16 +08:00
hideAdd,
removeTab: this.removeTab,
createNewTab: this.createNewTab,
2017-11-30 19:11:42 +08:00
}
2017-12-01 12:21:43 +08:00
const tabContentProps = {
animated: tabPaneAnimated,
animatedWithMargin: true,
}
2017-12-01 18:48:16 +08:00
const self = this
const tabsProps = {
props: {
prefixCls,
tabBarPosition: tabPosition,
onChange: this.handleChange,
tabBarProps: tabBarProps,
tabContentProps: tabContentProps,
destroyInactiveTabPane,
activeKey,
defaultActiveKey,
type,
onTabClick: this.onTabClick,
},
on: {
change (val) {
self.handleChange(val)
},
},
}
2017-11-30 19:11:42 +08:00
return (
<Tabs
class={cls}
2017-12-01 18:48:16 +08:00
{...tabsProps}
2017-11-30 19:11:42 +08:00
>
2017-12-01 18:48:16 +08:00
{this.$slots.default}
2017-12-12 11:08:56 +08:00
{tabBarExtraContent ? <template slot='tabBarExtraContent'>
{tabBarExtraContent}
</template> : null}
2017-11-30 19:11:42 +08:00
</Tabs>
)
},
}
</script>