2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2020-06-03 16:13:09 +08:00
|
|
|
import RcTabs, { TabPane, TabsProps as RcTabsProps, TabPaneProps } from 'rc-tabs';
|
2020-05-30 18:28:25 +08:00
|
|
|
import { EditableConfig } from 'rc-tabs/lib/interface';
|
2015-12-14 16:30:15 +08:00
|
|
|
import classNames from 'classnames';
|
2020-05-30 18:28:25 +08:00
|
|
|
import EllipsisOutlined from '@ant-design/icons/EllipsisOutlined';
|
2020-03-02 12:09:38 +08:00
|
|
|
import PlusOutlined from '@ant-design/icons/PlusOutlined';
|
2020-05-30 18:28:25 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2020-05-30 18:28:25 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2021-03-24 12:23:24 +08:00
|
|
|
import SizeContext, { SizeType } from '../config-provider/SizeContext';
|
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
|
|
|
|
2020-06-01 16:04:13 +08:00
|
|
|
export { TabPaneProps };
|
|
|
|
|
2020-05-30 18:28:25 +08:00
|
|
|
export interface TabsProps extends Omit<RcTabsProps, 'editable'> {
|
2016-07-09 10:50:51 +08:00
|
|
|
type?: TabsType;
|
2020-05-30 18:28:25 +08:00
|
|
|
size?: SizeType;
|
|
|
|
hideAdd?: boolean;
|
2020-06-28 22:41:59 +08:00
|
|
|
centered?: boolean;
|
|
|
|
addIcon?: React.ReactNode;
|
2020-05-30 18:28:25 +08:00
|
|
|
onEdit?: (e: React.MouseEvent | React.KeyboardEvent | string, action: 'add' | 'remove') => void;
|
2016-07-09 10:50:51 +08:00
|
|
|
}
|
|
|
|
|
2021-03-24 12:23:24 +08:00
|
|
|
function Tabs({
|
|
|
|
type,
|
|
|
|
className,
|
|
|
|
size: propSize,
|
|
|
|
onEdit,
|
|
|
|
hideAdd,
|
|
|
|
centered,
|
|
|
|
addIcon,
|
|
|
|
...props
|
|
|
|
}: TabsProps) {
|
|
|
|
const { prefixCls: customizePrefixCls, moreIcon = <EllipsisOutlined /> } = props;
|
2020-05-30 18:28:25 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('tabs', customizePrefixCls);
|
|
|
|
|
|
|
|
let editable: EditableConfig | undefined;
|
|
|
|
if (type === 'editable-card') {
|
|
|
|
editable = {
|
|
|
|
onEdit: (editType, { key, event }) => {
|
|
|
|
onEdit?.(editType === 'add' ? event : key!, editType);
|
|
|
|
},
|
|
|
|
removeIcon: <CloseOutlined />,
|
2020-06-28 22:41:59 +08:00
|
|
|
addIcon: addIcon || <PlusOutlined />,
|
2020-05-31 21:57:42 +08:00
|
|
|
showAdd: hideAdd !== true,
|
2020-05-30 18:28:25 +08:00
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
2021-02-08 17:09:13 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-05-30 18:28:25 +08:00
|
|
|
devWarning(
|
|
|
|
!('onPrevClick' in props) && !('onNextClick' in props),
|
|
|
|
'Tabs',
|
|
|
|
'`onPrevClick` and `onNextClick` has been removed. Please use `onTabScroll` instead.',
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2021-03-24 12:23:24 +08:00
|
|
|
<SizeContext.Consumer>
|
|
|
|
{contextSize => {
|
|
|
|
const size = propSize !== undefined ? propSize : contextSize;
|
|
|
|
return (
|
|
|
|
<RcTabs
|
|
|
|
direction={direction}
|
|
|
|
moreTransitionName={`${rootPrefixCls}-slide-up`}
|
|
|
|
{...props}
|
|
|
|
className={classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${size}`]: size,
|
|
|
|
[`${prefixCls}-card`]: ['card', 'editable-card'].includes(type as string),
|
|
|
|
[`${prefixCls}-editable-card`]: type === 'editable-card',
|
|
|
|
[`${prefixCls}-centered`]: centered,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
)}
|
|
|
|
editable={editable}
|
|
|
|
moreIcon={moreIcon}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</SizeContext.Consumer>
|
2020-05-30 18:28:25 +08:00
|
|
|
);
|
|
|
|
}
|
2018-09-06 14:36:40 +08:00
|
|
|
|
2020-05-30 18:28:25 +08:00
|
|
|
Tabs.TabPane = TabPane;
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-05-30 18:28:25 +08:00
|
|
|
export default Tabs;
|