2019-01-12 11:33:27 +08:00
|
|
|
import Icon from '../icon';
|
|
|
|
import VcTabs, { TabPane } from '../vc-tabs/src';
|
|
|
|
import TabContent from '../vc-tabs/src/TabContent';
|
2019-09-20 19:19:59 +08:00
|
|
|
import { isFlexSupported } from '../_util/styleChecker';
|
2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-01-18 21:44:13 +08:00
|
|
|
import {
|
|
|
|
getComponentFromProp,
|
|
|
|
getOptionProps,
|
|
|
|
filterEmpty,
|
|
|
|
getListeners,
|
|
|
|
} from '../_util/props-util';
|
2019-01-12 11:33:27 +08:00
|
|
|
import { cloneElement } from '../_util/vnode';
|
2019-04-17 09:21:35 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2019-01-12 11:33:27 +08:00
|
|
|
import TabBar from './TabBar';
|
2018-12-11 20:53:56 +08:00
|
|
|
|
2017-11-30 19:11:42 +08:00
|
|
|
export default {
|
2018-09-05 21:28:54 +08:00
|
|
|
TabPane,
|
2018-04-08 21:17:20 +08:00
|
|
|
name: 'ATabs',
|
2019-02-01 17:23:00 +08:00
|
|
|
model: {
|
|
|
|
prop: 'activeKey',
|
|
|
|
event: 'change',
|
|
|
|
},
|
2017-11-30 19:11:42 +08:00
|
|
|
props: {
|
2019-04-17 09:21:35 +08:00
|
|
|
prefixCls: PropTypes.string,
|
2019-01-12 11:33:27 +08:00
|
|
|
activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
|
|
defaultActiveKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2018-12-11 20:53:56 +08:00
|
|
|
hideAdd: PropTypes.bool.def(false),
|
|
|
|
tabBarStyle: PropTypes.object,
|
2020-03-07 19:45:13 +08:00
|
|
|
tabBarExtraContent: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number,
|
|
|
|
PropTypes.func,
|
|
|
|
PropTypes.array,
|
|
|
|
]),
|
2018-12-11 20:53:56 +08:00
|
|
|
destroyInactiveTabPane: PropTypes.bool.def(false),
|
|
|
|
type: PropTypes.oneOf(['line', 'card', 'editable-card']),
|
2018-12-14 23:31:02 +08:00
|
|
|
tabPosition: PropTypes.oneOf(['top', 'right', 'bottom', 'left']).def('top'),
|
2018-12-11 20:53:56 +08:00
|
|
|
size: PropTypes.oneOf(['default', 'small', 'large']),
|
2019-01-12 11:33:27 +08:00
|
|
|
animated: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
|
2018-12-11 20:53:56 +08:00
|
|
|
tabBarGutter: PropTypes.number,
|
|
|
|
renderTabBar: PropTypes.func,
|
2017-12-01 18:48:16 +08:00
|
|
|
},
|
2019-04-17 09:21:35 +08:00
|
|
|
inject: {
|
2019-09-11 22:35:25 +08:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-04-17 09:21:35 +08:00
|
|
|
},
|
2019-02-01 17:23:00 +08:00
|
|
|
mounted() {
|
|
|
|
const NO_FLEX = ' no-flex';
|
|
|
|
const tabNode = this.$el;
|
2019-09-20 19:19:59 +08:00
|
|
|
if (tabNode && !isFlexSupported && tabNode.className.indexOf(NO_FLEX) === -1) {
|
2019-02-01 17:23:00 +08:00
|
|
|
tabNode.className += NO_FLEX;
|
|
|
|
}
|
2017-11-30 19:11:42 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
removeTab(targetKey, e) {
|
|
|
|
e.stopPropagation();
|
2017-11-30 19:11:42 +08:00
|
|
|
if (!targetKey) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2017-11-30 19:11:42 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.$emit('edit', targetKey, 'remove');
|
2017-11-30 19:11:42 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
handleChange(activeKey) {
|
|
|
|
this.$emit('change', activeKey);
|
2017-11-30 19:11:42 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
createNewTab(targetKey) {
|
|
|
|
this.$emit('edit', targetKey, 'add');
|
2018-12-11 20:53:56 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
onTabClick(val) {
|
|
|
|
this.$emit('tabClick', val);
|
2017-12-01 18:48:16 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
onPrevClick(val) {
|
|
|
|
this.$emit('prevClick', val);
|
2017-12-01 18:48:16 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
onNextClick(val) {
|
|
|
|
this.$emit('nextClick', val);
|
2017-12-01 18:48:16 +08:00
|
|
|
},
|
2017-11-30 19:11:42 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
|
|
|
const props = getOptionProps(this);
|
2017-11-30 19:11:42 +08:00
|
|
|
const {
|
2019-04-17 09:21:35 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2017-11-30 19:11:42 +08:00
|
|
|
size,
|
|
|
|
type = 'line',
|
|
|
|
tabPosition,
|
2018-12-11 20:53:56 +08:00
|
|
|
animated = true,
|
2017-12-01 18:48:16 +08:00
|
|
|
hideAdd,
|
2018-12-11 20:53:56 +08:00
|
|
|
renderTabBar,
|
2019-01-12 11:33:27 +08:00
|
|
|
} = props;
|
2019-09-11 22:35:25 +08:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-04-17 09:21:35 +08:00
|
|
|
const prefixCls = getPrefixCls('tabs', customizePrefixCls);
|
2019-01-12 11:33:27 +08:00
|
|
|
const children = filterEmpty(this.$slots.default);
|
2018-12-11 20:53:56 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
let tabBarExtraContent = getComponentFromProp(this, 'tabBarExtraContent');
|
|
|
|
let tabPaneAnimated = typeof animated === 'object' ? animated.tabPane : animated;
|
2017-11-30 19:11:42 +08:00
|
|
|
|
|
|
|
// card tabs should not have animation
|
|
|
|
if (type !== 'line') {
|
2019-01-12 11:33:27 +08:00
|
|
|
tabPaneAnimated = 'animated' in props ? tabPaneAnimated : false;
|
2017-11-30 19:11:42 +08:00
|
|
|
}
|
|
|
|
const cls = {
|
|
|
|
[`${prefixCls}-vertical`]: tabPosition === 'left' || tabPosition === 'right',
|
2018-12-11 20:53:56 +08:00
|
|
|
[`${prefixCls}-${size}`]: !!size,
|
2017-11-30 19:11:42 +08:00
|
|
|
[`${prefixCls}-card`]: type.indexOf('card') >= 0,
|
|
|
|
[`${prefixCls}-${type}`]: true,
|
|
|
|
[`${prefixCls}-no-animation`]: !tabPaneAnimated,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-09-05 21:28:54 +08:00
|
|
|
// only card type tabs can be added and closed
|
2019-01-12 11:33:27 +08:00
|
|
|
let childrenWithClose = [];
|
2018-09-05 21:28:54 +08:00
|
|
|
if (type === 'editable-card') {
|
2019-01-12 11:33:27 +08:00
|
|
|
childrenWithClose = [];
|
2018-09-05 21:28:54 +08:00
|
|
|
children.forEach((child, index) => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const props = getOptionProps(child);
|
|
|
|
let closable = props.closable;
|
|
|
|
closable = typeof closable === 'undefined' ? true : closable;
|
2018-09-05 21:28:54 +08:00
|
|
|
const closeIcon = closable ? (
|
|
|
|
<Icon
|
2019-01-12 11:33:27 +08:00
|
|
|
type="close"
|
2018-12-11 20:53:56 +08:00
|
|
|
class={`${prefixCls}-close-x`}
|
2018-09-05 21:28:54 +08:00
|
|
|
onClick={e => this.removeTab(child.key, e)}
|
|
|
|
/>
|
2019-01-12 11:33:27 +08:00
|
|
|
) : null;
|
|
|
|
childrenWithClose.push(
|
|
|
|
cloneElement(child, {
|
|
|
|
props: {
|
|
|
|
tab: (
|
|
|
|
<div class={closable ? undefined : `${prefixCls}-tab-unclosable`}>
|
|
|
|
{getComponentFromProp(child, 'tab')}
|
|
|
|
{closeIcon}
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
key: child.key || index,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2018-09-05 21:28:54 +08:00
|
|
|
// Add new tab handler
|
|
|
|
if (!hideAdd) {
|
|
|
|
tabBarExtraContent = (
|
|
|
|
<span>
|
2019-01-12 11:33:27 +08:00
|
|
|
<Icon type="plus" class={`${prefixCls}-new-tab`} onClick={this.createNewTab} />
|
2018-09-05 21:28:54 +08:00
|
|
|
{tabBarExtraContent}
|
|
|
|
</span>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2017-12-08 11:46:53 +08:00
|
|
|
}
|
2017-11-30 19:11:42 +08:00
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
|
|
|
|
tabBarExtraContent = tabBarExtraContent ? (
|
2018-12-14 23:31:02 +08:00
|
|
|
<div class={`${prefixCls}-extra-content`}>{tabBarExtraContent}</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
) : null;
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const renderTabBarSlot = renderTabBar || this.$scopedSlots.renderTabBar;
|
2020-01-18 21:44:13 +08:00
|
|
|
const listeners = getListeners(this);
|
2018-12-11 20:53:56 +08:00
|
|
|
const tabBarProps = {
|
|
|
|
props: {
|
|
|
|
...this.$props,
|
2019-04-17 09:21:35 +08:00
|
|
|
prefixCls,
|
2018-12-11 20:53:56 +08:00
|
|
|
tabBarExtraContent,
|
|
|
|
renderTabBar: renderTabBarSlot,
|
|
|
|
},
|
2020-01-18 21:44:13 +08:00
|
|
|
on: listeners,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-12-14 23:31:02 +08:00
|
|
|
const contentCls = {
|
|
|
|
[`${prefixCls}-${tabPosition}-content`]: true,
|
|
|
|
[`${prefixCls}-card-content`]: type.indexOf('card') >= 0,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2017-12-01 18:48:16 +08:00
|
|
|
const tabsProps = {
|
|
|
|
props: {
|
2018-09-05 21:28:54 +08:00
|
|
|
...getOptionProps(this),
|
2019-04-17 09:21:35 +08:00
|
|
|
prefixCls,
|
2017-12-01 18:48:16 +08:00
|
|
|
tabBarPosition: tabPosition,
|
2019-01-12 11:33:27 +08:00
|
|
|
renderTabBar: () => <TabBar {...tabBarProps} />,
|
|
|
|
renderTabContent: () => (
|
|
|
|
<TabContent class={contentCls} animated={tabPaneAnimated} animatedWithMargin />
|
|
|
|
),
|
2018-09-05 21:28:54 +08:00
|
|
|
children: childrenWithClose.length > 0 ? childrenWithClose : children,
|
|
|
|
__propsSymbol__: Symbol(),
|
2017-12-01 18:48:16 +08:00
|
|
|
},
|
2018-09-13 21:23:13 +08:00
|
|
|
on: {
|
2020-01-18 21:44:13 +08:00
|
|
|
...listeners,
|
2018-09-13 21:23:13 +08:00
|
|
|
change: this.handleChange,
|
|
|
|
},
|
|
|
|
class: cls,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
|
|
|
return <VcTabs {...tabsProps} />;
|
2017-11-30 19:11:42 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|