vant/src/tag/Tag.tsx
2021-03-08 11:50:37 +08:00

81 lines
1.7 KiB
TypeScript

import { CSSProperties, PropType, Transition, defineComponent } from 'vue';
import { createNamespace } from '../utils';
import Icon from '../icon';
const [name, bem] = createNamespace('tag');
export type TagType = 'default' | 'primary' | 'success' | 'warning' | 'danger';
export default defineComponent({
name,
props: {
size: String,
mark: Boolean,
color: String,
plain: Boolean,
round: Boolean,
textColor: String,
closeable: Boolean,
type: {
type: String as PropType<TagType>,
default: 'default',
},
show: {
type: Boolean,
default: true,
},
},
emits: ['close'],
setup(props, { slots, emit }) {
const onClose = (event: MouseEvent) => {
event.stopPropagation();
emit('close');
};
const getStyle = (): CSSProperties => {
if (props.plain) {
return {
color: props.textColor || props.color,
};
}
return {
color: props.textColor,
background: props.color,
};
};
const renderTag = () => {
const { type, mark, plain, round, size, closeable } = props;
const classes: Record<string, unknown> = {
mark,
plain,
round,
};
if (size) {
classes[size] = size;
}
const CloseIcon = closeable && (
<Icon name="cross" class={bem('close')} onClick={onClose} />
);
return (
<span style={getStyle()} class={bem([classes, type])}>
{slots.default?.()}
{CloseIcon}
</span>
);
};
return () => (
<Transition name={props.closeable ? 'van-fade' : undefined}>
{props.show ? renderTag() : null}
</Transition>
);
},
});