|
| 1 | +import { LiteralUnion, tuple } from '../_util/type'; |
| 2 | +import { PresetColorType } from '../_util/colors'; |
| 3 | +import { isPresetColor } from './utils'; |
| 4 | +import { defaultConfigProvider } from '../config-provider'; |
| 5 | +import { HTMLAttributes, FunctionalComponent, VNodeTypes, inject, CSSProperties } from 'vue'; |
| 6 | +import PropTypes from '../_util/vue-types'; |
| 7 | + |
| 8 | +type RibbonPlacement = 'start' | 'end'; |
| 9 | + |
| 10 | +export interface RibbonProps extends HTMLAttributes { |
| 11 | + prefixCls?: string; |
| 12 | + text?: VNodeTypes; |
| 13 | + color?: LiteralUnion<PresetColorType, string>; |
| 14 | + placement?: RibbonPlacement; |
| 15 | +} |
| 16 | + |
| 17 | +const Ribbon: FunctionalComponent<RibbonProps> = (props, { attrs, slots }) => { |
| 18 | + const { prefixCls: customizePrefixCls, color, text = slots.text?.(), placement = 'end' } = props; |
| 19 | + const { class: className, style } = attrs; |
| 20 | + const children = slots.default?.(); |
| 21 | + const { getPrefixCls, direction } = inject('configProvider', defaultConfigProvider); |
| 22 | + |
| 23 | + const prefixCls = getPrefixCls('ribbon', customizePrefixCls); |
| 24 | + const colorInPreset = isPresetColor(color); |
| 25 | + const ribbonCls = [ |
| 26 | + prefixCls, |
| 27 | + `${prefixCls}-placement-${placement}`, |
| 28 | + { |
| 29 | + [`${prefixCls}-rtl`]: direction === 'rtl', |
| 30 | + [`${prefixCls}-color-${color}`]: colorInPreset, |
| 31 | + }, |
| 32 | + className, |
| 33 | + ]; |
| 34 | + const colorStyle: CSSProperties = {}; |
| 35 | + const cornerColorStyle: CSSProperties = {}; |
| 36 | + if (color && !colorInPreset) { |
| 37 | + colorStyle.background = color; |
| 38 | + cornerColorStyle.color = color; |
| 39 | + } |
| 40 | + return ( |
| 41 | + <div class={`${prefixCls}-wrapper`}> |
| 42 | + {children} |
| 43 | + <div class={ribbonCls} style={{ ...colorStyle, ...(style as CSSProperties) }}> |
| 44 | + <span class={`${prefixCls}-text`}>{text}</span> |
| 45 | + <div class={`${prefixCls}-corner`} style={cornerColorStyle} /> |
| 46 | + </div> |
| 47 | + </div> |
| 48 | + ); |
| 49 | +}; |
| 50 | + |
| 51 | +Ribbon.displayName = 'ABadgeRibbon'; |
| 52 | +Ribbon.inheritAttrs = false; |
| 53 | +Ribbon.props = { |
| 54 | + prefix: PropTypes.string, |
| 55 | + color: PropTypes.string, |
| 56 | + text: PropTypes.any, |
| 57 | + placement: PropTypes.oneOf(tuple('start', 'end')), |
| 58 | +}; |
| 59 | + |
| 60 | +export default Ribbon; |
0 commit comments