ant-design/components/tabs/TabBar.tsx
二货机器人 2afab58ac8
chore: small bundle size follow up (#24164)
* update config

* replace react function

* fix alert
2020-05-14 20:54:49 +08:00

92 lines
2.6 KiB
TypeScript

import * as React from 'react';
import ScrollableInkTabBar from 'rc-tabs/lib/ScrollableInkTabBar';
import classNames from 'classnames';
import UpOutlined from '@ant-design/icons/UpOutlined';
import LeftOutlined from '@ant-design/icons/LeftOutlined';
import DownOutlined from '@ant-design/icons/DownOutlined';
import RightOutlined from '@ant-design/icons/RightOutlined';
import { TabsProps } from './index';
import { ConfigConsumerProps, ConfigConsumer } from '../config-provider';
import { cloneElement } from '../_util/reactNode';
export default class TabBar extends React.Component<TabsProps> {
static defaultProps = {
animated: true,
type: 'line' as TabsProps['type'],
};
renderTabBar = ({ direction }: ConfigConsumerProps) => {
const {
tabBarStyle,
animated,
renderTabBar,
tabBarExtraContent,
tabPosition,
prefixCls,
className,
size,
type,
} = this.props;
const inkBarAnimated = typeof animated === 'object' ? animated.inkBar : animated;
const isVertical = tabPosition === 'left' || tabPosition === 'right';
let prevIconComponent = isVertical ? UpOutlined : LeftOutlined;
let nextIconComponent = isVertical ? DownOutlined : RightOutlined;
if (direction === 'rtl' && !isVertical) {
prevIconComponent = RightOutlined;
nextIconComponent = LeftOutlined;
}
const prevIcon = (
<span className={`${prefixCls}-tab-prev-icon`}>
{React.createElement(prevIconComponent, {
className: `${prefixCls}-tab-prev-icon-target`,
})}
</span>
);
const nextIcon = (
<span className={`${prefixCls}-tab-next-icon`}>
{React.createElement(nextIconComponent, {
className: `${prefixCls}-tab-next-icon-target`,
})}
</span>
);
// Additional className for style usage
const cls: string = classNames(
`${prefixCls}-${tabPosition}-bar`,
{
[`${prefixCls}-${size}-bar`]: !!size,
[`${prefixCls}-card-bar`]: type && type.indexOf('card') >= 0,
},
className,
);
const renderProps = {
...this.props,
children: null,
inkBarAnimated,
extraContent: tabBarExtraContent,
style: tabBarStyle,
prevIcon,
nextIcon,
className: cls,
};
let RenderTabBar: React.ReactElement<any>;
if (renderTabBar) {
RenderTabBar = renderTabBar(renderProps, ScrollableInkTabBar);
} else {
RenderTabBar = <ScrollableInkTabBar {...renderProps} />;
}
return cloneElement(RenderTabBar);
};
render() {
return <ConfigConsumer>{this.renderTabBar}</ConfigConsumer>;
}
}