wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<template>
    <view
        class="cl-badge"
        :class="[
            `cl-badge--${type}`,
            {
                'is-dot': isDot,
                'is-plain': plain,
            },
        ]"
        v-if="$slots.default"
    >
        <slot></slot>
        <text
            class="cl-badge__content"
            :style="[
                baseStyle,
                {
                    backgroundColor: color,
                },
            ]"
            v-if="!hidden && ((content && content != 0) || isDot)"
            >{{ content }}</text
        >
    </view>
</template>
 
<script lang="ts">
import { computed, defineComponent } from "vue";
import { type PropType } from "vue";
import { isNumber } from "lodash-es";
import { useStyle } from "../../hooks";
 
export default defineComponent({
    name: "cl-badge",
 
    props: {
        // 文本内容
        value: [String, Number],
        // 最大值
        max: Number,
        // 是否点状
        isDot: Boolean,
        // 是否隐藏
        hidden: Boolean,
        // 类型
        type: {
            type: String as PropType<"primary" | "success" | "warning" | "error">,
            default: "error",
        },
        // 颜色
        color: String,
        // 朴素
        plain: Boolean,
    },
 
    setup(props) {
        const content = computed(() => {
            if (props.isDot) return "";
 
            const value: any = props.value;
            const max: any = props.max;
 
            if (isNumber(value) && isNumber(max) && max > 0) {
                return max < value ? `${max}+` : value;
            }
 
            return value;
        });
 
        return {
            content,
            ...useStyle(),
        };
    },
});
</script>