u-text.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. >¥</text
  17. >
  18. <view class="u-text__prefix-icon" v-if="prefixIcon">
  19. <u-icon
  20. :name="prefixIcon"
  21. :customStyle="$u.addStyle(iconStyle)"
  22. ></u-icon>
  23. </view>
  24. <u-link
  25. v-if="mode === 'link'"
  26. :text="value"
  27. :href="href"
  28. underLine
  29. ></u-link>
  30. <template v-else-if="openType && isMp">
  31. <button
  32. class="u-reset-button u-text__value"
  33. :style="[valueStyle]"
  34. :data-index="index"
  35. :openType="openType"
  36. @getuserinfo="onGetUserInfo"
  37. @contact="onContact"
  38. @getphonenumber="onGetPhoneNumber"
  39. @error="onError"
  40. @launchapp="onLaunchApp"
  41. @opensetting="onOpenSetting"
  42. :lang="lang"
  43. :session-from="sessionFrom"
  44. :send-message-title="sendMessageTitle"
  45. :send-message-path="sendMessagePath"
  46. :send-message-img="sendMessageImg"
  47. :show-message-card="showMessageCard"
  48. :app-parameter="appParameter"
  49. >
  50. {{ value }}
  51. </button>
  52. </template>
  53. <text
  54. v-else
  55. class="u-text__value"
  56. :style="[valueStyle]"
  57. :class="[
  58. type && `u-text__value--${type}`,
  59. lines && `u-line-${lines}`
  60. ]"
  61. >{{ value }}</text
  62. >
  63. <view class="u-text__suffix-icon" v-if="suffixIcon">
  64. <u-icon
  65. :name="suffixIcon"
  66. :customStyle="$u.addStyle(iconStyle)"
  67. ></u-icon>
  68. </view>
  69. </view>
  70. </template>
  71. <script>
  72. import value from './value.js'
  73. import mpMixin from '../../libs/mixin/mpMixin.js';
  74. import mixin from '../../libs/mixin/mixin.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://ijry.github.io/uview-plus/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: [mpMixin, mixin, value, button, openType, props],
  110. // #endif
  111. // #ifndef MP
  112. mixins: [mpMixin, mixin, value, props],
  113. // #endif
  114. emits: ['click'],
  115. computed: {
  116. valueStyle() {
  117. const style = {
  118. textDecoration: this.decoration,
  119. fontWeight: this.bold ? 'bold' : 'normal',
  120. wordWrap: this.wordWrap,
  121. fontSize: uni.$u.addUnit(this.size)
  122. }
  123. !this.type && (style.color = this.color)
  124. this.isNvue && this.lines && (style.lines = this.lines)
  125. this.lineHeight &&
  126. (style.lineHeight = uni.$u.addUnit(this.lineHeight))
  127. !this.isNvue && this.block && (style.display = 'block')
  128. return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle))
  129. },
  130. isNvue() {
  131. let nvue = false
  132. // #ifdef APP-NVUE
  133. nvue = true
  134. // #endif
  135. return nvue
  136. },
  137. isMp() {
  138. let mp = false
  139. // #ifdef MP
  140. mp = true
  141. // #endif
  142. return mp
  143. }
  144. },
  145. data() {
  146. return {}
  147. },
  148. methods: {
  149. clickHandler() {
  150. // 如果为手机号模式,拨打电话
  151. if (this.call && this.mode === 'phone') {
  152. uni.makePhoneCall({
  153. phoneNumber: this.text
  154. })
  155. }
  156. this.$emit('click')
  157. }
  158. }
  159. }
  160. </script>
  161. <style lang="scss" scoped>
  162. @import '../../libs/css/components.scss';
  163. .u-text {
  164. @include flex(row);
  165. align-items: center;
  166. flex-wrap: nowrap;
  167. flex: 1;
  168. /* #ifndef APP-NVUE */
  169. width: 100%;
  170. /* #endif */
  171. &__price {
  172. font-size: 14px;
  173. color: $u-content-color;
  174. }
  175. &__value {
  176. font-size: 14px;
  177. @include flex;
  178. color: $u-content-color;
  179. flex-wrap: wrap;
  180. // flex: 1;
  181. text-overflow: ellipsis;
  182. align-items: center;
  183. &--primary {
  184. color: $u-primary;
  185. }
  186. &--warning {
  187. color: $u-warning;
  188. }
  189. &--success {
  190. color: $u-success;
  191. }
  192. &--info {
  193. color: $u-info;
  194. }
  195. &--error {
  196. color: $u-error;
  197. }
  198. &--main {
  199. color: $u-main-color;
  200. }
  201. &--content {
  202. color: $u-content-color;
  203. }
  204. &--tips {
  205. color: $u-tips-color;
  206. }
  207. &--light {
  208. color: $u-light-color;
  209. }
  210. }
  211. }
  212. </style>