u-icon.vue 10 KB

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