ant-design/components/tabs/index.tsx
Karott 338ec7dad7
perf: refactor devWarning for production code size (#35411)
* pref: better code style for production

* refactor `devWarning`

* don't use `useEffect` only wrap `devWarning`

* chore: add 'noop' to coverage

* chore: add test cases for devWarning

* chore: add test case

* chore: update test cases for devWarning

* chore: restore test script command

* fix: remove 'throw new Error'

* should not use `throw` for browser

* chore: update test case for AutoComplete

* perf: add prefix for `devWarning`

* update RegExp for UMD

* add prefix for ES and CJS

* chore: better code style

* perf:

* upgrade antd-tools

* remove `injectWarningCondition`

* rename `devWarning` to `warning`

* chore: better code style

* chore: better code style

* chore: restore hasValidName
2022-05-10 15:43:29 +08:00

93 lines
2.8 KiB
TypeScript
Executable File

import * as React from 'react';
import type { TabsProps as RcTabsProps } from 'rc-tabs';
import RcTabs, { TabPane, TabPaneProps } from 'rc-tabs';
import type { EditableConfig } from 'rc-tabs/lib/interface';
import classNames from 'classnames';
import EllipsisOutlined from '@ant-design/icons/EllipsisOutlined';
import PlusOutlined from '@ant-design/icons/PlusOutlined';
import CloseOutlined from '@ant-design/icons/CloseOutlined';
import warning from '../_util/warning';
import { ConfigContext } from '../config-provider';
import type { SizeType } from '../config-provider/SizeContext';
import SizeContext from '../config-provider/SizeContext';
export type TabsType = 'line' | 'card' | 'editable-card';
export type TabsPosition = 'top' | 'right' | 'bottom' | 'left';
export { TabPaneProps };
export interface TabsProps extends Omit<RcTabsProps, 'editable'> {
type?: TabsType;
size?: SizeType;
hideAdd?: boolean;
centered?: boolean;
addIcon?: React.ReactNode;
onEdit?: (e: React.MouseEvent | React.KeyboardEvent | string, action: 'add' | 'remove') => void;
}
function Tabs({
type,
className,
size: propSize,
onEdit,
hideAdd,
centered,
addIcon,
...props
}: TabsProps) {
const { prefixCls: customizePrefixCls, moreIcon = <EllipsisOutlined /> } = props;
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 />,
addIcon: addIcon || <PlusOutlined />,
showAdd: hideAdd !== true,
};
}
const rootPrefixCls = getPrefixCls();
warning(
!('onPrevClick' in props) && !('onNextClick' in props),
'Tabs',
'`onPrevClick` and `onNextClick` has been removed. Please use `onTabScroll` instead.',
);
return (
<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>
);
}
Tabs.TabPane = TabPane;
export default Tabs;