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.
 
 
 

293 lines
7.1 KiB

  1. <template>
  2. <view v-if="show" :class="[
  3. disabled ? 'u-disabled' : '',
  4. 'u-size-' + size,
  5. 'u-shape-' + shape,
  6. 'u-mode-' + mode + '-' + type
  7. ]"
  8. class="u-tag" :style="[customStyle]" @tap="clickTag">
  9. {{text}}
  10. <view class="u-icon-wrap" @tap.stop>
  11. <u-icon @click="close" size="22" v-if="closeable" :color="closeIconColor"
  12. name="close" class="u-close-icon" :style="[iconStyle]"></u-icon>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. /**
  18. * tag 提示
  19. * @description 该组件一般用于标记和选择
  20. * @tutorial https://www.uviewui.com/components/tag.html
  21. * @property {String} type 主题类型(默认primary)
  22. * @property {String} size 标签大小(默认default)
  23. * @property {String} shape 标签形状(默认square)
  24. * @property {String} text 标签的文字内容
  25. * @property {String} bg-color 自定义标签的背景颜色
  26. * @property {String} border-color 标签的边框颜色
  27. * @property {String} close-color 关闭按钮的颜色
  28. * @property {String Number} index 点击标签时,会通过click事件返回该值
  29. * @property {String} mode 模式选择,见官网说明(默认light)
  30. * @property {Boolean} closeable 是否可关闭,设置为true,文字右边会出现一个关闭图标(默认false)
  31. * @property {Boolean} show 标签显示与否(默认true)
  32. * @event {Function} click 点击标签触发
  33. * @event {Function} close closeable为true时,点击标签关闭按钮触发
  34. * @example <u-tag text="雪月夜" type="success" />
  35. */
  36. export default {
  37. name: 'u-tag',
  38. // 是否禁用这个标签,禁用的话,会屏蔽点击事件
  39. props: {
  40. // 标签类型info、primary、success、warning、error
  41. type: {
  42. type: String,
  43. default: 'primary'
  44. },
  45. disabled: {
  46. type: [Boolean, String],
  47. default: false
  48. },
  49. // 标签的大小,分为default(默认),mini(较小)
  50. size: {
  51. type: String,
  52. default: 'default'
  53. },
  54. // tag的形状,circle(两边半圆形), square(方形,带圆角),circleLeft(左边是半圆),circleRight(右边是半圆)
  55. shape: {
  56. type: String,
  57. default: 'square'
  58. },
  59. // 标签文字
  60. text: {
  61. type: [String, Number],
  62. default: ''
  63. },
  64. // 背景颜色,默认为空字符串,即不处理
  65. bgColor: {
  66. type: String,
  67. default: ''
  68. },
  69. // 标签字体颜色,默认为空字符串,即不处理
  70. color: {
  71. type: String,
  72. default: ''
  73. },
  74. // 镂空形式标签的边框颜色
  75. borderColor: {
  76. type: String,
  77. default: ''
  78. },
  79. // 关闭按钮图标的颜色
  80. closeColor: {
  81. type: String,
  82. default: ''
  83. },
  84. // 点击时返回的索引值,用于区分例遍的数组哪个元素被点击了
  85. index: {
  86. type: [Number, String],
  87. default: ''
  88. },
  89. // 模式选择,dark|light|plain
  90. mode: {
  91. type: String,
  92. default: 'light'
  93. },
  94. // 是否可关闭
  95. closeable: {
  96. type: Boolean,
  97. default: false
  98. },
  99. // 是否显示
  100. show: {
  101. type: Boolean,
  102. default: true
  103. }
  104. },
  105. data() {
  106. return {
  107. }
  108. },
  109. computed: {
  110. customStyle() {
  111. let style = {};
  112. // 文字颜色(如果有此值,会覆盖type值的颜色)
  113. if(this.color) style.color = this.color+"!important";
  114. // tag的背景颜色(如果有此值,会覆盖type值的颜色)
  115. if(this.bgColor) style.backgroundColor = this.bgColor+"!important";
  116. // 如果是镂空型tag,没有传递边框颜色(borderColor)的话,使用文字的颜色(color属性)
  117. if(this.mode == 'plain' && this.color && !this.borderColor) style.borderColor = this.color;
  118. else style.borderColor = this.borderColor;
  119. return style;
  120. },
  121. iconStyle() {
  122. if(!this.closeable) return ;
  123. let style = {};
  124. if(this.size == 'mini') style.fontSize = '20rpx';
  125. else style.fontSize = '22rpx';
  126. if(this.mode == 'plain' || this.mode == 'light') style.color = this.type;
  127. else if(this.mode == 'dark') style.color = "#ffffff";
  128. if(this.closeColor) style.color = this.closeColor;
  129. return style;
  130. },
  131. // 关闭图标的颜色
  132. closeIconColor() {
  133. // 如果定义了关闭图标的颜色,就用此值,否则用字体颜色的值
  134. // 如果上面的二者都没有,如果是dark深色模式,图标就为白色
  135. // 最后如果上面的三者都不合适,就返回type值给图标获取颜色
  136. let color = '';
  137. if(this.closeColor) return this.closeColor;
  138. else if(this.color) return this.color;
  139. else if(this.mode == 'dark') return '#ffffff';
  140. else return this.type;
  141. }
  142. },
  143. methods: {
  144. // 标签被点击
  145. clickTag() {
  146. // 如果是disabled状态,不发送点击事件
  147. if(this.disabled) return ;
  148. this.$emit('click', this.index);
  149. },
  150. // 点击标签关闭按钮
  151. close() {
  152. this.$emit('close', this.index);
  153. }
  154. }
  155. }
  156. </script>
  157. <style lang="scss" scoped>
  158. @import "../../libs/css/style.components.scss";
  159. .u-tag {
  160. box-sizing: border-box;
  161. align-items: center;
  162. border-radius: 6rpx;
  163. display: inline-block;
  164. line-height: 1;
  165. }
  166. .u-size-default {
  167. font-size: 22rpx;
  168. padding: 12rpx 22rpx;
  169. }
  170. .u-size-mini {
  171. font-size: 20rpx;
  172. padding: 6rpx 12rpx;
  173. }
  174. .u-mode-light-primary {
  175. background-color: $u-type-primary-light;
  176. color: $u-type-primary;
  177. border: 1px solid $u-type-primary-disabled;
  178. }
  179. .u-mode-light-success {
  180. background-color: $u-type-success-light;
  181. color: $u-type-success;
  182. border: 1px solid $u-type-success-disabled;
  183. }
  184. .u-mode-light-error {
  185. background-color: $u-type-error-light;
  186. color: $u-type-error;
  187. border: 1px solid $u-type-error-disabled;
  188. }
  189. .u-mode-light-warning {
  190. background-color: $u-type-warning-light;
  191. color: $u-type-warning;
  192. border: 1px solid $u-type-warning-disabled;
  193. }
  194. .u-mode-light-info {
  195. background-color: $u-type-info-light;
  196. color: $u-type-info;
  197. border: 1px solid $u-type-info-disabled;
  198. }
  199. .u-mode-dark-primary {
  200. background-color: $u-type-primary;
  201. color: #FFFFFF;
  202. }
  203. .u-mode-dark-success {
  204. background-color: $u-type-success;
  205. color: #FFFFFF;
  206. }
  207. .u-mode-dark-error {
  208. background-color: $u-type-error;
  209. color: #FFFFFF;
  210. }
  211. .u-mode-dark-warning {
  212. background-color: $u-type-warning;
  213. color: #FFFFFF;
  214. }
  215. .u-mode-dark-info {
  216. background-color: $u-type-info;
  217. color: #FFFFFF;
  218. }
  219. .u-mode-plain-primary {
  220. background-color: #FFFFFF;
  221. color: $u-type-primary;
  222. border: 1px solid $u-type-primary;
  223. }
  224. .u-mode-plain-success {
  225. background-color: #FFFFFF;
  226. color: $u-type-success;
  227. border: 1px solid $u-type-success;
  228. }
  229. .u-mode-plain-error {
  230. background-color: #FFFFFF;
  231. color: $u-type-error;
  232. border: 1px solid $u-type-error;
  233. }
  234. .u-mode-plain-warning {
  235. background-color: #FFFFFF;
  236. color: $u-type-warning;
  237. border: 1px solid $u-type-warning;
  238. }
  239. .u-mode-plain-info {
  240. background-color: #FFFFFF;
  241. color: $u-type-info;
  242. border: 1px solid $u-type-info;
  243. }
  244. .u-disabled {
  245. opacity: 0.55;
  246. }
  247. .u-shape-circle {
  248. border-radius: 100rpx;
  249. }
  250. .u-shape-circleRight {
  251. border-radius: 0 100rpx 100rpx 0;
  252. }
  253. .u-shape-circleLeft {
  254. border-radius: 100rpx 0 0 100rpx;
  255. }
  256. .u-close-icon {
  257. margin-left: 14rpx;
  258. font-size: 22rpx;
  259. color: $u-type-success;
  260. }
  261. .u-icon-wrap {
  262. display: inline-flex;
  263. transform: scale(0.86);
  264. }
  265. </style>