u-icon.vue 10 KB

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