uni-tag.vue 4.9 KB

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