u-cell.vue 9.5 KB

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