u-text.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <view
  3. class="u-text"
  4. :class="[]"
  5. v-if="show"
  6. :style="{
  7. margin: margin,
  8. justifyContent: align === 'left' ? 'flex-start' : align === 'center' ? 'center' : 'flex-end'
  9. }"
  10. @tap="clickHandler"
  11. >
  12. <text
  13. :class="['u-text__price', type && `u-text__value--${type}`]"
  14. v-if="mode === 'price'"
  15. :style="[valueStyle]"
  16. >¥
  17. </text
  18. >
  19. <view class="u-text__prefix-icon" v-if="prefixIcon">
  20. <u-icon
  21. :name="prefixIcon"
  22. :customStyle="$u.addStyle(iconStyle)"
  23. ></u-icon>
  24. </view>
  25. <u-link
  26. v-if="mode === 'link'"
  27. :text="value"
  28. :href="href"
  29. underLine
  30. ></u-link>
  31. <template v-else-if="openType && isMp">
  32. <button
  33. class="u-reset-button u-text__value"
  34. :style="[valueStyle]"
  35. :data-index="index"
  36. :openType="openType"
  37. @getuserinfo="onGetUserInfo"
  38. @contact="onContact"
  39. @getphonenumber="onGetPhoneNumber"
  40. @error="onError"
  41. @launchapp="onLaunchApp"
  42. @opensetting="onOpenSetting"
  43. :lang="lang"
  44. :session-from="sessionFrom"
  45. :send-message-title="sendMessageTitle"
  46. :send-message-path="sendMessagePath"
  47. :send-message-img="sendMessageImg"
  48. :show-message-card="showMessageCard"
  49. :app-parameter="appParameter"
  50. >
  51. {{ value }}
  52. </button>
  53. </template>
  54. <text
  55. v-else
  56. class="u-text__value"
  57. :style="[valueStyle]"
  58. :class="[
  59. type && `u-text__value--${type}`,
  60. lines && `u-line-${lines}`
  61. ]"
  62. >{{ value }}
  63. </text
  64. >
  65. <view class="u-text__suffix-icon" v-if="suffixIcon">
  66. <u-icon
  67. :name="suffixIcon"
  68. :customStyle="$u.addStyle(iconStyle)"
  69. ></u-icon>
  70. </view>
  71. </view>
  72. </template>
  73. <script>
  74. import value from './value.js'
  75. import button from '../../libs/mixin/button.js'
  76. import openType from '../../libs/mixin/openType.js'
  77. import props from './props.js'
  78. /**
  79. * Text 文本
  80. * @description 此组件集成了文本类在项目中的常用功能,包括状态,拨打电话,格式化日期,*替换,超链接...等功能。 您大可不必在使用特殊文本时自己定义,text组件几乎涵盖您能使用的大部分场景。
  81. * @tutorial https://www.uviewui.com/components/loading.html
  82. * @property {String} type 主题颜色
  83. * @property {Boolean} show 是否显示(默认 true )
  84. * @property {String | Number} text 显示的值
  85. * @property {String} prefixIcon 前置图标
  86. * @property {String} suffixIcon 后置图标
  87. * @property {String} mode 文本处理的匹配模式 text-普通文本,price-价格,phone-手机号,name-姓名,date-日期,link-超链接
  88. * @property {String} href mode=link下,配置的链接
  89. * @property {String | Function} format 格式化规则
  90. * @property {Boolean} call mode=phone时,点击文本是否拨打电话(默认 false )
  91. * @property {String} openType 小程序的打开方式
  92. * @property {Boolean} bold 是否粗体,默认normal(默认 false )
  93. * @property {Boolean} block 是否块状(默认 false )
  94. * @property {String | Number} lines 文本显示的行数,如果设置,超出此行数,将会显示省略号
  95. * @property {String} color 文本颜色(默认 '#303133' )
  96. * @property {String | Number} size 字体大小(默认 15 )
  97. * @property {Object | String} iconStyle 图标的样式 (默认 {fontSize: '15px'} )
  98. * @property {String} decoration 文字装饰,下划线,中划线等,可选值 none|underline|line-through(默认 'none' )
  99. * @property {Object | String | Number} margin 外边距,对象、字符串,数值形式均可(默认 0 )
  100. * @property {String | Number} lineHeight 文本行高
  101. * @property {String} align 文本对齐方式,可选值left|center|right(默认 'left' )
  102. * @property {String} wordWrap 文字换行,可选值break-word|normal|anywhere(默认 'normal' )
  103. * @event {Function} click 点击触发事件
  104. * @example <u--text text="我用十年青春,赴你最后之约"></u--text>
  105. */
  106. export default {
  107. name: 'u--text',
  108. // #ifdef MP
  109. mixins: [uni.$u.mpMixin, uni.$u.mixin, value, button, openType, props],
  110. // #endif
  111. // #ifndef MP
  112. mixins: [uni.$u.mpMixin, uni.$u.mixin, value, props],
  113. // #endif
  114. computed: {
  115. valueStyle() {
  116. const style = {
  117. textDecoration: this.decoration,
  118. fontWeight: this.bold ? 'bold' : 'normal',
  119. wordWrap: this.wordWrap,
  120. fontSize: uni.$u.addUnit(this.size)
  121. }
  122. !this.type && (style.color = this.color)
  123. this.isNvue && this.lines && (style.lines = this.lines)
  124. this.lineHeight &&
  125. (style.lineHeight = uni.$u.addUnit(this.lineHeight))
  126. !this.isNvue && this.block && (style.display = 'block')
  127. return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle))
  128. },
  129. isNvue() {
  130. let nvue = false
  131. // #ifdef APP-NVUE
  132. nvue = true
  133. // #endif
  134. return nvue
  135. },
  136. isMp() {
  137. let mp = false
  138. // #ifdef MP
  139. mp = true
  140. // #endif
  141. return mp
  142. }
  143. },
  144. data() {
  145. return {}
  146. },
  147. methods: {
  148. clickHandler() {
  149. // 如果为手机号模式,拨打电话
  150. if (this.call && this.mode === 'phone' && uni.$u.test.mobile(this.text)) {
  151. uni.makePhoneCall({
  152. phoneNumber: this.text
  153. })
  154. }
  155. this.$emit('click')
  156. }
  157. }
  158. }
  159. </script>
  160. <style lang="scss" scoped>
  161. @import '../../libs/css/components.scss';
  162. .u-text {
  163. @include flex(row);
  164. align-items: center;
  165. flex-wrap: nowrap;
  166. flex: 1;
  167. &__price {
  168. font-size: 14px;
  169. color: $u-content-color;
  170. }
  171. &__value {
  172. font-size: 14px;
  173. @include flex;
  174. color: $u-content-color;
  175. flex-wrap: wrap;
  176. // flex: 1;
  177. text-overflow: ellipsis;
  178. align-items: center;
  179. &--primary {
  180. color: $u-primary;
  181. }
  182. &--warning {
  183. color: $u-warning;
  184. }
  185. &--success {
  186. color: $u-success;
  187. }
  188. &--info {
  189. color: $u-info;
  190. }
  191. &--error {
  192. color: $u-error;
  193. }
  194. &--main {
  195. color: $u-main-color;
  196. }
  197. &--content {
  198. color: $u-content-color;
  199. }
  200. &--tips {
  201. color: $u-tips-color;
  202. }
  203. &--light {
  204. color: $u-light-color;
  205. }
  206. }
  207. }
  208. </style>