u-tag.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <u-transition
  3. mode="fade"
  4. :show="show"
  5. >
  6. <view class="u-tag-wrapper">
  7. <view
  8. class="u-tag"
  9. :class="[`u-tag--${shape}`, !plain && `u-tag--${type}`, plain && `u-tag--${type}--plain`, `u-tag--${size}`, plain && plainFill && `u-tag--${type}--plain--fill`]"
  10. @tap.stop="clickHandler"
  11. :style="[{
  12. marginRight: closable ? '10px' : 0,
  13. marginTop: closable ? '10px' : 0,
  14. }, style]"
  15. >
  16. <slot name="icon">
  17. <view
  18. class="u-tag__icon"
  19. v-if="icon"
  20. >
  21. <image
  22. v-if="$u.test.image(icon)"
  23. :src="icon"
  24. :style="[imgStyle]"
  25. ></image>
  26. <u-icon
  27. v-else
  28. :color="elIconColor"
  29. :name="icon"
  30. :size="iconSize"
  31. ></u-icon>
  32. </view>
  33. </slot>
  34. <text
  35. class="u-tag__text"
  36. :style="[textColor]"
  37. :class="[`u-tag__text--${type}`, plain && `u-tag__text--${type}--plain`, `u-tag__text--${size}`]"
  38. >{{ text }}</text>
  39. </view>
  40. <view
  41. class="u-tag__close"
  42. :class="[`u-tag__close--${size}`]"
  43. v-if="closable"
  44. @tap.stop="closeHandler"
  45. :style="{backgroundColor: closeColor}"
  46. >
  47. <u-icon
  48. name="close"
  49. :size="closeSize"
  50. color="#ffffff"
  51. ></u-icon>
  52. </view>
  53. </view>
  54. </u-transition>
  55. </template>
  56. <script>
  57. import props from './props.js';
  58. /**
  59. * Tag 标签
  60. * @description tag组件一般用于标记和选择,我们提供了更加丰富的表现形式,能够较全面的涵盖您的使用场景
  61. * @tutorial https://www.uviewui.com/components/tag.html
  62. * @property {String} type 标签类型info、primary、success、warning、error (默认 'primary' )
  63. * @property {Boolean | String} disabled 不可用(默认 false )
  64. * @property {String} size 标签的大小,large,medium,mini (默认 'medium' )
  65. * @property {String} shape tag的形状,circle(两边半圆形), square(方形,带圆角)(默认 'square' )
  66. * @property {String | Number} text 标签的文字内容
  67. * @property {String} bgColor 背景颜色,默认为空字符串,即不处理
  68. * @property {String} color 标签字体颜色,默认为空字符串,即不处理
  69. * @property {String} borderColor 镂空形式标签的边框颜色
  70. * @property {String} closeColor 关闭按钮图标的颜色(默认 #C6C7CB)
  71. * @property {String | Number} name 点击时返回的索引值,用于区分例遍的数组哪个元素被点击了
  72. * @property {Boolean} plainFill 镂空时是否填充背景色(默认 false )
  73. * @property {Boolean} plain 是否镂空(默认 false )
  74. * @property {Boolean} closable 是否可关闭,设置为true,文字右边会出现一个关闭图标(默认 false )
  75. * @property {Boolean} show 标签显示与否(默认 true )
  76. * @property {String} icon 内置图标,或绝对路径的图片
  77. * @event {Function(index)} click 点击标签时触发 index: 传递的index参数值
  78. * @event {Function(index)} close closable为true时,点击标签关闭按钮触发 index: 传递的index参数值
  79. * @example <u-tag text="标签" type="error" plain plainFill></u-tag>
  80. */
  81. export default {
  82. name: 'u-tag',
  83. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  84. data() {
  85. return {
  86. }
  87. },
  88. computed: {
  89. style() {
  90. const style = {}
  91. if (this.bgColor) {
  92. style.backgroundColor = this.bgColor
  93. }
  94. if (this.color) {
  95. style.color = this.color
  96. }
  97. if(this.borderColor) {
  98. style.borderColor = this.borderColor
  99. }
  100. return style
  101. },
  102. // nvue下,文本颜色无法继承父元素
  103. textColor() {
  104. const style = {}
  105. if (this.color) {
  106. style.color = this.color
  107. }
  108. return style
  109. },
  110. imgStyle() {
  111. const width = this.size === 'large' ? '17px' : this.size === 'medium' ? '15px' : '13px'
  112. return {
  113. width,
  114. height: width
  115. }
  116. },
  117. // 文本的样式
  118. closeSize() {
  119. const size = this.size === 'large' ? 15 : this.size === 'medium' ? 13 : 12
  120. return size
  121. },
  122. // 图标大小
  123. iconSize() {
  124. const size = this.size === 'large' ? 21 : this.size === 'medium' ? 19 : 16
  125. return size
  126. },
  127. // 图标颜色
  128. elIconColor() {
  129. return this.iconColor ? this.iconColor : this.plain ? this.type : '#ffffff'
  130. }
  131. },
  132. methods: {
  133. // 点击关闭按钮
  134. closeHandler() {
  135. this.$emit('close', this.name)
  136. },
  137. // 点击标签
  138. clickHandler() {
  139. this.$emit('click', this.name)
  140. }
  141. }
  142. }
  143. </script>
  144. <style
  145. lang="scss"
  146. scoped
  147. >
  148. @import "../../libs/css/components.scss";
  149. .u-tag-wrapper {
  150. position: relative;
  151. }
  152. .u-tag {
  153. @include flex;
  154. align-items: center;
  155. border-style: solid;
  156. &--circle {
  157. border-radius: 100px;
  158. }
  159. &--square {
  160. border-radius: 3px;
  161. }
  162. &__icon {
  163. margin-right: 4px;
  164. }
  165. &__text {
  166. &--mini {
  167. font-size: 12px;
  168. line-height: 12px;
  169. }
  170. &--medium {
  171. font-size: 13px;
  172. line-height: 13px;
  173. }
  174. &--large {
  175. font-size: 15px;
  176. line-height: 15px;
  177. }
  178. }
  179. &--mini {
  180. height: 22px;
  181. line-height: 22px;
  182. padding: 0 5px;
  183. }
  184. &--medium {
  185. height: 26px;
  186. line-height: 22px;
  187. padding: 0 10px;
  188. }
  189. &--large {
  190. height: 32px;
  191. line-height: 32px;
  192. padding: 0 15px;
  193. }
  194. &--primary {
  195. background-color: $u-primary;
  196. border-width: 1px;
  197. border-color: $u-primary;
  198. }
  199. &--primary--plain {
  200. border-width: 1px;
  201. border-color: $u-primary;
  202. }
  203. &--primary--plain--fill {
  204. background-color: #ecf5ff;
  205. }
  206. &__text--primary {
  207. color: #FFFFFF;
  208. }
  209. &__text--primary--plain {
  210. color: $u-primary;
  211. }
  212. &--error {
  213. background-color: $u-error;
  214. border-width: 1px;
  215. border-color: $u-error;
  216. }
  217. &--error--plain {
  218. border-width: 1px;
  219. border-color: $u-error;
  220. }
  221. &--error--plain--fill {
  222. background-color: #fef0f0;
  223. }
  224. &__text--error {
  225. color: #FFFFFF;
  226. }
  227. &__text--error--plain {
  228. color: $u-error;
  229. }
  230. &--warning {
  231. background-color: $u-warning;
  232. border-width: 1px;
  233. border-color: $u-warning;
  234. }
  235. &--warning--plain {
  236. border-width: 1px;
  237. border-color: $u-warning;
  238. }
  239. &--warning--plain--fill {
  240. background-color: #fdf6ec;
  241. }
  242. &__text--warning {
  243. color: #FFFFFF;
  244. }
  245. &__text--warning--plain {
  246. color: $u-warning;
  247. }
  248. &--success {
  249. background-color: $u-success;
  250. border-width: 1px;
  251. border-color: $u-success;
  252. }
  253. &--success--plain {
  254. border-width: 1px;
  255. border-color: $u-success;
  256. }
  257. &--success--plain--fill {
  258. background-color: #f5fff0;
  259. }
  260. &__text--success {
  261. color: #FFFFFF;
  262. }
  263. &__text--success--plain {
  264. color: $u-success;
  265. }
  266. &--info {
  267. background-color: $u-info;
  268. border-width: 1px;
  269. border-color: $u-info;
  270. }
  271. &--info--plain {
  272. border-width: 1px;
  273. border-color: $u-info;
  274. }
  275. &--info--plain--fill {
  276. background-color: #f4f4f5;
  277. }
  278. &__text--info {
  279. color: #FFFFFF;
  280. }
  281. &__text--info--plain {
  282. color: $u-info;
  283. }
  284. &__close {
  285. position: absolute;
  286. z-index: 999;
  287. top: 10px;
  288. right: 10px;
  289. border-radius: 100px;
  290. background-color: #C6C7CB;
  291. @include flex(row);
  292. align-items: center;
  293. justify-content: center;
  294. /* #ifndef APP-NVUE */
  295. transform: scale(0.6) translate(80%, -80%);
  296. /* #endif */
  297. /* #ifdef APP-NVUE */
  298. transform: scale(0.6) translate(50%, -50%);
  299. /* #endif */
  300. &--mini {
  301. width: 18px;
  302. height: 18px;
  303. }
  304. &--medium {
  305. width: 22px;
  306. height: 22px;
  307. }
  308. &--large {
  309. width: 25px;
  310. height: 25px;
  311. }
  312. }
  313. }
  314. </style>