2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2016-09-18 10:12:21 +08:00
|
|
|
import RcTabs, { TabPane } from 'rc-tabs';
|
|
|
|
import TabContent from 'rc-tabs/lib/TabContent';
|
2015-12-14 16:30:15 +08:00
|
|
|
import classNames from 'classnames';
|
2019-08-05 18:38:10 +08:00
|
|
|
import omit from 'omit.js';
|
2019-11-28 12:34:33 +08:00
|
|
|
import { CloseOutlined, PlusOutlined } from '@ant-design/icons';
|
2019-08-05 18:38:10 +08:00
|
|
|
import TabBar from './TabBar';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2016-12-16 17:08:11 +08:00
|
|
|
import warning from '../_util/warning';
|
2019-01-26 11:55:53 +08:00
|
|
|
import { isFlexSupported } from '../_util/styleChecker';
|
2015-06-12 12:01:02 +08:00
|
|
|
|
2017-03-23 21:15:49 +08:00
|
|
|
export type TabsType = 'line' | 'card' | 'editable-card';
|
2016-09-13 15:31:29 +08:00
|
|
|
export type TabsPosition = 'top' | 'right' | 'bottom' | 'left';
|
2016-07-09 10:50:51 +08:00
|
|
|
|
|
|
|
export interface TabsProps {
|
|
|
|
activeKey?: string;
|
|
|
|
defaultActiveKey?: string;
|
|
|
|
hideAdd?: boolean;
|
|
|
|
onChange?: (activeKey: string) => void;
|
|
|
|
onTabClick?: Function;
|
2019-06-16 20:51:47 +08:00
|
|
|
onPrevClick?: React.MouseEventHandler<HTMLElement>;
|
|
|
|
onNextClick?: React.MouseEventHandler<HTMLElement>;
|
2016-10-24 16:30:38 +08:00
|
|
|
tabBarExtraContent?: React.ReactNode | null;
|
2017-02-21 14:43:48 +08:00
|
|
|
tabBarStyle?: React.CSSProperties;
|
2016-07-09 10:50:51 +08:00
|
|
|
type?: TabsType;
|
|
|
|
tabPosition?: TabsPosition;
|
2019-06-16 20:51:47 +08:00
|
|
|
onEdit?: (targetKey: string | React.MouseEvent<HTMLElement>, action: 'add' | 'remove') => void;
|
2017-10-18 21:17:18 +08:00
|
|
|
size?: 'large' | 'default' | 'small';
|
2016-07-09 10:50:51 +08:00
|
|
|
style?: React.CSSProperties;
|
2016-08-16 18:07:37 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2018-12-07 20:02:01 +08:00
|
|
|
animated?: boolean | { inkBar: boolean; tabPane: boolean };
|
2017-12-17 23:47:19 +08:00
|
|
|
tabBarGutter?: number;
|
2019-07-16 17:32:55 +08:00
|
|
|
renderTabBar?: (
|
|
|
|
props: TabsProps,
|
|
|
|
DefaultTabBar: React.ComponentClass<any>,
|
|
|
|
) => React.ReactElement<any>;
|
2019-06-22 14:49:15 +08:00
|
|
|
destroyInactiveTabPane?: boolean;
|
2016-07-09 10:50:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tabs
|
|
|
|
export interface TabPaneProps {
|
2016-07-13 17:22:23 +08:00
|
|
|
/** 选项卡头显示文字 */
|
2016-09-28 16:58:14 +08:00
|
|
|
tab?: React.ReactNode | string;
|
2016-07-09 10:50:51 +08:00
|
|
|
style?: React.CSSProperties;
|
2017-02-13 15:43:54 +08:00
|
|
|
closable?: boolean;
|
2017-02-09 16:25:16 +08:00
|
|
|
className?: string;
|
|
|
|
disabled?: boolean;
|
2017-10-03 11:02:45 +08:00
|
|
|
forceRender?: boolean;
|
2018-10-21 00:24:47 +08:00
|
|
|
key?: string;
|
2016-07-09 10:50:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class Tabs extends React.Component<TabsProps, any> {
|
2016-09-28 16:58:14 +08:00
|
|
|
static TabPane = TabPane as React.ClassicComponentClass<TabPaneProps>;
|
2016-03-29 14:01:10 +08:00
|
|
|
|
|
|
|
static defaultProps = {
|
2016-05-24 14:35:21 +08:00
|
|
|
hideAdd: false,
|
2018-12-22 21:21:42 +08:00
|
|
|
tabPosition: 'top' as TabsPosition,
|
2016-07-09 10:50:51 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
componentDidMount() {
|
|
|
|
const NO_FLEX = ' no-flex';
|
|
|
|
const tabNode = ReactDOM.findDOMNode(this) as Element;
|
|
|
|
if (tabNode && !isFlexSupported && tabNode.className.indexOf(NO_FLEX) === -1) {
|
|
|
|
tabNode.className += NO_FLEX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:06:01 +08:00
|
|
|
removeTab = (targetKey: string, e: React.MouseEvent<HTMLElement>) => {
|
2015-12-14 22:16:25 +08:00
|
|
|
e.stopPropagation();
|
2015-12-15 16:34:02 +08:00
|
|
|
if (!targetKey) {
|
|
|
|
return;
|
2015-12-14 22:16:25 +08:00
|
|
|
}
|
2016-10-24 16:30:38 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
const { onEdit } = this.props;
|
2016-10-24 16:30:38 +08:00
|
|
|
if (onEdit) {
|
|
|
|
onEdit(targetKey, 'remove');
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2017-11-20 22:06:01 +08:00
|
|
|
handleChange = (activeKey: string) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
const { onChange } = this.props;
|
2016-10-24 16:30:38 +08:00
|
|
|
if (onChange) {
|
|
|
|
onChange(activeKey);
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2018-08-28 18:56:25 +08:00
|
|
|
createNewTab = (targetKey: React.MouseEvent<HTMLElement>) => {
|
|
|
|
const { onEdit } = this.props;
|
|
|
|
if (onEdit) {
|
|
|
|
onEdit(targetKey, 'add');
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-08-28 18:56:25 +08:00
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
renderTabs = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2018-11-10 21:40:21 +08:00
|
|
|
const {
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2016-10-24 16:30:38 +08:00
|
|
|
className = '',
|
2016-09-18 10:12:21 +08:00
|
|
|
size,
|
2016-10-24 16:30:38 +08:00
|
|
|
type = 'line',
|
2016-09-18 10:12:21 +08:00
|
|
|
tabPosition,
|
|
|
|
children,
|
2018-08-28 18:56:25 +08:00
|
|
|
animated = true,
|
2016-09-18 10:12:21 +08:00
|
|
|
hideAdd,
|
|
|
|
} = this.props;
|
2018-11-10 21:40:21 +08:00
|
|
|
let { tabBarExtraContent } = this.props;
|
2017-03-24 14:28:11 +08:00
|
|
|
|
2018-08-28 18:56:25 +08:00
|
|
|
let tabPaneAnimated = typeof animated === 'object' ? animated.tabPane : animated;
|
2017-03-24 14:28:11 +08:00
|
|
|
|
2017-05-10 20:49:05 +08:00
|
|
|
// card tabs should not have animation
|
2017-04-15 15:26:35 +08:00
|
|
|
if (type !== 'line') {
|
2017-05-10 20:49:05 +08:00
|
|
|
tabPaneAnimated = 'animated' in this.props ? tabPaneAnimated : false;
|
2017-04-15 15:26:35 +08:00
|
|
|
}
|
|
|
|
|
2016-12-16 17:08:11 +08:00
|
|
|
warning(
|
2017-10-18 21:17:18 +08:00
|
|
|
!(type.indexOf('card') >= 0 && (size === 'small' || size === 'large')),
|
2019-02-27 15:32:29 +08:00
|
|
|
'Tabs',
|
|
|
|
"`type=card|editable-card` doesn't have small or large size, it's by design.",
|
2016-12-16 17:08:11 +08:00
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('tabs', customizePrefixCls);
|
2017-10-18 21:17:18 +08:00
|
|
|
const cls = classNames(className, {
|
2016-02-17 18:04:42 +08:00
|
|
|
[`${prefixCls}-vertical`]: tabPosition === 'left' || tabPosition === 'right',
|
2017-10-18 21:17:18 +08:00
|
|
|
[`${prefixCls}-${size}`]: !!size,
|
2016-02-17 18:04:42 +08:00
|
|
|
[`${prefixCls}-card`]: type.indexOf('card') >= 0,
|
2016-03-02 20:25:19 +08:00
|
|
|
[`${prefixCls}-${type}`]: true,
|
2017-08-11 19:59:55 +08:00
|
|
|
[`${prefixCls}-no-animation`]: !tabPaneAnimated,
|
2015-12-14 16:30:15 +08:00
|
|
|
});
|
2015-12-14 22:16:25 +08:00
|
|
|
// only card type tabs can be added and closed
|
2017-11-20 22:06:01 +08:00
|
|
|
let childrenWithClose: React.ReactElement<any>[] = [];
|
2015-12-15 16:34:02 +08:00
|
|
|
if (type === 'editable-card') {
|
2016-09-18 10:12:21 +08:00
|
|
|
childrenWithClose = [];
|
2019-07-30 16:10:14 +08:00
|
|
|
React.Children.forEach(children as React.ReactNode, (child, index) => {
|
|
|
|
if (!React.isValidElement(child)) return child;
|
2019-08-05 18:38:10 +08:00
|
|
|
let { closable } = child.props;
|
2019-07-30 16:10:14 +08:00
|
|
|
closable = typeof closable === 'undefined' ? true : closable;
|
|
|
|
const closeIcon = closable ? (
|
2019-11-28 12:34:33 +08:00
|
|
|
<CloseOutlined
|
2019-07-30 16:10:14 +08:00
|
|
|
className={`${prefixCls}-close-x`}
|
|
|
|
onClick={e => this.removeTab(child.key as string, e)}
|
|
|
|
/>
|
|
|
|
) : null;
|
|
|
|
childrenWithClose.push(
|
|
|
|
React.cloneElement(child, {
|
|
|
|
tab: (
|
|
|
|
<div className={closable ? undefined : `${prefixCls}-tab-unclosable`}>
|
|
|
|
{child.props.tab}
|
|
|
|
{closeIcon}
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
key: child.key || index,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2015-12-14 22:16:25 +08:00
|
|
|
// Add new tab handler
|
2016-05-24 14:35:21 +08:00
|
|
|
if (!hideAdd) {
|
|
|
|
tabBarExtraContent = (
|
|
|
|
<span>
|
2019-11-28 12:34:33 +08:00
|
|
|
<PlusOutlined className={`${prefixCls}-new-tab`} onClick={this.createNewTab} />
|
2016-05-24 14:35:21 +08:00
|
|
|
{tabBarExtraContent}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
2015-12-14 22:16:25 +08:00
|
|
|
}
|
2016-03-02 20:25:19 +08:00
|
|
|
|
2016-04-17 17:47:08 +08:00
|
|
|
tabBarExtraContent = tabBarExtraContent ? (
|
2018-12-07 20:02:01 +08:00
|
|
|
<div className={`${prefixCls}-extra-content`}>{tabBarExtraContent}</div>
|
2016-04-17 17:47:08 +08:00
|
|
|
) : null;
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
const { ...tabBarProps } = this.props;
|
2018-11-23 13:39:33 +08:00
|
|
|
const contentCls: string = classNames(
|
|
|
|
`${prefixCls}-${tabPosition}-content`,
|
|
|
|
type.indexOf('card') >= 0 && `${prefixCls}-card-content`,
|
|
|
|
);
|
2018-09-06 14:36:40 +08:00
|
|
|
|
2016-01-07 14:21:29 +08:00
|
|
|
return (
|
2016-11-23 17:53:10 +08:00
|
|
|
<RcTabs
|
|
|
|
{...this.props}
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls={prefixCls}
|
2016-10-24 16:30:38 +08:00
|
|
|
className={cls}
|
2016-09-18 10:12:21 +08:00
|
|
|
tabBarPosition={tabPosition}
|
2020-01-02 19:10:16 +08:00
|
|
|
direction={direction}
|
2019-08-05 18:38:10 +08:00
|
|
|
renderTabBar={() => (
|
|
|
|
<TabBar {...omit(tabBarProps, ['className'])} tabBarExtraContent={tabBarExtraContent} />
|
|
|
|
)}
|
2018-12-07 20:02:01 +08:00
|
|
|
renderTabContent={() => (
|
|
|
|
<TabContent className={contentCls} animated={tabPaneAnimated} animatedWithMargin />
|
|
|
|
)}
|
2016-01-07 17:46:46 +08:00
|
|
|
onChange={this.handleChange}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2017-11-20 22:06:01 +08:00
|
|
|
{childrenWithClose.length > 0 ? childrenWithClose : children}
|
2016-03-21 21:16:38 +08:00
|
|
|
</RcTabs>
|
2016-01-07 14:21:29 +08:00
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderTabs}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2015-06-12 12:01:02 +08:00
|
|
|
}
|