cl-text.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <view
  3. class="cl-text"
  4. :class="[classList]"
  5. :style="{
  6. margin: parseRpx(margin),
  7. color,
  8. 'font-size': '26rpx',
  9. 'letter-spacing': parseRpx(letterSpacing),
  10. '-webkit-line-clamp': ellipsis,
  11. }"
  12. @tap="onTap"
  13. >
  14. <!-- 价格 -->
  15. <text class="cl-text__symbol--price" v-if="type == 'price'">¥</text>
  16. <!-- 前缀图标 -->
  17. <text class="cl-text__prefix-icon" v-if="prefixIcon">
  18. <cl-icon :name="prefixIcon" :size="fontSize"></cl-icon>
  19. </text>
  20. <text
  21. class="cl-text__value"
  22. :style="{
  23. fontSize,
  24. }"
  25. >{{ d.value }}</text
  26. >
  27. <text class="cl-text__precision" v-if="type == 'price' && precision"
  28. >.{{ d.precision }}</text
  29. >
  30. <!-- 后缀图标 -->
  31. <text class="cl-text__suffix-icon" v-if="suffixIcon">
  32. <cl-icon :name="suffixIcon" :size="fontSize"></cl-icon>
  33. </text>
  34. </view>
  35. </template>
  36. <script>
  37. import { parseRpx } from "../../utils";
  38. /**
  39. * text 文本
  40. * @description 多种文本展示,包括价格,手机号等等
  41. * @tutorial https://docs.cool-js.com/uni/components/basic/text.html
  42. * @property {String, Number} value 文本内容
  43. * @property {String} type 类型 (text | price | phone)
  44. * @property {Boolean} encrypt 手机号是否加密,默认true
  45. * @property {Boolean} bold 是否粗体
  46. * @property {Boolean} block 是否块状
  47. * @property {Number} ellipsis 文本超出省略
  48. * @property {String} color 字体颜色,默认#444
  49. * @property {Number, String} size 字体大小,默认24
  50. * @property {Boolean} precision 价格小数点,默认true
  51. * @property {Boolean} lineThrough 穿过文本下的一条线
  52. * @property {Boolean} underline 文本下的一条线
  53. * @property {String, Number} letterSpacing 文本水平间隔
  54. * @property {String, Number, Array} margin 外间距
  55. * @property {String} prefixIcon 前缀图标
  56. * @property {String} suffixIcon 后缀图标
  57. * @example <cl-text value="Hello, Cool uni" />
  58. */
  59. export default {
  60. name: "cl-text",
  61. props: {
  62. // 绑定值
  63. value: [String, Number],
  64. // 类型:text | price | phone
  65. type: {
  66. type: String,
  67. default: "text",
  68. },
  69. // 是否加密
  70. encrypt: {
  71. type: Boolean,
  72. default: true,
  73. },
  74. // 是否粗体 500
  75. bold: Boolean,
  76. // 是否块状
  77. block: Boolean,
  78. // 文本超出省略
  79. ellipsis: Number,
  80. // 字体颜色
  81. color: {
  82. type: String,
  83. default: "#444",
  84. },
  85. // 字体大小
  86. size: {
  87. type: [String, Number],
  88. default: 24,
  89. },
  90. // 小数点:price 有效
  91. precision: {
  92. type: Boolean,
  93. default: true,
  94. },
  95. // 穿过文本中的一条线
  96. lineThrough: Boolean,
  97. // 文本下的一条线
  98. underline: Boolean,
  99. // 文本水平间隔
  100. letterSpacing: {
  101. type: [String, Number],
  102. default: 0,
  103. },
  104. // 外间距
  105. margin: [String, Number, Array],
  106. // 前缀图标
  107. prefixIcon: String,
  108. // 后缀图标
  109. suffixIcon: String,
  110. // 是否能长按复制
  111. copy: Boolean,
  112. },
  113. computed: {
  114. d() {
  115. if (this.type == "price") {
  116. const [value, precision = "00"] = parseFloat(this.value || 0)
  117. .toFixed(2)
  118. .split(".");
  119. if (this.precision) {
  120. return {
  121. value,
  122. precision,
  123. };
  124. } else {
  125. return {
  126. value,
  127. };
  128. }
  129. } else if (this.type == "phone") {
  130. const str = String(this.value);
  131. return {
  132. value: this.encrypt ? str.substr(0, 3) + "****" + str.substr(7) : str,
  133. };
  134. } else {
  135. return {
  136. value: this.value,
  137. };
  138. }
  139. },
  140. fontSize() {
  141. return parseRpx(this.size);
  142. },
  143. classList() {
  144. let list = [];
  145. if (this.bold) {
  146. list.push("is-bold");
  147. }
  148. if (this.block) {
  149. list.push("is-block");
  150. }
  151. if (this.lineThrough) {
  152. list.push("is-line-through");
  153. }
  154. if (this.underline) {
  155. list.push("is-underline");
  156. }
  157. if (this.ellipsis > 0) {
  158. list.push("is-ellipsis");
  159. }
  160. if (this.type) {
  161. list.push(`is-${this.type}`);
  162. }
  163. if (this.color) {
  164. list.push(`is-color-${this.color}`);
  165. }
  166. if (this.copy) {
  167. list.push("is-copy");
  168. }
  169. return list.join(" ");
  170. },
  171. },
  172. methods: {
  173. parseRpx,
  174. onTap(e) {
  175. this.$emit("click", e);
  176. this.$emit("tap", e);
  177. },
  178. },
  179. };
  180. </script>