u-section.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <view class="u-section">
  3. <view class="u-section__title" :style="uStyle" :class="uClasses">
  4. <slot name="left">
  5. <view class="u-section__title__icon-wrap" :style="[lineStyle]" v-if="showLine">
  6. <u-icon name="column-line" :size="fontSize * 1.25" bold :color="lineColor ? lineColor : color"></u-icon>
  7. </view>
  8. </slot>
  9. <text class="u-section__title__text">{{title}}</text>
  10. </view>
  11. <view class="u-section__right-info" v-if="right || $slots.right" :style="{
  12. color: subColor
  13. }" @tap="rightClick">
  14. <slot name="right">
  15. {{subTitle}}
  16. <view class="u-section__right-info__icon-arrow" v-if="arrow">
  17. <u-icon name="arrow-right" size="12" :color="subColor"></u-icon>
  18. </view>
  19. </slot>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import props from './props.js';
  25. import mixin from '../../libs/mixin/mixin'
  26. import mpMixin from '../../libs/mixin/mpMixin';
  27. /**
  28. * section 查看更多
  29. * @description 该组件一般用于分类信息有很多,但是限于篇幅只能列出一部分,让用户通过"查看更多"获得更多信息的场景,实际效果见演示。
  30. * @tutorial https://uview.d3u.cn/components/search.html
  31. * @property {String} title 左边主标题
  32. * @property {String} sub-title 右边副标题(默认更多)
  33. * @property {Boolean} right 是否显示右边的内容(默认true)
  34. * @property {Boolean} showLine 是否显示左边的竖条(默认true)
  35. * @property {Boolean} arrow 是否显示右边箭头(默认true)
  36. * @property {String Number} font-size 主标题的字体大小(默认28)
  37. * @property {Boolean} bold 主标题是否加粗(默认true)
  38. * @property {String} color 主标题颜色(默认#303133)
  39. * @event {Function} click 组件右侧的内容被点击时触发,用于跳转"更多"
  40. * @example <u-section title="今日热门" :right="false"></u-section>
  41. */
  42. export default {
  43. name: "u-section",
  44. mixins: [mpMixin, mixin, props],
  45. computed: {
  46. uClasses() {
  47. let classes = [];
  48. if(this.showLine){
  49. classes.push('u-section--line');
  50. }
  51. // 主题色,通过类配置
  52. if (this.color && uni.$u.config.type.includes(this.color)) classes.push('u-section__title--' + this.color)
  53. //#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
  54. classes = classes.join(' ')
  55. //#endif
  56. return classes
  57. },
  58. uStyle() {
  59. let style = {
  60. fontWeight: this.bold ? 'bold' : 'normal',
  61. fontSize: uni.$u.addUnit(this.fontSize),
  62. paddingLeft: this.showLine ? uni.$u.addUnit(this.fontSize * 0.7) : 0
  63. };
  64. if (this.color && !uni.$u.config.type.includes(this.color)) style.color = this.color;
  65. return style;
  66. },
  67. // 左边竖条的样式
  68. lineStyle() {
  69. // 由于安卓和iOS的,需要稍微调整绝对定位的top值,才能让左边的竖线和右边的文字垂直居中
  70. return {
  71. // 由于竖线为字体图标,具有比实际线宽更宽的宽度,所以也需要根据字体打下动态调整
  72. left: uni.$u.addUnit(-(Number(this.fontSize) * 0.5)),
  73. top: uni.$u.addUnit(-(Number(this.fontSize) * (uni.$u.os() == 'ios' ? 0.14 : 0.15))),
  74. }
  75. }
  76. },
  77. // #ifdef VUE3
  78. emits: [ 'click'],
  79. // #endif
  80. methods: {
  81. rightClick() {
  82. this.$emit('click');
  83. }
  84. }
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. @import "../../libs/css/components.scss";
  89. .u-section {
  90. @include flex;
  91. justify-content: space-between;
  92. align-items: center;
  93. width: 100%;
  94. &__title {
  95. position: relative;
  96. font-size: 14px;
  97. padding-left: 10px;
  98. @include flex;
  99. align-items: center;
  100. &__icon-wrap {
  101. position: absolute;
  102. }
  103. &__text {
  104. line-height: 1;
  105. }
  106. &--primary {
  107. color: $u-primary;
  108. }
  109. &--success {
  110. color: $u-success;
  111. }
  112. &--error {
  113. color: $u-error;
  114. }
  115. &--warning {
  116. color: $u-warning;
  117. }
  118. &--info {
  119. color: $u-info;
  120. }
  121. }
  122. &__right-info {
  123. color: $u-tips-color;
  124. font-size: 13px;
  125. @include flex;
  126. align-items: center;
  127. &__icon-arrow {
  128. margin-left: 3px;
  129. }
  130. }
  131. }
  132. </style>