Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

u-icon.vue 6.3 KiB

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