bug
jiangping
2023-11-07 64b432916af9c9218ab3f3eca614e26c542142ae
minipro_standard/uni_modules/uview-ui/components/u-icon/u-icon.vue
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,234 @@
<template>
   <view
       class="u-icon"
       @tap="clickHandler"
       :class="['u-icon--' + labelPos]"
   >
      <image
          class="u-icon__img"
          v-if="isImg"
          :src="name"
          :mode="imgMode"
          :style="[imgStyle, $u.addStyle(customStyle)]"
      ></image>
      <text
          v-else
          class="u-icon__icon"
          :class="uClasses"
          :style="[iconStyle, $u.addStyle(customStyle)]"
          :hover-class="hoverClass"
      >{{icon}}</text>
      <!-- è¿™é‡Œè¿›è¡Œç©ºå­—符串判断,如果仅仅是v-if="label",可能会出现传递0的时候,结果也无法显示 -->
      <text
          v-if="label !== ''"
          class="u-icon__label"
          :style="{
         color: labelColor,
         fontSize: $u.addUnit(labelSize),
         marginLeft: labelPos == 'right' ? $u.addUnit(space) : 0,
         marginTop: labelPos == 'bottom' ? $u.addUnit(space) : 0,
         marginRight: labelPos == 'left' ? $u.addUnit(space) : 0,
         marginBottom: labelPos == 'top' ? $u.addUnit(space) : 0,
      }"
      >{{ label }}</text>
   </view>
</template>
<script>
   // #ifdef APP-NVUE
   // nvue通过weex的dom模块引入字体,相关文档地址如下:
   // https://weex.apache.org/zh/docs/modules/dom.html#addrule
   const fontUrl = 'https://at.alicdn.com/t/font_2225171_8kdcwk4po24.ttf'
   const domModule = weex.requireModule('dom')
   domModule.addRule('fontFace', {
      'fontFamily': "uicon-iconfont",
      'src': `url('${fontUrl}')`
   })
   // #endif
   // å¼•入图标名称,已经对应的unicode
   import icons from './icons'
   import props from './props.js';;
   /**
    * icon å›¾æ ‡
    * @description åŸºäºŽå­—体的图标集,包含了大多数常见场景的图标。
    * @tutorial https://www.uviewui.com/components/icon.html
    * @property {String}         name         å›¾æ ‡åç§°ï¼Œè§ç¤ºä¾‹å›¾æ ‡é›†
    * @property {String}         color         å›¾æ ‡é¢œè‰²,可接受主题色 ï¼ˆé»˜è®¤ color['u-content-color'] ï¼‰
    * @property {String | Number}   size         å›¾æ ‡å­—体大小,单位px ï¼ˆé»˜è®¤ '16px' ï¼‰
    * @property {Boolean}         bold         æ˜¯å¦æ˜¾ç¤ºç²—体 ï¼ˆé»˜è®¤ false ï¼‰
    * @property {String | Number}   index         ç‚¹å‡»å›¾æ ‡çš„æ—¶å€™ä¼ é€’事件出去的index(用于区分点击了哪一个)
    * @property {String}         hoverClass      å›¾æ ‡æŒ‰ä¸‹åŽ»çš„æ ·å¼ç±»ï¼Œç”¨æ³•åŒuni的view组件的hoverClass参数,详情见官网
    * @property {String}         customPrefix   è‡ªå®šä¹‰æ‰©å±•前缀,方便用户扩展自己的图标库 ï¼ˆé»˜è®¤ 'uicon' ï¼‰
    * @property {String | Number}   label         å›¾æ ‡å³ä¾§çš„label文字
    * @property {String}         labelPos      label相对于图标的位置,只能right或bottom ï¼ˆé»˜è®¤ 'right' ï¼‰
    * @property {String | Number}   labelSize      label字体大小,单位px ï¼ˆé»˜è®¤ '15px' ï¼‰
    * @property {String}         labelColor      å›¾æ ‡å³ä¾§çš„label文字颜色 ï¼ˆ é»˜è®¤ color['u-content-color'] ï¼‰
    * @property {String | Number}   space         label与图标的距离,单位px ï¼ˆé»˜è®¤ '3px' ï¼‰
    * @property {String}         imgMode         å›¾ç‰‡çš„mode
    * @property {String | Number}   width         æ˜¾ç¤ºå›¾ç‰‡å°å›¾æ ‡æ—¶çš„宽度
    * @property {String | Number}   height         æ˜¾ç¤ºå›¾ç‰‡å°å›¾æ ‡æ—¶çš„高度
    * @property {String | Number}   top            å›¾æ ‡åœ¨åž‚直方向上的定位 ç”¨äºŽè§£å†³æŸäº›æƒ…况下,让图标垂直居中的用途  ï¼ˆé»˜è®¤ 0 ï¼‰
    * @property {Boolean}         stop         æ˜¯å¦é˜»æ­¢äº‹ä»¶ä¼ æ’­ ï¼ˆé»˜è®¤ false ï¼‰
    * @property {Object}         customStyle      icon的样式,对象形式
    * @event {Function} click ç‚¹å‡»å›¾æ ‡æ—¶è§¦å‘
    * @event {Function} touchstart äº‹ä»¶è§¦æ‘¸æ—¶è§¦å‘
    * @example <u-icon name="photo" color="#2979ff" size="28"></u-icon>
    */
   export default {
      name: 'u-icon',
      data() {
         return {
         }
      },
      mixins: [uni.$u.mpMixin, uni.$u.mixin,props],
      computed: {
         uClasses() {
            let classes = []
            classes.push(this.customPrefix + '-' + this.name)
            // // uView的自定义图标类名为u-iconfont
            // if (this.customPrefix == 'uicon') {
            //    classes.push('u-iconfont')
            // } else {
            //    classes.push(this.customPrefix)
            // }
            // ä¸»é¢˜è‰²ï¼Œé€šè¿‡ç±»é…ç½®
            if (this.color && uni.$u.config.type.includes(this.color)) classes.push('u-icon__icon--' + this.color)
            // é˜¿é‡Œï¼Œå¤´æ¡ï¼Œç™¾åº¦å°ç¨‹åºé€šè¿‡æ•°ç»„绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
            // æ•…需将其拆成一个字符串的形式,通过空格隔开各个类名
            //#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
            classes = classes.join(' ')
            //#endif
            return classes
         },
         iconStyle() {
            let style = {}
            style = {
               fontSize: uni.$u.addUnit(this.size),
               lineHeight: uni.$u.addUnit(this.size),
               fontWeight: this.bold ? 'bold' : 'normal',
               // æŸäº›ç‰¹æ®Šæƒ…况需要设置一个到顶部的距离,才能更好的垂直居中
               top: uni.$u.addUnit(this.top)
            }
            // éžä¸»é¢˜è‰²å€¼æ—¶ï¼Œæ‰å½“作颜色值
            if (this.color && !uni.$u.config.type.includes(this.color)) style.color = this.color
            return style
         },
         // åˆ¤æ–­ä¼ å…¥çš„name属性,是否图片路径,只要带有"/"均认为是图片形式
         isImg() {
            return this.name.indexOf('/') !== -1
         },
         imgStyle() {
            let style = {}
            // å¦‚果设置width和height属性,则优先使用,否则使用size属性
            style.width = this.width ? uni.$u.addUnit(this.width) : uni.$u.addUnit(this.size)
            style.height = this.height ? uni.$u.addUnit(this.height) : uni.$u.addUnit(this.size)
            return style
         },
         // é€šè¿‡å›¾æ ‡åï¼ŒæŸ¥æ‰¾å¯¹åº”的图标
         icon() {
            // å¦‚果内置的图标中找不到对应的图标,就直接返回name值,因为用户可能传入的是unicode代码
            return icons['uicon-' + this.name] || this.name
         }
      },
      methods: {
         clickHandler(e) {
            this.$emit('click', this.index)
            // æ˜¯å¦é˜»æ­¢äº‹ä»¶å†’泡
            this.stop && this.preventEvent(e)
         }
      }
   }
</script>
<style lang="scss" scoped>
   @import "../../libs/css/components.scss";
   // å˜é‡å®šä¹‰
   $u-icon-primary: $u-primary !default;
   $u-icon-success: $u-success !default;
   $u-icon-info: $u-info !default;
   $u-icon-warning: $u-warning !default;
   $u-icon-error: $u-error !default;
   $u-icon-label-line-height:1 !default;
   /* #ifndef APP-NVUE */
   // éžnvue下加载字体
   @font-face {
      font-family: 'uicon-iconfont';
      src: url('https://at.alicdn.com/t/font_2225171_8kdcwk4po24.ttf') format('truetype');
   }
   /* #endif */
   .u-icon {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      align-items: center;
      &--left {
         flex-direction: row-reverse;
         align-items: center;
      }
      &--right {
         flex-direction: row;
         align-items: center;
      }
      &--top {
         flex-direction: column-reverse;
         justify-content: center;
      }
      &--bottom {
         flex-direction: column;
         justify-content: center;
      }
      &__icon {
         font-family: uicon-iconfont;
         position: relative;
         @include flex;
         align-items: center;
         &--primary {
            color: $u-icon-primary;
         }
         &--success {
            color: $u-icon-success;
         }
         &--error {
            color: $u-icon-error;
         }
         &--warning {
            color: $u-icon-warning;
         }
         &--info {
            color: $u-icon-info;
         }
      }
      &__img {
         /* #ifndef APP-NVUE */
         height: auto;
         will-change: transform;
         /* #endif */
      }
      &__label {
         /* #ifndef APP-NVUE */
         line-height: $u-icon-label-line-height;
         /* #endif */
      }
   }
</style>