Novecane
2025-02-15 a12e5f63a90446525b2f322fb2bbd21d5285cd6d
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<template>
    <!-- #ifdef APP-NVUE -->
    <text :style="styleObj" class="uni-icons" @click="_onClick">{{unicode}}</text>
    <!-- #endif -->
    <!-- #ifndef APP-NVUE -->
    <text :style="styleObj" class="uni-icons" :class="['uniui-'+type,customPrefix,customPrefix?type:'']" @click="_onClick">
        <slot></slot>
    </text>
    <!-- #endif -->
</template>
 
<script>
    import { fontData } from './uniicons_file_vue.js';
 
    const getVal = (val) => {
        const reg = /^[0-9]*$/g
        return (typeof val === 'number' || reg.test(val)) ? val + 'px' : val;
    }
 
    // #ifdef APP-NVUE
    var domModule = weex.requireModule('dom');
    import iconUrl from './uniicons.ttf'
    domModule.addRule('fontFace', {
        'fontFamily': "uniicons",
        'src': "url('" + iconUrl + "')"
    });
    // #endif
 
    /**
     * Icons 图标
     * @description 用于展示 icons 图标
     * @tutorial https://ext.dcloud.net.cn/plugin?id=28
     * @property {Number} size 图标大小
     * @property {String} type 图标图案,参考示例
     * @property {String} color 图标颜色
     * @property {String} customPrefix 自定义图标
     * @event {Function} click 点击 Icon 触发事件
     */
    export default {
        name: 'UniIcons',
        emits: ['click'],
        props: {
            type: {
                type: String,
                default: ''
            },
            color: {
                type: String,
                default: '#333333'
            },
            size: {
                type: [Number, String],
                default: 16
            },
            customPrefix: {
                type: String,
                default: ''
            },
            fontFamily: {
                type: String,
                default: ''
            }
        },
        data() {
            return {
                icons: fontData
            }
        },
        computed: {
            unicode() {
                let code = this.icons.find(v => v.font_class === this.type)
                if (code) {
                    return code.unicode
                }
                return ''
            },
            iconSize() {
                return getVal(this.size)
            },
            styleObj() {
                if (this.fontFamily !== '') {
                    return `color: ${this.color}; font-size: ${this.iconSize}; font-family: ${this.fontFamily};`
                }
                return `color: ${this.color}; font-size: ${this.iconSize};`
            }
        },
        methods: {
            _onClick() {
                this.$emit('click')
            }
        }
    }
</script>
 
<style lang="scss">
    /* #ifndef APP-NVUE */
    @import './uniicons.css';
 
    @font-face {
        font-family: uniicons;
        src: url('./uniicons.ttf');
    }
 
    /* #endif */
    .uni-icons {
        font-family: uniicons;
        text-decoration: none;
        text-align: center;
    }
</style>