u-icon.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <view :style="[customStyle]" class="u-icon" @tap="click" :class="[labelPos == 'bottom' ? 'u-flex-col u-row-center' : 'u-flex u-col-center']">
  3. <image class="u-icon__img" v-if="isImg" :src="name" :mode="imgMode" :style="[imgStyle]"></image>
  4. <text v-else class="u-icon__icon" :class="customClass" :style="[iconStyle]" :hover-class="hoverClass" @touchstart="touchstart"></text>
  5. <text v-if="label" class="u-icon__label" :style="{
  6. color: labelColor,
  7. fontSize: labelSize + 'rpx',
  8. marginLeft: labelPos == 'right' ? marginLeft + 'rpx' : 0,
  9. marginTop: labelPos == 'bottom' ? marginTop + 'rpx' : 0,
  10. }">{{label}}</text>
  11. </view>
  12. </template>
  13. <script>
  14. /**
  15. * icon 图标
  16. * @description 基于字体的图标集,包含了大多数常见场景的图标。
  17. * @tutorial https://www.uviewui.com/components/icon.html
  18. * @property {String} name 图标名称,见示例图标集
  19. * @property {String} color 图标颜色(默认inherit)
  20. * @property {String | Number} size 图标字体大小,单位rpx(默认32)
  21. * @property {String | Number} label-size label字体大小,单位rpx(默认28)
  22. * @property {String} label 图标右侧的label文字(默认28)
  23. * @property {String} label-pos label文字相对于图标的位置,只能right或bottom(默认right)
  24. * @property {String} label-color label字体颜色(默认#606266)
  25. * @property {String | Number} margin-left label在右侧时与图标的距离,单位rpx(默认6)
  26. * @property {String | Number} margin-top label在下方时与图标的距离,单位rpx(默认6)
  27. * @property {String} label-pos label相对于图标的位置,只能right或bottom(默认right)
  28. * @property {String} index 一个用于区分多个图标的值,点击图标时通过click事件传出
  29. * @property {String} hover-class 图标按下去的样式类,用法同uni的view组件的hover-class参数,详情见官网
  30. * @event {Function} click 点击图标时触发
  31. * @example <u-icon name="photo" color="#2979ff" size="28"></u-icon>
  32. */
  33. export default {
  34. name: 'u-icon',
  35. props: {
  36. // 图标类名
  37. name: {
  38. type: String,
  39. default: ''
  40. },
  41. // 图标颜色
  42. color: {
  43. type: String,
  44. default: ''
  45. },
  46. // 字体大小,单位rpx
  47. size: {
  48. type: [Number, String],
  49. default: 'inherit'
  50. },
  51. // 是否显示粗体
  52. bold: {
  53. type: Boolean,
  54. default: false
  55. },
  56. // 点击图标的时候传递事件出去的index(用于区分点击了哪一个)
  57. index: {
  58. type: [Number, String],
  59. default: ''
  60. },
  61. // 触摸图标时的类名
  62. hoverClass: {
  63. type: String,
  64. default: ''
  65. },
  66. // 自定义扩展前缀,方便用户扩展自己的图标库
  67. customPrefix: {
  68. type: String,
  69. default: 'uicon'
  70. },
  71. // 图标右边或者下面的文字
  72. label: {
  73. type: String,
  74. default: ''
  75. },
  76. // label的位置,只能右边或者下边
  77. labelPos: {
  78. type: String,
  79. default: 'right'
  80. },
  81. // label的大小
  82. labelSize: {
  83. type: [String, Number],
  84. default: '28'
  85. },
  86. // label的颜色
  87. labelColor: {
  88. type: String,
  89. default: '#606266'
  90. },
  91. // label与图标的距离(横向排列)
  92. marginLeft: {
  93. type: [String, Number],
  94. default: '6'
  95. },
  96. // label与图标的距离(竖向排列)
  97. marginTop: {
  98. type: [String, Number],
  99. default: '6'
  100. },
  101. // 图片的mode
  102. imgMode: {
  103. type: String,
  104. default: 'widthFix'
  105. },
  106. // 自定义样式
  107. customStyle: {
  108. type: Object,
  109. default() {
  110. return {}
  111. }
  112. }
  113. },
  114. data() {
  115. return {};
  116. },
  117. computed: {
  118. customClass() {
  119. let classes = [];
  120. classes.push(this.customPrefix + '-' + this.name);
  121. // uView的自定义图标类名为u-iconfont
  122. if (this.customPrefix == 'uicon') classes.push('u-iconfont');
  123. else classes.push(this.customPrefix);
  124. // 阿里,头条,百度小程序通过数组绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
  125. // 故需将其拆成一个字符串的形式,通过空格隔开各个类名
  126. //#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
  127. classes = classes.join(' ');
  128. //#endif
  129. return classes;
  130. },
  131. iconStyle() {
  132. let style = {};
  133. style = {
  134. fontSize: this.size == 'inherit' ? 'inherit' : this.size + 'rpx',
  135. fontWeight: this.bold ? 'bold' : 'normal'
  136. };
  137. if (this.color) style.color = this.color;
  138. return style;
  139. },
  140. // 判断传入的name属性,是否图片路径,只要带有"/"均认为是图片形式
  141. isImg() {
  142. return this.name.indexOf('/') !== -1;
  143. },
  144. imgStyle() {
  145. let style = {};
  146. style.width = this.size + 'rpx';
  147. return style;
  148. }
  149. },
  150. methods: {
  151. click() {
  152. this.$emit('click', this.index);
  153. },
  154. touchstart() {
  155. this.$emit('touchstart', this.index);
  156. }
  157. }
  158. };
  159. </script>
  160. <style scoped lang="scss">
  161. @import '../../iconfont.css';
  162. .u-icon {
  163. display: inline-flex;
  164. align-items: center;
  165. }
  166. .u-icon__img {
  167. height: auto;
  168. will-change: transform;
  169. }
  170. </style>