u-cell.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. @import "../../libs/css/components.scss";
  114. $u-cell-padding: 13px 15px !default;
  115. $u-cell-font-size: 15px !default;
  116. $u-cell-line-height: 24px !default;
  117. $u-cell-color: $u-main-color !default;
  118. $u-cell-icon-size: 16px !default;
  119. $u-cell-title-font-size: 15px !default;
  120. $u-cell-title-line-height: 22px !default;
  121. $u-cell-title-color: $u-main-color !default;
  122. $u-cell-label-font-size: 12px !default;
  123. $u-cell-label-color: $u-tips-color !default;
  124. $u-cell-label-line-height: 18px !default;
  125. $u-cell-value-font-size: 14px !default;
  126. $u-cell-value-color: $u-content-color !default;
  127. $u-cell-clickable-color: $u-bg-color !default;
  128. $u-cell-disabled-color: #c8c9cc !default;
  129. $u-cell-padding-top-large: 13px !default;
  130. $u-cell-padding-bottom-large: 13px !default;
  131. $u-cell-value-font-size-large: 15px !default;
  132. $u-cell-label-font-size-large: 14px !default;
  133. $u-cell-title-font-size-large: 16px !default;
  134. $u-cell-left-icon-wrap-margin-right: 4px !default;
  135. $u-cell-right-icon-wrap-margin-left: 4px !default;
  136. $u-cell-title-flex:1 !default;
  137. $u-cell-label-margin-top:5px !default;
  138. .u-cell {
  139. &__body {
  140. @include flex();
  141. /* #ifndef APP-NVUE */
  142. box-sizing: border-box;
  143. /* #endif */
  144. padding: $u-cell-padding;
  145. font-size: $u-cell-font-size;
  146. color: $u-cell-color;
  147. // line-height: $u-cell-line-height;
  148. align-items: center;
  149. &__content {
  150. @include flex(row);
  151. align-items: center;
  152. flex: 1;
  153. }
  154. &--large {
  155. padding-top: $u-cell-padding-top-large;
  156. padding-bottom: $u-cell-padding-bottom-large;
  157. }
  158. }
  159. &__left-icon-wrap,
  160. &__right-icon-wrap {
  161. @include flex();
  162. align-items: center;
  163. // height: $u-cell-line-height;
  164. font-size: $u-cell-icon-size;
  165. }
  166. &__left-icon-wrap {
  167. margin-right: $u-cell-left-icon-wrap-margin-right;
  168. }
  169. &__right-icon-wrap {
  170. margin-left: $u-cell-right-icon-wrap-margin-left;
  171. transition: transform 0.3s;
  172. &--up {
  173. transform: rotate(-90deg);
  174. }
  175. &--down {
  176. transform: rotate(90deg);
  177. }
  178. }
  179. &__title {
  180. flex: $u-cell-title-flex;
  181. display: flex;
  182. flex-direction: column;
  183. &-text {
  184. font-size: $u-cell-title-font-size;
  185. line-height: $u-cell-title-line-height;
  186. color: $u-cell-title-color;
  187. &--large {
  188. font-size: $u-cell-title-font-size-large;
  189. }
  190. }
  191. }
  192. &__label {
  193. margin-top: $u-cell-label-margin-top;
  194. font-size: $u-cell-label-font-size;
  195. color: $u-cell-label-color;
  196. line-height: $u-cell-label-line-height;
  197. &--large {
  198. font-size: $u-cell-label-font-size-large;
  199. }
  200. }
  201. &__value {
  202. text-align: right;
  203. /* #ifndef APP-NVUE */
  204. margin-left: auto;
  205. /* #endif */
  206. font-size: $u-cell-value-font-size;
  207. line-height: $u-cell-line-height;
  208. color: $u-cell-value-color;
  209. &--large {
  210. font-size: $u-cell-value-font-size-large;
  211. }
  212. }
  213. &--required {
  214. /* #ifndef APP-NVUE */
  215. overflow: visible;
  216. /* #endif */
  217. @include flex;
  218. align-items: center;
  219. }
  220. &--required:before {
  221. position: absolute;
  222. /* #ifndef APP-NVUE */
  223. content: '*';
  224. /* #endif */
  225. left: -8px;
  226. margin-top: 4rpx;
  227. font-size: 14px;
  228. color: $u-error;
  229. }
  230. &--clickable {
  231. background-color: $u-cell-clickable-color;
  232. }
  233. &--disabled {
  234. color: $u-cell-disabled-color;
  235. /* #ifndef APP-NVUE */
  236. cursor: not-allowed;
  237. /* #endif */
  238. }
  239. &--center {
  240. align-items: center;
  241. }
  242. }
  243. </style>