2022-06-02 20:22:42 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
|
|
|
import EllipsisOutlined from '@ant-design/icons/EllipsisOutlined';
|
|
|
|
import PlusOutlined from '@ant-design/icons/PlusOutlined';
|
|
|
|
import classNames from 'classnames';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { TabsProps as RcTabsProps } from 'rc-tabs';
|
2022-08-05 10:49:08 +08:00
|
|
|
import RcTabs from 'rc-tabs';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { EditableConfig } from 'rc-tabs/lib/interface';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-05-12 14:53:47 +08:00
|
|
|
import warning from '../_util/warning';
|
2020-05-30 18:28:25 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
2023-05-12 14:53:47 +08:00
|
|
|
import useSize from '../config-provider/hooks/useSize';
|
|
|
|
import TabPane, { type TabPaneProps } from './TabPane';
|
2022-08-08 17:01:25 +08:00
|
|
|
import useAnimateConfig from './hooks/useAnimateConfig';
|
2022-08-05 10:49:08 +08:00
|
|
|
import useLegacyItems from './hooks/useLegacyItems';
|
2022-06-02 20:22:42 +08:00
|
|
|
import useStyle from './style';
|
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
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
export type { TabPaneProps };
|
2020-06-01 16:04:13 +08:00
|
|
|
|
2020-05-30 18:28:25 +08:00
|
|
|
export interface TabsProps extends Omit<RcTabsProps, 'editable'> {
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
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;
|
2022-08-05 10:49:08 +08:00
|
|
|
children?: React.ReactNode;
|
2016-07-09 10:50:51 +08:00
|
|
|
}
|
|
|
|
|
2023-07-03 13:38:38 +08:00
|
|
|
const Tabs: React.FC<TabsProps> & { TabPane: typeof TabPane } = (props) => {
|
|
|
|
const {
|
|
|
|
type,
|
|
|
|
className,
|
|
|
|
rootClassName,
|
|
|
|
size: customSize,
|
|
|
|
onEdit,
|
|
|
|
hideAdd,
|
|
|
|
centered,
|
|
|
|
addIcon,
|
|
|
|
popupClassName,
|
|
|
|
children,
|
|
|
|
items,
|
|
|
|
animated,
|
|
|
|
style,
|
|
|
|
...otherProps
|
|
|
|
} = props;
|
|
|
|
const { prefixCls: customizePrefixCls, moreIcon = <EllipsisOutlined /> } = otherProps;
|
|
|
|
const { direction, tabs, getPrefixCls, getPopupContainer } = React.useContext(ConfigContext);
|
2020-05-30 18:28:25 +08:00
|
|
|
const prefixCls = getPrefixCls('tabs', customizePrefixCls);
|
2022-04-07 22:07:18 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2020-05-30 18:28:25 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
2020-05-30 18:28:25 +08:00
|
|
|
!('onPrevClick' in props) && !('onNextClick' in props),
|
|
|
|
'Tabs',
|
|
|
|
'`onPrevClick` and `onNextClick` has been removed. Please use `onTabScroll` instead.',
|
|
|
|
);
|
|
|
|
|
2022-08-05 10:49:08 +08:00
|
|
|
const mergedItems = useLegacyItems(items, children);
|
|
|
|
|
2022-08-08 17:01:25 +08:00
|
|
|
const mergedAnimated = useAnimateConfig(prefixCls, animated);
|
|
|
|
|
2023-05-12 14:53:47 +08:00
|
|
|
const size = useSize(customSize);
|
2023-01-01 21:17:05 +08:00
|
|
|
|
2023-07-03 13:38:38 +08:00
|
|
|
const mergedStyle: React.CSSProperties = { ...tabs?.style, ...style };
|
|
|
|
|
2022-04-07 22:07:18 +08:00
|
|
|
return wrapSSR(
|
2023-01-01 21:17:05 +08:00
|
|
|
<RcTabs
|
|
|
|
direction={direction}
|
|
|
|
getPopupContainer={getPopupContainer}
|
|
|
|
moreTransitionName={`${rootPrefixCls}-slide-up`}
|
2023-07-03 13:38:38 +08:00
|
|
|
{...otherProps}
|
2023-01-01 21:17:05 +08:00
|
|
|
items={mergedItems}
|
|
|
|
className={classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${size}`]: size,
|
2023-07-03 13:38:38 +08:00
|
|
|
[`${prefixCls}-card`]: ['card', 'editable-card'].includes(type!),
|
2023-01-01 21:17:05 +08:00
|
|
|
[`${prefixCls}-editable-card`]: type === 'editable-card',
|
|
|
|
[`${prefixCls}-centered`]: centered,
|
|
|
|
},
|
2023-07-03 13:38:38 +08:00
|
|
|
tabs?.className,
|
2023-01-01 21:17:05 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2023-01-01 21:17:05 +08:00
|
|
|
hashId,
|
|
|
|
)}
|
|
|
|
popupClassName={classNames(popupClassName, hashId)}
|
2023-07-03 13:38:38 +08:00
|
|
|
style={mergedStyle}
|
2023-01-01 21:17:05 +08:00
|
|
|
editable={editable}
|
|
|
|
moreIcon={moreIcon}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
animated={mergedAnimated}
|
|
|
|
/>,
|
2020-05-30 18:28:25 +08:00
|
|
|
);
|
2023-07-03 13:38:38 +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
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Tabs.displayName = 'Tabs';
|
|
|
|
}
|
|
|
|
|
2020-05-30 18:28:25 +08:00
|
|
|
export default Tabs;
|