u-icon.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <view :style="[customStyle]" class="u-icon" @tap="click" :class="['u-icon--' + labelPos]">
  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. <!-- 这里进行空字符串判断,如果仅仅是v-if="label",可能会出现传递0的时候,结果也无法显示 -->
  6. <text v-if="label !== ''" class="u-icon__label" :style="{
  7. color: labelColor,
  8. fontSize: $u.addUnit(labelSize),
  9. marginLeft: labelPos == 'right' ? $u.addUnit(marginLeft) : 0,
  10. marginTop: labelPos == 'bottom' ? $u.addUnit(marginTop) : 0,
  11. marginRight: labelPos == 'left' ? $u.addUnit(marginRight) : 0,
  12. marginBottom: labelPos == 'top' ? $u.addUnit(marginBottom) : 0,
  13. }">{{label}}</text>
  14. </view>
  15. </template>
  16. <script>
  17. /**
  18. * icon 图标
  19. * @description 基于字体的图标集,包含了大多数常见场景的图标。
  20. * @tutorial https://www.uviewui.com/components/icon.html
  21. * @property {String} name 图标名称,见示例图标集
  22. * @property {String} color 图标颜色(默认inherit)
  23. * @property {String | Number} size 图标字体大小,单位rpx(默认32)
  24. * @property {String | Number} label-size label字体大小,单位rpx(默认28)
  25. * @property {String} label 图标右侧的label文字(默认28)
  26. * @property {String} label-pos label文字相对于图标的位置,只能right或bottom(默认right)
  27. * @property {String} label-color label字体颜色(默认#606266)
  28. * @property {Object} custom-style icon的样式,对象形式
  29. * @property {String} custom-prefix 自定义字体图标库时,需要写上此值
  30. * @property {String | Number} margin-left label在右侧时与图标的距离,单位rpx(默认6)
  31. * @property {String | Number} margin-top label在下方时与图标的距离,单位rpx(默认6)
  32. * @property {String | Number} margin-bottom label在上方时与图标的距离,单位rpx(默认6)
  33. * @property {String | Number} margin-right label在左侧时与图标的距离,单位rpx(默认6)
  34. * @property {String} label-pos label相对于图标的位置,只能right或bottom(默认right)
  35. * @property {String} index 一个用于区分多个图标的值,点击图标时通过click事件传出
  36. * @property {String} hover-class 图标按下去的样式类,用法同uni的view组件的hover-class参数,详情见官网
  37. * @property {String} width 显示图片小图标时的宽度
  38. * @property {String} height 显示图片小图标时的高度
  39. * @property {String} top 图标在垂直方向上的定位
  40. * @event {Function} click 点击图标时触发
  41. * @example <u-icon name="photo" color="#2979ff" size="28"></u-icon>
  42. */
  43. export default {
  44. name: 'u-icon',
  45. props: {
  46. // 图标类名
  47. name: {
  48. type: String,
  49. default: ''
  50. },
  51. // 图标颜色,可接受主题色
  52. color: {
  53. type: String,
  54. default: ''
  55. },
  56. // 字体大小,单位rpx
  57. size: {
  58. type: [Number, String],
  59. default: 'inherit'
  60. },
  61. // 是否显示粗体
  62. bold: {
  63. type: Boolean,
  64. default: false
  65. },
  66. // 点击图标的时候传递事件出去的index(用于区分点击了哪一个)
  67. index: {
  68. type: [Number, String],
  69. default: ''
  70. },
  71. // 触摸图标时的类名
  72. hoverClass: {
  73. type: String,
  74. default: ''
  75. },
  76. // 自定义扩展前缀,方便用户扩展自己的图标库
  77. customPrefix: {
  78. type: String,
  79. default: 'uicon'
  80. },
  81. // 图标右边或者下面的文字
  82. label: {
  83. type: [String, Number],
  84. default: ''
  85. },
  86. // label的位置,只能右边或者下边
  87. labelPos: {
  88. type: String,
  89. default: 'right'
  90. },
  91. // label的大小
  92. labelSize: {
  93. type: [String, Number],
  94. default: '28'
  95. },
  96. // label的颜色
  97. labelColor: {
  98. type: String,
  99. default: '#606266'
  100. },
  101. // label与图标的距离(横向排列)
  102. marginLeft: {
  103. type: [String, Number],
  104. default: '6'
  105. },
  106. // label与图标的距离(竖向排列)
  107. marginTop: {
  108. type: [String, Number],
  109. default: '6'
  110. },
  111. // label与图标的距离(竖向排列)
  112. marginRight: {
  113. type: [String, Number],
  114. default: '6'
  115. },
  116. // label与图标的距离(竖向排列)
  117. marginBottom: {
  118. type: [String, Number],
  119. default: '6'
  120. },
  121. // 图片的mode
  122. imgMode: {
  123. type: String,
  124. default: 'widthFix'
  125. },
  126. // 自定义样式
  127. customStyle: {
  128. type: Object,
  129. default() {
  130. return {}
  131. }
  132. },
  133. // 用于显示图片小图标时,图片的宽度
  134. width: {
  135. type: [String, Number],
  136. default: ''
  137. },
  138. // 用于显示图片小图标时,图片的高度
  139. height: {
  140. type: [String, Number],
  141. default: ''
  142. },
  143. // 用于解决某些情况下,让图标垂直居中的用途
  144. top: {
  145. type: [String, Number],
  146. default: 0
  147. }
  148. },
  149. computed: {
  150. customClass() {
  151. let classes = [];
  152. classes.push(this.customPrefix + '-' + this.name);
  153. // uView的自定义图标类名为u-iconfont
  154. if (this.customPrefix == 'uicon') classes.push('u-iconfont');
  155. else classes.push(this.customPrefix);
  156. // 主题色,通过类配置
  157. if (this.color && this.$u.config.type.includes(this.color)) classes.push('u-icon__icon--' + this.color);
  158. // 阿里,头条,百度小程序通过数组绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
  159. // 故需将其拆成一个字符串的形式,通过空格隔开各个类名
  160. //#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
  161. classes = classes.join(' ');
  162. //#endif
  163. return classes;
  164. },
  165. iconStyle() {
  166. let style = {};
  167. style = {
  168. fontSize: this.size == 'inherit' ? 'inherit' : this.$u.addUnit(this.size),
  169. fontWeight: this.bold ? 'bold' : 'normal',
  170. // 某些特殊情况需要设置一个到顶部的距离,才能更好的垂直居中
  171. top: this.$u.addUnit(this.top)
  172. };
  173. // 非主题色值时,才当作颜色值
  174. if (this.color && !this.$u.config.type.includes(this.color)) style.color = this.color;
  175. return style;
  176. },
  177. // 判断传入的name属性,是否图片路径,只要带有"/"均认为是图片形式
  178. isImg() {
  179. return this.name.indexOf('/') !== -1;
  180. },
  181. imgStyle() {
  182. let style = {};
  183. // 如果设置width和height属性,则优先使用,否则使用size属性
  184. style.width = this.width ? this.$u.addUnit(this.width) : this.$u.addUnit(this.size);
  185. style.height = this.height ? this.$u.addUnit(this.height) : this.$u.addUnit(this.size);
  186. return style;
  187. }
  188. },
  189. methods: {
  190. click() {
  191. this.$emit('click', this.index);
  192. },
  193. touchstart() {
  194. this.$emit('touchstart', this.index);
  195. }
  196. }
  197. };
  198. </script>
  199. <style scoped lang="scss">
  200. @import "../../libs/css/style.components.scss";
  201. @import '../../iconfont.css';
  202. .u-icon {
  203. display: inline-flex;
  204. align-items: center;
  205. &--left {
  206. flex-direction: row-reverse;
  207. align-items: center;
  208. }
  209. &--right {
  210. flex-direction: row;
  211. align-items: center;
  212. }
  213. &--top {
  214. flex-direction: column-reverse;
  215. justify-content: center;
  216. }
  217. &--bottom {
  218. flex-direction: column;
  219. justify-content: center;
  220. }
  221. &__icon {
  222. position: relative;
  223. &--primary {
  224. color: $u-type-primary;
  225. }
  226. &--success {
  227. color: $u-type-success;
  228. }
  229. &--error {
  230. color: $u-type-error;
  231. }
  232. &--warning {
  233. color: $u-type-warning;
  234. }
  235. &--info {
  236. color: $u-type-info;
  237. }
  238. }
  239. &__img {
  240. height: auto;
  241. will-change: transform;
  242. }
  243. &__label {
  244. line-height: 1;
  245. }
  246. }
  247. </style>