You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
7.4 KiB

2 months ago
  1. <template>
  2. <view
  3. class="u-icon"
  4. @tap="clickHandler"
  5. :class="['u-icon--' + labelPos]"
  6. >
  7. <image
  8. class="u-icon__img"
  9. v-if="isImg"
  10. :src="name"
  11. :mode="imgMode"
  12. :style="[imgStyle, addStyle(customStyle)]"
  13. ></image>
  14. <text
  15. v-else
  16. class="u-icon__icon"
  17. :class="uClasses"
  18. :style="[iconStyle, addStyle(customStyle)]"
  19. :hover-class="hoverClass"
  20. >{{icon}}</text>
  21. <!-- 这里进行空字符串判断如果仅仅是v-if="label"可能会出现传递0的时候结果也无法显示 -->
  22. <text
  23. v-if="label !== ''"
  24. class="u-icon__label"
  25. :style="{
  26. color: labelColor,
  27. fontSize: addUnit(labelSize),
  28. marginLeft: labelPos == 'right' ? addUnit(space) : 0,
  29. marginTop: labelPos == 'bottom' ? addUnit(space) : 0,
  30. marginRight: labelPos == 'left' ? addUnit(space) : 0,
  31. marginBottom: labelPos == 'top' ? addUnit(space) : 0,
  32. }"
  33. >{{ label }}</text>
  34. </view>
  35. </template>
  36. <script>
  37. // #ifdef APP-NVUE
  38. // nvue通过weex的dom模块引入字体,相关文档地址如下:
  39. // https://weex.apache.org/zh/docs/modules/dom.html#addrule
  40. const fontUrl = 'https://at.alicdn.com/t/font_2225171_8kdcwk4po24.ttf'
  41. const domModule = weex.requireModule('dom')
  42. domModule.addRule('fontFace', {
  43. 'fontFamily': "uicon-iconfont",
  44. 'src': `url('${fontUrl}')`
  45. })
  46. // #endif
  47. // 引入图标名称,已经对应的unicode
  48. import icons from './icons'
  49. import { props } from './props';
  50. import { mpMixin } from '../../libs/mixin/mpMixin';
  51. import { mixin } from '../../libs/mixin/mixin';
  52. import { addUnit, addStyle } from '../../libs/function/index';
  53. import config from '../../libs/config/config';
  54. /**
  55. * icon 图标
  56. * @description 基于字体的图标集包含了大多数常见场景的图标
  57. * @tutorial https://ijry.github.io/uview-plus/components/icon.html
  58. * @property {String} name 图标名称见示例图标集
  59. * @property {String} color 图标颜色,可接受主题色 默认 color['u-content-color']
  60. * @property {String | Number} size 图标字体大小单位px 默认 '16px'
  61. * @property {Boolean} bold 是否显示粗体 默认 false
  62. * @property {String | Number} index 点击图标的时候传递事件出去的index用于区分点击了哪一个
  63. * @property {String} hoverClass 图标按下去的样式类用法同uni的view组件的hoverClass参数详情见官网
  64. * @property {String} customPrefix 自定义扩展前缀方便用户扩展自己的图标库 默认 'uicon'
  65. * @property {String | Number} label 图标右侧的label文字
  66. * @property {String} labelPos label相对于图标的位置只能right或bottom 默认 'right'
  67. * @property {String | Number} labelSize label字体大小单位px 默认 '15px'
  68. * @property {String} labelColor 图标右侧的label文字颜色 默认 color['u-content-color']
  69. * @property {String | Number} space label与图标的距离单位px 默认 '3px'
  70. * @property {String} imgMode 图片的mode
  71. * @property {String | Number} width 显示图片小图标时的宽度
  72. * @property {String | Number} height 显示图片小图标时的高度
  73. * @property {String | Number} top 图标在垂直方向上的定位 用于解决某些情况下让图标垂直居中的用途 默认 0
  74. * @property {Boolean} stop 是否阻止事件传播 默认 false
  75. * @property {Object} customStyle icon的样式对象形式
  76. * @event {Function} click 点击图标时触发
  77. * @event {Function} touchstart 事件触摸时触发
  78. * @example <u-icon name="photo" color="#2979ff" size="28"></u-icon>
  79. */
  80. export default {
  81. name: 'u-icon',
  82. data() {
  83. return {
  84. }
  85. },
  86. emits: ['click'],
  87. mixins: [mpMixin, mixin, props],
  88. computed: {
  89. uClasses() {
  90. let classes = []
  91. classes.push(this.customPrefix + '-' + this.name)
  92. // uView的自定义图标类名为u-iconfont
  93. if (this.customPrefix == 'uicon') {
  94. classes.push('u-iconfont')
  95. } else {
  96. // 不能缺少这一步,否则自定义图标会无效
  97. classes.push(this.customPrefix)
  98. }
  99. // 主题色,通过类配置
  100. if (this.color && config.type.includes(this.color)) classes.push('u-icon__icon--' + this.color)
  101. // 阿里,头条,百度小程序通过数组绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
  102. // 故需将其拆成一个字符串的形式,通过空格隔开各个类名
  103. //#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
  104. classes = classes.join(' ')
  105. //#endif
  106. return classes
  107. },
  108. iconStyle() {
  109. let style = {}
  110. style = {
  111. fontSize: addUnit(this.size),
  112. lineHeight: addUnit(this.size),
  113. fontWeight: this.bold ? 'bold' : 'normal',
  114. // 某些特殊情况需要设置一个到顶部的距离,才能更好的垂直居中
  115. top: addUnit(this.top)
  116. }
  117. // 非主题色值时,才当作颜色值
  118. if (this.color && !config.type.includes(this.color)) style.color = this.color
  119. return style
  120. },
  121. // 判断传入的name属性,是否图片路径,只要带有"/"均认为是图片形式
  122. isImg() {
  123. return this.name.indexOf('/') !== -1
  124. },
  125. imgStyle() {
  126. let style = {}
  127. // 如果设置width和height属性,则优先使用,否则使用size属性
  128. style.width = this.width ? addUnit(this.width) : addUnit(this.size)
  129. style.height = this.height ? addUnit(this.height) : addUnit(this.size)
  130. return style
  131. },
  132. // 通过图标名,查找对应的图标
  133. icon() {
  134. // 使用自定义图标的时候页面上会把name属性也展示出来,所以在这里处理一下
  135. if (this.customPrefix !== "uicon") return "";
  136. // 如果内置的图标中找不到对应的图标,就直接返回name值,因为用户可能传入的是unicode代码
  137. return icons['uicon-' + this.name] || this.name
  138. }
  139. },
  140. methods: {
  141. addStyle,
  142. addUnit,
  143. clickHandler(e) {
  144. this.$emit('click', this.index)
  145. // 是否阻止事件冒泡
  146. this.stop && this.preventEvent(e)
  147. }
  148. }
  149. }
  150. </script>
  151. <style lang="scss" scoped>
  152. @import "../../libs/css/components.scss";
  153. // 变量定义
  154. $u-icon-primary: $u-primary !default;
  155. $u-icon-success: $u-success !default;
  156. $u-icon-info: $u-info !default;
  157. $u-icon-warning: $u-warning !default;
  158. $u-icon-error: $u-error !default;
  159. $u-icon-label-line-height:1 !default;
  160. /* #ifndef APP-NVUE */
  161. // 非nvue下加载字体
  162. @font-face {
  163. font-family: 'uicon-iconfont';
  164. src: url('https://at.alicdn.com/t/font_2225171_8kdcwk4po24.ttf') format('truetype');
  165. }
  166. /* #endif */
  167. .u-icon {
  168. /* #ifndef APP-NVUE */
  169. display: flex;
  170. /* #endif */
  171. align-items: center;
  172. &--left {
  173. flex-direction: row-reverse;
  174. align-items: center;
  175. }
  176. &--right {
  177. flex-direction: row;
  178. align-items: center;
  179. }
  180. &--top {
  181. flex-direction: column-reverse;
  182. justify-content: center;
  183. }
  184. &--bottom {
  185. flex-direction: column;
  186. justify-content: center;
  187. }
  188. &__icon {
  189. font-family: uicon-iconfont;
  190. position: relative;
  191. @include flex;
  192. align-items: center;
  193. &--primary {
  194. color: $u-icon-primary;
  195. }
  196. &--success {
  197. color: $u-icon-success;
  198. }
  199. &--error {
  200. color: $u-icon-error;
  201. }
  202. &--warning {
  203. color: $u-icon-warning;
  204. }
  205. &--info {
  206. color: $u-icon-info;
  207. }
  208. }
  209. &__img {
  210. /* #ifndef APP-NVUE */
  211. height: auto;
  212. will-change: transform;
  213. /* #endif */
  214. }
  215. &__label {
  216. /* #ifndef APP-NVUE */
  217. line-height: $u-icon-label-line-height;
  218. /* #endif */
  219. }
  220. }
  221. </style>