2020-07-08 14:47:07 +08:00
|
|
|
import classNames from 'classnames';
|
2022-06-22 14:57:09 +08:00
|
|
|
import * as React from 'react';
|
2020-07-08 14:47:07 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { PresetColorType } from '../_util/colors';
|
|
|
|
import type { LiteralUnion } from '../_util/type';
|
2020-07-08 14:47:07 +08:00
|
|
|
import { isPresetColor } from './utils';
|
|
|
|
|
|
|
|
type RibbonPlacement = 'start' | 'end';
|
|
|
|
|
|
|
|
export interface RibbonProps {
|
|
|
|
className?: string;
|
|
|
|
prefixCls?: string;
|
|
|
|
style?: React.CSSProperties; // style of ribbon element, not the wrapper
|
|
|
|
text?: React.ReactNode;
|
|
|
|
color?: LiteralUnion<PresetColorType, string>;
|
|
|
|
children?: React.ReactNode;
|
|
|
|
placement?: RibbonPlacement;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Ribbon: React.FC<RibbonProps> = function Ribbon({
|
|
|
|
className,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
style,
|
|
|
|
color,
|
|
|
|
children,
|
|
|
|
text,
|
|
|
|
placement = 'end',
|
|
|
|
}) {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('ribbon', customizePrefixCls);
|
|
|
|
const colorInPreset = isPresetColor(color);
|
2020-09-06 13:07:39 +08:00
|
|
|
const ribbonCls = classNames(
|
|
|
|
prefixCls,
|
|
|
|
`${prefixCls}-placement-${placement}`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-color-${color}`]: colorInPreset,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2020-07-08 14:47:07 +08:00
|
|
|
const colorStyle: React.CSSProperties = {};
|
|
|
|
const cornerColorStyle: React.CSSProperties = {};
|
|
|
|
if (color && !colorInPreset) {
|
|
|
|
colorStyle.background = color;
|
|
|
|
cornerColorStyle.color = color;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div className={`${prefixCls}-wrapper`}>
|
|
|
|
{children}
|
|
|
|
<div className={ribbonCls} style={{ ...colorStyle, ...style }}>
|
2020-10-14 16:35:29 +08:00
|
|
|
<span className={`${prefixCls}-text`}>{text}</span>
|
2020-07-08 14:47:07 +08:00
|
|
|
<div className={`${prefixCls}-corner`} style={cornerColorStyle} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Ribbon;
|