u-cell.vue 8.3 KB

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