2020-10-13 09:46:31 +00:00
|
|
|
|
import { defineComponent, inject, PropType } from 'vue';
|
|
|
|
|
import { tuple } from '../_util/type';
|
2020-03-30 15:43:35 +00:00
|
|
|
|
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
|
|
|
|
|
import PlusOutlined from '@ant-design/icons-vue/PlusOutlined';
|
2019-01-12 03:33:27 +00:00
|
|
|
|
import VcTabs, { TabPane } from '../vc-tabs/src';
|
|
|
|
|
import TabContent from '../vc-tabs/src/TabContent';
|
2019-09-20 11:19:59 +00:00
|
|
|
|
import { isFlexSupported } from '../_util/styleChecker';
|
2020-10-11 13:31:57 +00:00
|
|
|
|
import PropTypes, { withUndefined } from '../_util/vue-types';
|
2020-01-18 13:44:13 +00:00
|
|
|
|
import {
|
2020-06-23 14:34:08 +00:00
|
|
|
|
getComponent,
|
2020-01-18 13:44:13 +00:00
|
|
|
|
getOptionProps,
|
|
|
|
|
filterEmpty,
|
2020-06-23 14:34:08 +00:00
|
|
|
|
findDOMNode,
|
|
|
|
|
getPropsData,
|
|
|
|
|
getSlot,
|
2020-01-18 13:44:13 +00:00
|
|
|
|
} from '../_util/props-util';
|
2019-01-12 03:33:27 +00:00
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
2020-05-17 03:25:36 +00:00
|
|
|
|
import isValid from '../_util/isValid';
|
2020-09-30 02:47:18 +00:00
|
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2019-01-12 03:33:27 +00:00
|
|
|
|
import TabBar from './TabBar';
|
2018-12-11 12:53:56 +00:00
|
|
|
|
|
2020-10-12 11:19:10 +00:00
|
|
|
|
export default defineComponent({
|
2018-09-05 13:28:54 +00:00
|
|
|
|
TabPane,
|
2018-04-08 13:17:20 +00:00
|
|
|
|
name: 'ATabs',
|
2020-06-24 08:07:03 +00:00
|
|
|
|
inheritAttrs: false,
|
2017-11-30 11:11:42 +00:00
|
|
|
|
props: {
|
2019-04-17 01:21:35 +00:00
|
|
|
|
prefixCls: PropTypes.string,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
|
|
|
defaultActiveKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2020-10-10 10:16:28 +00:00
|
|
|
|
hideAdd: PropTypes.looseBool.def(false),
|
2021-01-10 07:23:53 +00:00
|
|
|
|
centered: PropTypes.looseBool.def(false),
|
2018-12-11 12:53:56 +00:00
|
|
|
|
tabBarStyle: PropTypes.object,
|
2020-03-26 09:24:13 +00:00
|
|
|
|
tabBarExtraContent: PropTypes.any,
|
2020-10-10 10:16:28 +00:00
|
|
|
|
destroyInactiveTabPane: PropTypes.looseBool.def(false),
|
2020-10-13 09:46:31 +00:00
|
|
|
|
type: PropTypes.oneOf(tuple('line', 'card', 'editable-card')),
|
2018-12-14 15:31:02 +00:00
|
|
|
|
tabPosition: PropTypes.oneOf(['top', 'right', 'bottom', 'left']).def('top'),
|
2018-12-11 12:53:56 +00:00
|
|
|
|
size: PropTypes.oneOf(['default', 'small', 'large']),
|
2020-10-11 13:31:57 +00:00
|
|
|
|
animated: withUndefined(PropTypes.oneOfType([PropTypes.looseBool, PropTypes.object])),
|
2018-12-11 12:53:56 +00:00
|
|
|
|
tabBarGutter: PropTypes.number,
|
|
|
|
|
renderTabBar: PropTypes.func,
|
2020-10-13 09:46:31 +00:00
|
|
|
|
onChange: {
|
|
|
|
|
type: Function as PropType<(activeKey: string) => void>,
|
|
|
|
|
},
|
2020-08-04 09:30:31 +00:00
|
|
|
|
onTabClick: PropTypes.func,
|
2020-10-13 09:46:31 +00:00
|
|
|
|
onPrevClick: {
|
|
|
|
|
type: Function as PropType<(e: MouseEvent) => void>,
|
|
|
|
|
},
|
|
|
|
|
onNextClick: {
|
|
|
|
|
type: Function as PropType<(e: MouseEvent) => void>,
|
|
|
|
|
},
|
|
|
|
|
onEdit: {
|
|
|
|
|
type: Function as PropType<
|
|
|
|
|
(targetKey: string | MouseEvent, action: 'add' | 'remove') => void
|
|
|
|
|
>,
|
|
|
|
|
},
|
2017-12-01 10:48:16 +00:00
|
|
|
|
},
|
2020-10-13 09:46:31 +00:00
|
|
|
|
emits: ['update:activeKey', 'edit', 'change'],
|
2020-06-23 14:34:08 +00:00
|
|
|
|
setup() {
|
|
|
|
|
return {
|
2020-09-30 02:47:18 +00:00
|
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-06-23 14:34:08 +00:00
|
|
|
|
};
|
2019-04-17 01:21:35 +00:00
|
|
|
|
},
|
2019-02-01 09:23:00 +00:00
|
|
|
|
mounted() {
|
|
|
|
|
const NO_FLEX = ' no-flex';
|
2020-06-23 14:34:08 +00:00
|
|
|
|
const tabNode = findDOMNode(this);
|
2019-09-20 11:19:59 +00:00
|
|
|
|
if (tabNode && !isFlexSupported && tabNode.className.indexOf(NO_FLEX) === -1) {
|
2019-02-01 09:23:00 +00:00
|
|
|
|
tabNode.className += NO_FLEX;
|
|
|
|
|
}
|
2017-11-30 11:11:42 +00:00
|
|
|
|
},
|
|
|
|
|
methods: {
|
2020-10-13 09:46:31 +00:00
|
|
|
|
removeTab(targetKey: string, e: MouseEvent) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
e.stopPropagation();
|
2020-05-27 11:11:46 +00:00
|
|
|
|
if (isValid(targetKey)) {
|
2020-05-17 03:25:36 +00:00
|
|
|
|
this.$emit('edit', targetKey, 'remove');
|
2017-11-30 11:11:42 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
2020-10-13 09:46:31 +00:00
|
|
|
|
handleChange(activeKey: string) {
|
2020-06-23 14:34:08 +00:00
|
|
|
|
this.$emit('update:activeKey', activeKey);
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.$emit('change', activeKey);
|
2017-11-30 11:11:42 +00:00
|
|
|
|
},
|
2020-10-13 09:46:31 +00:00
|
|
|
|
createNewTab(targetKey: MouseEvent) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.$emit('edit', targetKey, 'add');
|
2018-12-11 12:53:56 +00:00
|
|
|
|
},
|
2017-11-30 11:11:42 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
render() {
|
|
|
|
|
const props = getOptionProps(this);
|
2017-11-30 11:11:42 +00:00
|
|
|
|
const {
|
2019-04-17 01:21:35 +00:00
|
|
|
|
prefixCls: customizePrefixCls,
|
2017-11-30 11:11:42 +00:00
|
|
|
|
size,
|
|
|
|
|
type = 'line',
|
|
|
|
|
tabPosition,
|
2018-12-11 12:53:56 +00:00
|
|
|
|
animated = true,
|
2017-12-01 10:48:16 +00:00
|
|
|
|
hideAdd,
|
2018-12-11 12:53:56 +00:00
|
|
|
|
renderTabBar,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
} = props;
|
2020-06-24 08:07:03 +00:00
|
|
|
|
const { class: className, ...restProps } = this.$attrs;
|
|
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-04-17 01:21:35 +00:00
|
|
|
|
const prefixCls = getPrefixCls('tabs', customizePrefixCls);
|
2020-06-23 14:34:08 +00:00
|
|
|
|
const children = filterEmpty(getSlot(this));
|
2018-12-11 12:53:56 +00:00
|
|
|
|
|
2020-06-23 14:34:08 +00:00
|
|
|
|
let tabBarExtraContent = getComponent(this, 'tabBarExtraContent');
|
2019-01-12 03:33:27 +00:00
|
|
|
|
let tabPaneAnimated = typeof animated === 'object' ? animated.tabPane : animated;
|
2017-11-30 11:11:42 +00:00
|
|
|
|
|
|
|
|
|
// card tabs should not have animation
|
|
|
|
|
if (type !== 'line') {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
tabPaneAnimated = 'animated' in props ? tabPaneAnimated : false;
|
2017-11-30 11:11:42 +00:00
|
|
|
|
}
|
|
|
|
|
const cls = {
|
2020-10-13 09:46:31 +00:00
|
|
|
|
[className as string]: className,
|
2017-11-30 11:11:42 +00:00
|
|
|
|
[`${prefixCls}-vertical`]: tabPosition === 'left' || tabPosition === 'right',
|
2018-12-11 12:53:56 +00:00
|
|
|
|
[`${prefixCls}-${size}`]: !!size,
|
2017-11-30 11:11:42 +00:00
|
|
|
|
[`${prefixCls}-card`]: type.indexOf('card') >= 0,
|
|
|
|
|
[`${prefixCls}-${type}`]: true,
|
|
|
|
|
[`${prefixCls}-no-animation`]: !tabPaneAnimated,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
|
// only card type tabs can be added and closed
|
2019-01-12 03:33:27 +00:00
|
|
|
|
let childrenWithClose = [];
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (type === 'editable-card') {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
childrenWithClose = [];
|
2018-09-05 13:28:54 +00:00
|
|
|
|
children.forEach((child, index) => {
|
2020-10-13 09:46:31 +00:00
|
|
|
|
const props = getPropsData(child) as any;
|
2019-01-12 03:33:27 +00:00
|
|
|
|
let closable = props.closable;
|
|
|
|
|
closable = typeof closable === 'undefined' ? true : closable;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const closeIcon = closable ? (
|
2020-03-30 15:43:35 +00:00
|
|
|
|
<CloseOutlined
|
2018-12-11 12:53:56 +00:00
|
|
|
|
class={`${prefixCls}-close-x`}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
onClick={e => this.removeTab(child.key, e)}
|
|
|
|
|
/>
|
2019-01-12 03:33:27 +00:00
|
|
|
|
) : null;
|
|
|
|
|
childrenWithClose.push(
|
|
|
|
|
cloneElement(child, {
|
2020-06-23 14:34:08 +00:00
|
|
|
|
tab: (
|
|
|
|
|
<div class={closable ? undefined : `${prefixCls}-tab-unclosable`}>
|
|
|
|
|
{getComponent(child, 'tab')}
|
|
|
|
|
{closeIcon}
|
|
|
|
|
</div>
|
|
|
|
|
),
|
2019-01-12 03:33:27 +00:00
|
|
|
|
key: child.key || index,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
// Add new tab handler
|
|
|
|
|
if (!hideAdd) {
|
|
|
|
|
tabBarExtraContent = (
|
|
|
|
|
<span>
|
2020-03-30 15:43:35 +00:00
|
|
|
|
<PlusOutlined class={`${prefixCls}-new-tab`} onClick={this.createNewTab} />
|
2018-09-05 13:28:54 +00:00
|
|
|
|
{tabBarExtraContent}
|
|
|
|
|
</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
|
);
|
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 ? (
|
2018-12-14 15:31:02 +00:00
|
|
|
|
<div class={`${prefixCls}-extra-content`}>{tabBarExtraContent}</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
|
) : null;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
2020-06-23 14:34:08 +00:00
|
|
|
|
const renderTabBarSlot = renderTabBar || this.$slots.renderTabBar;
|
2018-12-11 12:53:56 +00:00
|
|
|
|
const tabBarProps = {
|
2020-06-24 08:07:03 +00:00
|
|
|
|
...props,
|
2020-06-23 14:34:08 +00:00
|
|
|
|
prefixCls,
|
|
|
|
|
tabBarExtraContent,
|
|
|
|
|
renderTabBar: renderTabBarSlot,
|
|
|
|
|
...restProps,
|
2020-06-24 15:45:36 +00:00
|
|
|
|
children,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2018-12-14 15:31:02 +00:00
|
|
|
|
const contentCls = {
|
|
|
|
|
[`${prefixCls}-${tabPosition}-content`]: true,
|
|
|
|
|
[`${prefixCls}-card-content`]: type.indexOf('card') >= 0,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2017-12-01 10:48:16 +00:00
|
|
|
|
const tabsProps = {
|
2020-06-24 08:07:03 +00:00
|
|
|
|
...props,
|
2020-06-23 14:34:08 +00:00
|
|
|
|
prefixCls,
|
|
|
|
|
tabBarPosition: tabPosition,
|
|
|
|
|
// https://github.com/vueComponent/ant-design-vue/issues/2030
|
|
|
|
|
// 如仅传递 tabBarProps 会导致,第二次执行 renderTabBar 时,丢失 on 属性,
|
|
|
|
|
// 添加key之后,会在babel jsx 插件中做一次merge,最终TabBar接收的是一个新的对象,而不是 tabBarProps
|
|
|
|
|
renderTabBar: () => <TabBar key="tabBar" {...tabBarProps} />,
|
|
|
|
|
renderTabContent: () => (
|
|
|
|
|
<TabContent class={contentCls} animated={tabPaneAnimated} animatedWithMargin />
|
|
|
|
|
),
|
|
|
|
|
children: childrenWithClose.length > 0 ? childrenWithClose : children,
|
|
|
|
|
...restProps,
|
|
|
|
|
onChange: this.handleChange,
|
2018-09-13 13:23:13 +00:00
|
|
|
|
class: cls,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2020-08-27 09:28:18 +00:00
|
|
|
|
return <VcTabs {...tabsProps} />;
|
2017-11-30 11:11:42 +00:00
|
|
|
|
},
|
2020-10-12 11:19:10 +00:00
|
|
|
|
});
|