u-text.vue 7.6 KB

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