2021-06-26 01:35:40 +00:00
|
|
|
import type { LiteralUnion } from '../_util/type';
|
|
|
|
import { tuple } from '../_util/type';
|
|
|
|
import type { PresetColorType } from '../_util/colors';
|
2021-02-25 05:39:39 +00:00
|
|
|
import { isPresetColor } from './utils';
|
2021-06-26 01:35:40 +00:00
|
|
|
import type { CSSProperties, PropType, ExtractPropTypes } from 'vue';
|
|
|
|
import { defineComponent, computed } from 'vue';
|
2021-02-25 05:39:39 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2021-06-07 09:35:03 +00:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
2021-02-25 05:39:39 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const ribbonProps = {
|
2021-02-25 05:39:39 +00:00
|
|
|
prefix: PropTypes.string,
|
2021-06-07 09:35:03 +00:00
|
|
|
color: { type: String as PropType<LiteralUnion<PresetColorType, string>> },
|
2021-02-25 05:39:39 +00:00
|
|
|
text: PropTypes.any,
|
2021-06-07 09:35:03 +00:00
|
|
|
placement: PropTypes.oneOf(tuple('start', 'end')).def('end'),
|
2021-02-25 05:39:39 +00:00
|
|
|
};
|
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
export type RibbonProps = Partial<ExtractPropTypes<typeof ribbonProps>>;
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
name: 'ABadgeRibbon',
|
|
|
|
inheritAttrs: false,
|
|
|
|
props: ribbonProps,
|
|
|
|
slots: ['text'],
|
|
|
|
setup(props, { attrs, slots }) {
|
|
|
|
const { prefixCls, direction } = useConfigInject('ribbon', props);
|
|
|
|
const colorInPreset = computed(() => isPresetColor(props.color));
|
|
|
|
const ribbonCls = computed(() => [
|
|
|
|
prefixCls.value,
|
|
|
|
`${prefixCls.value}-placement-${props.placement}`,
|
|
|
|
{
|
|
|
|
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
|
|
|
|
[`${prefixCls.value}-color-${props.color}`]: colorInPreset.value,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
return () => {
|
|
|
|
const { class: className, style, ...restAttrs } = attrs;
|
|
|
|
const colorStyle: CSSProperties = {};
|
|
|
|
const cornerColorStyle: CSSProperties = {};
|
|
|
|
if (props.color && !colorInPreset.value) {
|
|
|
|
colorStyle.background = props.color;
|
|
|
|
cornerColorStyle.color = props.color;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div class={`${prefixCls.value}-wrapper`} {...restAttrs}>
|
|
|
|
{slots.default?.()}
|
|
|
|
<div
|
|
|
|
class={[ribbonCls.value, className]}
|
|
|
|
style={{ ...colorStyle, ...(style as CSSProperties) }}
|
|
|
|
>
|
|
|
|
<span class={`${prefixCls.value}-text`}>{props.text || slots.text?.()}</span>
|
|
|
|
<div class={`${prefixCls.value}-corner`} style={cornerColorStyle} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|