Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

288 linhas
8.9 KiB

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