uni-tag.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <text class="uni-tag" v-if="text" :class="classes" :style="customStyle" @click="onClick">
  3. <slot />{{text}}
  4. <slot name="right" />
  5. </text>
  6. </template>
  7. <script>
  8. /**
  9. * Tag 标签
  10. * @description 用于展示1个或多个文字标签,可点击切换选中、不选中的状态
  11. * @tutorial https://ext.dcloud.net.cn/plugin?id=35
  12. * @property {String} text 标签内容
  13. * @property {String} size = [normal|small] 大小尺寸
  14. * @value normal 正常
  15. * @value small 小尺寸
  16. * @property {String} type = [default|primary|success|warning|error|royal] 颜色类型
  17. * @value default 灰色
  18. * @value primary 蓝色
  19. * @value success 绿色
  20. * @value warning 黄色
  21. * @value error 红色
  22. * @value royal 紫色
  23. * @property {Boolean} disabled = [true|false] 是否为禁用状态
  24. * @property {Boolean} inverted = [true|false] 是否无需背景颜色(空心标签)
  25. * @property {Boolean} circle = [true|false] 是否为圆角
  26. * @event {Function} click 点击 Tag 触发事件
  27. */
  28. export default {
  29. name: "UniTag",
  30. emits: ['click'],
  31. props: {
  32. type: {
  33. // 标签类型default、primary、success、warning、error、royal
  34. type: String,
  35. default: "default"
  36. },
  37. size: {
  38. // 标签大小 normal, small
  39. type: String,
  40. default: "normal"
  41. },
  42. // 标签内容
  43. text: {
  44. type: String,
  45. default: ""
  46. },
  47. disabled: {
  48. // 是否为禁用状态
  49. type: [Boolean, String],
  50. default: false
  51. },
  52. inverted: {
  53. // 是否为空心
  54. type: [Boolean, String],
  55. default: false
  56. },
  57. circle: {
  58. // 是否为圆角样式
  59. type: [Boolean, String],
  60. default: false
  61. },
  62. mark: {
  63. // 是否为标记样式
  64. type: [Boolean, String],
  65. default: false
  66. },
  67. customStyle: {
  68. type: String,
  69. default: ''
  70. }
  71. },
  72. computed: {
  73. classes() {
  74. const {
  75. type,
  76. disabled,
  77. inverted,
  78. circle,
  79. mark,
  80. size,
  81. isTrue
  82. } = this
  83. const classArr = [
  84. 'uni-tag--' + type,
  85. isTrue(disabled) ? 'uni-tag--disabled' : '',
  86. isTrue(inverted) ? type + '-uni-tag--inverted' : '',
  87. isTrue(circle) ? 'uni-tag--circle' : '',
  88. isTrue(mark) ? 'uni-tag--mark' : '',
  89. 'uni-tag--' + size,
  90. // type === 'default' ? 'uni-tag--default' : 'uni-tag-text',
  91. isTrue(inverted) ? 'uni-tag-text--' + type : '',
  92. size === 'small' ? 'uni-tag-text--small' : ''
  93. ]
  94. return classArr.join(' ')
  95. }
  96. },
  97. methods: {
  98. isTrue(value) {
  99. return value === true || value === 'true'
  100. },
  101. onClick() {
  102. if (this.isTrue(this.disabled)) return
  103. this.$emit("click");
  104. }
  105. }
  106. };
  107. </script>
  108. <style scoped>
  109. .uni-tag {
  110. /* #ifndef APP-NVUE */
  111. display: inline-block;
  112. /* #endif */
  113. /* #ifdef APP-NVUE */
  114. align-self: flex-start;
  115. /* #endif */
  116. padding: 0px 16px;
  117. line-height: 30px;
  118. color: #333;
  119. border-radius: 3px;
  120. background-color: #f8f8f8;
  121. border-width: 1rpx;
  122. border-style: solid;
  123. border-color: #f8f8f8;
  124. /* #ifdef H5 */
  125. cursor: pointer;
  126. /* #endif */
  127. }
  128. .uni-tag--circle {
  129. border-radius: 15px;
  130. }
  131. .uni-tag--mark {
  132. border-top-left-radius: 0;
  133. border-bottom-left-radius: 0;
  134. border-top-right-radius: 15px;
  135. border-bottom-right-radius: 15px;
  136. }
  137. .uni-tag--disabled {
  138. opacity: 0.5;
  139. /* #ifdef H5 */
  140. cursor: not-allowed;
  141. /* #endif */
  142. }
  143. .uni-tag--small {
  144. height: 20px;
  145. padding: 0px 8px;
  146. line-height: 20px;
  147. font-size: 12px;
  148. }
  149. .uni-tag--default {
  150. color: #333;
  151. font-size: 14px;
  152. }
  153. .uni-tag--royal {
  154. color: #333;
  155. font-size: 14px;
  156. }
  157. .uni-tag-text--small {
  158. font-size: 12px;
  159. }
  160. .uni-tag-text {
  161. color: #fff;
  162. font-size: 14px;
  163. }
  164. .uni-tag-text--primary {
  165. color: #007aff;
  166. }
  167. .uni-tag-text--success {
  168. color: #4cd964;
  169. }
  170. .uni-tag-text--warning {
  171. color: #f0ad4e;
  172. }
  173. .uni-tag-text--error {
  174. color: #dd524d;
  175. }
  176. .uni-tag-text--royal {
  177. color: #4335d6;
  178. }
  179. .uni-tag--primary {
  180. color: #fff;
  181. background-color: #007aff;
  182. border-width: 1rpx;
  183. border-style: solid;
  184. border-color: #007aff;
  185. }
  186. .primary-uni-tag--inverted {
  187. color: #007aff;
  188. background-color: #ffffff;
  189. border-width: 1rpx;
  190. border-style: solid;
  191. border-color: #007aff;
  192. }
  193. .uni-tag--success {
  194. color: #fff;
  195. background-color: #4cd964;
  196. border-width: 1rpx;
  197. border-style: solid;
  198. border-color: #4cd964;
  199. }
  200. .success-uni-tag--inverted {
  201. color: #4cd964;
  202. background-color: #ffffff;
  203. border-width: 1rpx;
  204. border-style: solid;
  205. border-color: #4cd964;
  206. }
  207. .uni-tag--warning {
  208. color: #fff;
  209. background-color: #f0ad4e;
  210. border-width: 1rpx;
  211. border-style: solid;
  212. border-color: #f0ad4e;
  213. }
  214. .warning-uni-tag--inverted {
  215. color: #f0ad4e;
  216. background-color: #ffffff;
  217. border-width: 1rpx;
  218. border-style: solid;
  219. border-color: #f0ad4e;
  220. }
  221. .uni-tag--error {
  222. color: #fff;
  223. background-color: #dd524d;
  224. border-width: 1rpx;
  225. border-style: solid;
  226. border-color: #dd524d;
  227. }
  228. .error-uni-tag--inverted {
  229. color: #dd524d;
  230. background-color: #ffffff;
  231. border-width: 1rpx;
  232. border-style: solid;
  233. border-color: #dd524d;
  234. }
  235. .uni-tag--royal {
  236. color: #fff;
  237. background-color: #4335d6;
  238. border-width: 1rpx;
  239. border-style: solid;
  240. border-color: #4335d6;
  241. }
  242. .royal-uni-tag--inverted {
  243. color: #4335d6;
  244. background-color: #ffffff;
  245. border-width: 1rpx;
  246. border-style: solid;
  247. border-color: #4335d6;
  248. }
  249. .uni-tag--inverted {
  250. color: #333;
  251. background-color: #ffffff;
  252. border-width: 1rpx;
  253. border-style: solid;
  254. border-color: #f8f8f8;
  255. }
  256. </style>