u-cell.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <view class="u-cell" :class="[customClass]" :style="[$u.addStyle(customStyle)]"
  3. :hover-class="(!disabled && (clickable || isLink)) ? 'u-cell--clickable' : ''" :hover-stay-time="250"
  4. @tap="clickHandler">
  5. <view class="u-cell__body" :class="[ center && 'u-cell--center', size === 'large' && 'u-cell__body--large']">
  6. <view class="u-cell__body__content">
  7. <view class="u-cell__left-icon-wrap" v-if="$slots.icon || icon">
  8. <slot name="icon" v-if="$slots.icon">
  9. </slot>
  10. <u-icon v-else :name="icon"
  11. :custom-style="iconStyle"
  12. :size="size === 'large' ? 22 : 18"></u-icon>
  13. </view>
  14. <view class="u-cell__title">
  15. <slot name="title">
  16. <text v-if="title" class="u-cell__title-text" :style="[titleTextStyle]"
  17. :class="[disabled && 'u-cell--disabled', size === 'large' && 'u-cell__title-text--large']">{{ title }}</text>
  18. </slot>
  19. <slot name="label">
  20. <text class="u-cell__label" v-if="label"
  21. :class="[disabled && 'u-cell--disabled', size === 'large' && 'u-cell__label--large']">{{ label }}</text>
  22. </slot>
  23. </view>
  24. </view>
  25. <slot name="value">
  26. <text class="u-cell__value"
  27. :class="[disabled && 'u-cell--disabled', size === 'large' && 'u-cell__value--large']"
  28. v-if="!$u.test.empty(value)">{{ value }}</text>
  29. </slot>
  30. <view class="u-cell__right-icon-wrap" v-if="$slots['right-icon'] || isLink"
  31. :class="[`u-cell__right-icon-wrap--${arrowDirection}`]">
  32. <slot name="right-icon" v-if="$slots['right-icon']">
  33. </slot>
  34. <u-icon v-else :name="rightIcon" :custom-style="rightIconStyle" :color="disabled ? '#c8c9cc' : 'info'"
  35. :size="size === 'large' ? 18 : 16"></u-icon>
  36. </view>
  37. </view>
  38. <u-line v-if="border"></u-line>
  39. </view>
  40. </template>
  41. <script>
  42. import props from './props.js';
  43. import mpMixin from '../../libs/mixin/mpMixin.js';
  44. import mixin from '../../libs/mixin/mixin.js';
  45. /**
  46. * cell 单元格
  47. * @description cell单元格一般用于一组列表的情况,比如个人中心页,设置页等。
  48. * @tutorial https://uiadmin.net/uview-plus/components/cell.html
  49. * @property {String | Number} title 标题
  50. * @property {String | Number} label 标题下方的描述信息
  51. * @property {String | Number} value 右侧的内容
  52. * @property {String} icon 左侧图标名称,或者图片链接(本地文件建议使用绝对地址)
  53. * @property {Boolean} disabled 是否禁用cell
  54. * @property {Boolean} border 是否显示下边框 (默认 true )
  55. * @property {Boolean} center 内容是否垂直居中(主要是针对右侧的value部分) (默认 false )
  56. * @property {String} url 点击后跳转的URL地址
  57. * @property {String} linkType 链接跳转的方式,内部使用的是uView封装的route方法,可能会进行拦截操作 (默认 'navigateTo' )
  58. * @property {Boolean} clickable 是否开启点击反馈(表现为点击时加上灰色背景) (默认 false )
  59. * @property {Boolean} isLink 是否展示右侧箭头并开启点击反馈 (默认 false )
  60. * @property {Boolean} required 是否显示表单状态下的必填星号(此组件可能会内嵌入input组件) (默认 false )
  61. * @property {String} rightIcon 右侧的图标箭头 (默认 'arrow-right')
  62. * @property {String} arrowDirection 右侧箭头的方向,可选值为:left,up,down
  63. * @property {Object | String} rightIconStyle 右侧箭头图标的样式
  64. * @property {Object | String} titleStyle 标题的样式
  65. * @property {Object | String} iconStyle 左侧图标样式
  66. * @property {String} size 单位元的大小,可选值为 large,normal,mini
  67. * @property {Boolean} stop 点击cell是否阻止事件传播 (默认 true )
  68. * @property {Object} customStyle 定义需要用到的外部样式
  69. *
  70. * @event {Function} click 点击cell列表时触发
  71. * @example 该组件需要搭配cell-group组件使用,见官方文档示例
  72. */
  73. export default {
  74. name: 'u-cell',
  75. data() {
  76. return {
  77. }
  78. },
  79. mixins: [mpMixin, mixin, props],
  80. computed: {
  81. titleTextStyle() {
  82. return uni.$u.addStyle(this.titleStyle)
  83. }
  84. },
  85. emits: ['click'],
  86. methods: {
  87. // 点击cell
  88. clickHandler(e) {
  89. if (this.disabled) return
  90. this.$emit('click', {
  91. name: this.name
  92. })
  93. // 如果配置了url(此props参数通过mixin引入)参数,跳转页面
  94. this.openPage()
  95. // 是否阻止事件传播
  96. this.stop && this.preventEvent(e)
  97. },
  98. }
  99. }
  100. </script>
  101. <style lang="scss" scoped>
  102. @import "../../libs/css/components.scss";
  103. $u-cell-padding: 10px 15px !default;
  104. $u-cell-font-size: 15px !default;
  105. $u-cell-line-height: 24px !default;
  106. $u-cell-color: $u-main-color !default;
  107. $u-cell-icon-size: 16px !default;
  108. $u-cell-title-font-size: 15px !default;
  109. $u-cell-title-line-height: 22px !default;
  110. $u-cell-title-color: $u-main-color !default;
  111. $u-cell-label-font-size: 12px !default;
  112. $u-cell-label-color: $u-tips-color !default;
  113. $u-cell-label-line-height: 18px !default;
  114. $u-cell-value-font-size: 14px !default;
  115. $u-cell-value-color: $u-content-color !default;
  116. $u-cell-clickable-color: $u-bg-color !default;
  117. $u-cell-disabled-color: #c8c9cc !default;
  118. $u-cell-padding-top-large: 13px !default;
  119. $u-cell-padding-bottom-large: 13px !default;
  120. $u-cell-value-font-size-large: 15px !default;
  121. $u-cell-label-font-size-large: 14px !default;
  122. $u-cell-title-font-size-large: 16px !default;
  123. $u-cell-left-icon-wrap-margin-right: 4px !default;
  124. $u-cell-right-icon-wrap-margin-left: 4px !default;
  125. $u-cell-title-flex:1 !default;
  126. $u-cell-label-margin-top:5px !default;
  127. .u-cell {
  128. &__body {
  129. @include flex();
  130. /* #ifndef APP-NVUE */
  131. box-sizing: border-box;
  132. /* #endif */
  133. padding: $u-cell-padding;
  134. font-size: $u-cell-font-size;
  135. color: $u-cell-color;
  136. // line-height: $u-cell-line-height;
  137. align-items: center;
  138. &__content {
  139. @include flex(row);
  140. align-items: center;
  141. flex: 1;
  142. }
  143. &--large {
  144. padding-top: $u-cell-padding-top-large;
  145. padding-bottom: $u-cell-padding-bottom-large;
  146. }
  147. }
  148. &__left-icon-wrap,
  149. &__right-icon-wrap {
  150. @include flex();
  151. align-items: center;
  152. // height: $u-cell-line-height;
  153. font-size: $u-cell-icon-size;
  154. }
  155. &__left-icon-wrap {
  156. margin-right: $u-cell-left-icon-wrap-margin-right;
  157. }
  158. &__right-icon-wrap {
  159. margin-left: $u-cell-right-icon-wrap-margin-left;
  160. transition: transform 0.3s;
  161. &--up {
  162. transform: rotate(-90deg);
  163. }
  164. &--down {
  165. transform: rotate(90deg);
  166. }
  167. }
  168. &__title {
  169. flex: $u-cell-title-flex;
  170. &-text {
  171. font-size: $u-cell-title-font-size;
  172. line-height: $u-cell-title-line-height;
  173. color: $u-cell-title-color;
  174. &--large {
  175. font-size: $u-cell-title-font-size-large;
  176. }
  177. }
  178. }
  179. &__label {
  180. margin-top: $u-cell-label-margin-top;
  181. font-size: $u-cell-label-font-size;
  182. color: $u-cell-label-color;
  183. line-height: $u-cell-label-line-height;
  184. &--large {
  185. font-size: $u-cell-label-font-size-large;
  186. }
  187. }
  188. &__value {
  189. text-align: right;
  190. font-size: $u-cell-value-font-size;
  191. line-height: $u-cell-line-height;
  192. color: $u-cell-value-color;
  193. &--large {
  194. font-size: $u-cell-value-font-size-large;
  195. }
  196. }
  197. &--clickable {
  198. background-color: $u-cell-clickable-color;
  199. }
  200. &--disabled {
  201. color: $u-cell-disabled-color;
  202. /* #ifndef APP-NVUE */
  203. cursor: not-allowed;
  204. /* #endif */
  205. }
  206. &--center {
  207. align-items: center;
  208. }
  209. }
  210. </style>