u-text.vue 7.7 KB

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