2018-09-05 13:28:54 +00:00
|
|
|
import Icon from '../icon'
|
|
|
|
import VcTabs, { TabPane } from '../vc-tabs/src'
|
|
|
|
import ScrollableInkTabBar from '../vc-tabs/src/ScrollableInkTabBar'
|
|
|
|
import TabContent from '../vc-tabs/src/TabContent'
|
2017-11-30 11:11:42 +00:00
|
|
|
import isFlexSupported from '../_util/isFlexSupported'
|
2018-09-05 13:28:54 +00:00
|
|
|
import { hasProp, getComponentFromProp, isEmptyElement, getSlotOptions, getOptionProps, filterEmpty, mergeProps } from '../_util/props-util'
|
2018-01-21 03:52:34 +00:00
|
|
|
import warning from '../_util/warning'
|
2018-09-05 13:28:54 +00:00
|
|
|
import { cloneElement } from '../_util/vnode'
|
2017-11-30 11:11:42 +00:00
|
|
|
export default {
|
2018-09-05 13:28:54 +00:00
|
|
|
TabPane,
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ATabs',
|
2017-11-30 11:11:42 +00:00
|
|
|
props: {
|
|
|
|
prefixCls: { type: String, default: 'ant-tabs' },
|
|
|
|
activeKey: String,
|
|
|
|
defaultActiveKey: String,
|
|
|
|
hideAdd: { type: Boolean, default: false },
|
|
|
|
tabBarStyle: Object,
|
2017-12-01 10:48:16 +00:00
|
|
|
tabBarExtraContent: [String, Number, Function],
|
|
|
|
destroyInactiveTabPane: { type: Boolean, default: false },
|
2017-11-30 11:11:42 +00:00
|
|
|
type: {
|
|
|
|
validator (value) {
|
|
|
|
return ['line', 'card', 'editable-card'].includes(value)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
tabPosition: {
|
|
|
|
validator (value) {
|
|
|
|
return ['top', 'right', 'bottom', 'left'].includes(value)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
size: {
|
|
|
|
validator (value) {
|
2018-01-20 12:55:37 +00:00
|
|
|
return ['default', 'small', 'large'].includes(value)
|
2017-11-30 11:11:42 +00:00
|
|
|
},
|
|
|
|
},
|
2017-12-01 10:48:16 +00:00
|
|
|
animated: { type: [Boolean, Object], default: undefined },
|
2018-02-05 05:50:04 +00:00
|
|
|
tabBarGutter: Number,
|
2017-12-01 10:48:16 +00:00
|
|
|
},
|
|
|
|
model: {
|
|
|
|
prop: 'activeKey',
|
|
|
|
event: 'change',
|
2017-11-30 11:11:42 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
createNewTab (targetKey) {
|
2017-12-01 10:48:16 +00:00
|
|
|
this.$emit('edit', targetKey, 'add')
|
2017-11-30 11:11:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
removeTab (targetKey, e) {
|
|
|
|
e.stopPropagation()
|
|
|
|
if (!targetKey) {
|
|
|
|
return
|
|
|
|
}
|
2017-12-01 10:48:16 +00:00
|
|
|
this.$emit('edit', targetKey, 'remove')
|
2017-11-30 11:11:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
handleChange (activeKey) {
|
2017-12-01 04:21:43 +00:00
|
|
|
this.$emit('change', activeKey)
|
2017-11-30 11:11:42 +00:00
|
|
|
},
|
2017-12-01 10:48:16 +00:00
|
|
|
onTabClick (val) {
|
|
|
|
this.$emit('tabClick', val)
|
|
|
|
},
|
|
|
|
onPrevClick (val) {
|
|
|
|
this.$emit('prevClick', val)
|
|
|
|
},
|
|
|
|
onNextClick (val) {
|
|
|
|
this.$emit('nextClick', val)
|
|
|
|
},
|
2017-11-30 11:11:42 +00: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 10:48:16 +00:00
|
|
|
render (createElement) {
|
2017-11-30 11:11:42 +00:00
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
size,
|
|
|
|
type = 'line',
|
|
|
|
tabPosition,
|
|
|
|
tabBarStyle,
|
2017-12-01 10:48:16 +00:00
|
|
|
hideAdd,
|
2018-02-05 05:50:04 +00:00
|
|
|
onTabClick,
|
2017-11-30 11:11:42 +00:00
|
|
|
onPrevClick,
|
|
|
|
onNextClick,
|
2017-12-01 10:48:16 +00:00
|
|
|
animated,
|
2018-02-05 05:50:04 +00:00
|
|
|
tabBarGutter,
|
2017-12-01 10:48:16 +00:00
|
|
|
} = this
|
2018-09-05 13:28:54 +00:00
|
|
|
const children = filterEmpty(this.$slots.default)
|
|
|
|
let tabBarExtraContent = getComponentFromProp(this, 'tabBarExtraContent')
|
2017-11-30 11:11:42 +00:00
|
|
|
let { inkBarAnimated, tabPaneAnimated } = typeof animated === 'object' ? { // eslint-disable-line
|
2017-12-01 10:48:16 +00:00
|
|
|
inkBarAnimated: !!animated.inkBar, tabPaneAnimated: !!animated.tabPane,
|
2017-11-30 11:11:42 +00:00
|
|
|
} : {
|
2017-12-01 10:48:16 +00:00
|
|
|
inkBarAnimated: animated === undefined || animated, tabPaneAnimated: animated === undefined || animated,
|
2017-11-30 11:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// card tabs should not have animation
|
|
|
|
if (type !== 'line') {
|
2017-12-01 10:48:16 +00:00
|
|
|
tabPaneAnimated = animated === undefined ? false : tabPaneAnimated
|
2017-11-30 11:11:42 +00:00
|
|
|
}
|
|
|
|
const cls = {
|
2018-01-20 12:55:37 +00:00
|
|
|
[`${prefixCls}-small`]: size === 'small',
|
|
|
|
[`${prefixCls}-large`]: size === 'large',
|
|
|
|
[`${prefixCls}-default`]: size === 'default',
|
2017-11-30 11:11:42 +00:00
|
|
|
[`${prefixCls}-vertical`]: tabPosition === 'left' || tabPosition === 'right',
|
|
|
|
[`${prefixCls}-card`]: type.indexOf('card') >= 0,
|
|
|
|
[`${prefixCls}-${type}`]: true,
|
|
|
|
[`${prefixCls}-no-animation`]: !tabPaneAnimated,
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
// only card type tabs can be added and closed
|
|
|
|
let childrenWithClose = []
|
|
|
|
if (type === 'editable-card') {
|
|
|
|
childrenWithClose = []
|
|
|
|
children.forEach((child, index) => {
|
|
|
|
const props = getOptionProps(child)
|
|
|
|
let closable = props.closable
|
|
|
|
closable = typeof closable === 'undefined' ? true : closable
|
|
|
|
const closeIcon = closable ? (
|
|
|
|
<Icon
|
|
|
|
type='close'
|
|
|
|
onClick={e => this.removeTab(child.key, e)}
|
|
|
|
/>
|
|
|
|
) : null
|
|
|
|
childrenWithClose.push(cloneElement(child, {
|
|
|
|
props: {
|
|
|
|
tab: (
|
|
|
|
<div class={closable ? undefined : `${prefixCls}-tab-unclosable`}>
|
|
|
|
{getComponentFromProp(child, 'tab')}
|
|
|
|
{closeIcon}
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
key: child.key || index,
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
// Add new tab handler
|
|
|
|
if (!hideAdd) {
|
|
|
|
tabBarExtraContent = (
|
|
|
|
<span>
|
|
|
|
<Icon type='plus' class={`${prefixCls}-new-tab`} onClick={this.createNewTab} />
|
|
|
|
{tabBarExtraContent}
|
|
|
|
</span>
|
|
|
|
)
|
2017-12-08 03:46:53 +00:00
|
|
|
}
|
2017-11-30 11:11:42 +00:00
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
tabBarExtraContent = tabBarExtraContent ? (
|
|
|
|
<div class={`${prefixCls}-extra-content`}>
|
|
|
|
{tabBarExtraContent}
|
|
|
|
</div>
|
|
|
|
) : null
|
|
|
|
|
|
|
|
const renderTabBar = () => {
|
|
|
|
const scrollableInkTabBarProps = {
|
|
|
|
props: {
|
|
|
|
inkBarAnimated,
|
|
|
|
extraContent: tabBarExtraContent,
|
|
|
|
tabBarGutter,
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
tabClick: onTabClick,
|
|
|
|
prevClick: onPrevClick,
|
|
|
|
nextClick: onNextClick,
|
|
|
|
},
|
|
|
|
style: tabBarStyle,
|
|
|
|
}
|
|
|
|
return <ScrollableInkTabBar {...scrollableInkTabBarProps}/>
|
2017-12-01 04:21:43 +00:00
|
|
|
}
|
2017-12-01 10:48:16 +00:00
|
|
|
const tabsProps = {
|
|
|
|
props: {
|
2018-09-05 13:28:54 +00:00
|
|
|
...getOptionProps(this),
|
2017-12-01 10:48:16 +00:00
|
|
|
tabBarPosition: tabPosition,
|
2018-09-05 13:28:54 +00:00
|
|
|
renderTabBar: renderTabBar,
|
|
|
|
renderTabContent: () => <TabContent animated={tabPaneAnimated} animatedWithMargin />,
|
|
|
|
children: childrenWithClose.length > 0 ? childrenWithClose : children,
|
|
|
|
__propsSymbol__: Symbol(),
|
2017-12-01 10:48:16 +00:00
|
|
|
},
|
2018-09-13 13:23:13 +00:00
|
|
|
on: {
|
|
|
|
...this.$listeners,
|
|
|
|
change: this.handleChange,
|
|
|
|
},
|
|
|
|
class: cls,
|
2017-12-01 10:48:16 +00:00
|
|
|
}
|
2017-11-30 11:11:42 +00:00
|
|
|
return (
|
2018-09-13 13:23:13 +00:00
|
|
|
<VcTabs {...tabsProps} />
|
2017-11-30 11:11:42 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
2018-03-19 02:16:27 +00:00
|
|
|
|