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';
|
2018-08-28 18:56:25 +08:00
|
|
|
import TabBar from './TabBar';
|
2015-12-14 16:30:15 +08:00
|
|
|
import classNames from 'classnames';
|
2015-12-14 22:16:25 +08:00
|
|
|
import Icon from '../icon';
|
2016-12-16 17:08:11 +08:00
|
|
|
import warning from '../_util/warning';
|
2017-02-08 17:15:14 +08:00
|
|
|
import isFlexSupported from '../_util/isFlexSupported';
|
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;
|
2017-04-05 10:14:03 +08:00
|
|
|
onPrevClick?: React.MouseEventHandler<any>;
|
|
|
|
onNextClick?: React.MouseEventHandler<any>;
|
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;
|
2017-11-20 22:06:01 +08:00
|
|
|
onEdit?: (targetKey: string | React.MouseEvent<HTMLElement>, action: any) => 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;
|
2017-03-24 14:28:11 +08:00
|
|
|
animated?: boolean | { inkBar: boolean; tabPane: boolean; };
|
2017-12-17 23:47:19 +08:00
|
|
|
tabBarGutter?: number;
|
2018-08-28 18:56:25 +08:00
|
|
|
renderTabBar?: (props: TabsProps, DefaultTabBar: React.ReactNode) => React.ReactElement<any>;
|
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 = {
|
|
|
|
prefixCls: 'ant-tabs',
|
2016-05-24 14:35:21 +08:00
|
|
|
hideAdd: false,
|
2018-11-23 13:39:33 +08:00
|
|
|
tabPosition: 'top',
|
2016-07-09 10:50:51 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
const onEdit = this.props.onEdit;
|
|
|
|
if (onEdit) {
|
|
|
|
onEdit(targetKey, 'remove');
|
|
|
|
}
|
2015-12-14 22:16:25 +08:00
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2017-11-20 22:06:01 +08:00
|
|
|
handleChange = (activeKey: string) => {
|
2016-10-24 16:30:38 +08:00
|
|
|
const onChange = this.props.onChange;
|
|
|
|
if (onChange) {
|
|
|
|
onChange(activeKey);
|
|
|
|
}
|
2015-12-14 22:16:25 +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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:15:14 +08:00
|
|
|
componentDidMount() {
|
|
|
|
const NO_FLEX = ' no-flex';
|
2018-04-11 12:01:28 +08:00
|
|
|
const tabNode = ReactDOM.findDOMNode(this) as Element;
|
2017-02-08 17:15:14 +08:00
|
|
|
if (tabNode && !isFlexSupported() && tabNode.className.indexOf(NO_FLEX) === -1) {
|
|
|
|
tabNode.className += NO_FLEX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-12 12:01:02 +08:00
|
|
|
render() {
|
2018-11-10 21:40:21 +08:00
|
|
|
const {
|
2016-09-18 10:12:21 +08:00
|
|
|
prefixCls,
|
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')),
|
2018-08-31 16:02: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
|
|
|
);
|
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 = [];
|
2016-10-31 13:15:13 +08:00
|
|
|
React.Children.forEach(children as React.ReactNode, (child: React.ReactElement<any>, index) => {
|
2017-02-13 15:43:54 +08:00
|
|
|
let closable = child.props.closable;
|
|
|
|
closable = typeof closable === 'undefined' ? true : closable;
|
|
|
|
const closeIcon = closable ? (
|
2018-08-16 16:13:18 +08:00
|
|
|
<Icon
|
|
|
|
type="close"
|
|
|
|
className={`${prefixCls}-close-x`}
|
|
|
|
onClick={e => this.removeTab(child.key as string, e)}
|
|
|
|
/>
|
2017-02-13 15:43:54 +08:00
|
|
|
) : null;
|
2017-11-17 14:38:54 +08:00
|
|
|
childrenWithClose.push(React.cloneElement(child, {
|
2016-08-16 23:13:31 +08:00
|
|
|
tab: (
|
2017-02-13 15:43:54 +08:00
|
|
|
<div className={closable ? undefined : `${prefixCls}-tab-unclosable`}>
|
2016-08-16 23:13:31 +08:00
|
|
|
{child.props.tab}
|
2017-02-13 15:43:54 +08:00
|
|
|
{closeIcon}
|
2016-08-16 23:13:31 +08:00
|
|
|
</div>
|
|
|
|
),
|
2016-03-02 20:25:19 +08:00
|
|
|
key: child.key || index,
|
2016-08-16 23:13:31 +08:00
|
|
|
}));
|
2016-03-02 20:25:19 +08:00
|
|
|
});
|
2015-12-14 22:16:25 +08:00
|
|
|
// Add new tab handler
|
2016-05-24 14:35:21 +08:00
|
|
|
if (!hideAdd) {
|
|
|
|
tabBarExtraContent = (
|
|
|
|
<span>
|
|
|
|
<Icon type="plus" className={`${prefixCls}-new-tab`} onClick={this.createNewTab} />
|
|
|
|
{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 ? (
|
|
|
|
<div className={`${prefixCls}-extra-content`}>
|
|
|
|
{tabBarExtraContent}
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
|
2018-09-06 14:36:40 +08:00
|
|
|
const { className: dropped, ...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}
|
2016-10-24 16:30:38 +08:00
|
|
|
className={cls}
|
2016-09-18 10:12:21 +08:00
|
|
|
tabBarPosition={tabPosition}
|
2018-09-06 14:36:40 +08:00
|
|
|
renderTabBar={() => <TabBar {...tabBarProps} tabBarExtraContent={tabBarExtraContent}/>}
|
2018-11-23 13:39:33 +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
|
|
|
);
|
2015-06-12 12:01:02 +08:00
|
|
|
}
|
|
|
|
}
|