u-cell-item.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <template>
  2. <view
  3. @tap="click"
  4. class="u-cell"
  5. :class="{ 'u-cell-border': itemIndex > 0 && borderBottom, 'u-col-center': center, 'u-border-gap': borderGap, 'u-cell--required': required }"
  6. hover-stay-time="150"
  7. :hover-class="hoverClass"
  8. :style="{
  9. backgroundColor: bgColor
  10. }"
  11. >
  12. <u-icon :size="iconSize" :name="icon" v-if="icon" class="u-cell__left-icon-wrap"></u-icon>
  13. <view class="u-flex" v-else>
  14. <slot name="icon"></slot>
  15. </view>
  16. <view
  17. class="u-cell_title"
  18. :style="[
  19. {
  20. width: titleWidth ? titleWidth + 'rpx' : 'auto'
  21. },
  22. titleStyle
  23. ]"
  24. >
  25. <block v-if="title">{{ title }}</block>
  26. <slot name="title" v-else></slot>
  27. <view class="u-cell__label" v-if="label || $slots.label" :style="[labelStyle]">
  28. <block v-if="label">{{ label }}</block>
  29. <slot name="label" v-else></slot>
  30. </view>
  31. </view>
  32. <view class="u-cell__value" :style="[valueStyle]">
  33. <block class="u-cell__value" v-if="value">{{ value }}</block>
  34. <slot v-else></slot>
  35. </view>
  36. <u-icon v-if="arrow" name="arrow-right" :style="[arrowStyle]" class="u-icon-wrap u-cell__right-icon-wrap"></u-icon>
  37. <view class="u-flex" v-else>
  38. <slot name="right-icon"></slot>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. /**
  44. * cellItem 单元格Item
  45. * @description cell单元格一般用于一组列表的情况,比如个人中心页,设置页等。搭配u-cell-group使用
  46. * @tutorial https://www.uviewui.com/components/cell.html
  47. * @property {String} title 左侧标题
  48. * @property {String} icon 左侧图标名,只支持uView内置图标,见Icon 图标
  49. * @property {String} value 右侧内容
  50. * @property {String} label 标题下方的描述信息
  51. * @property {Boolean} border-bottom 是否显示每个cell的下边框(默认true)
  52. * @property {Boolean} center 是否使内容垂直居中(默认false)
  53. * @property {String} hover-class 是否开启点击反馈,none为无效果(默认true)
  54. * @property {Boolean} border-gap border-bottom为true时,Cell列表中间的条目的下边框是否与左边有一个间隔(默认true)
  55. * @property {Boolean} arrow 是否显示右侧箭头(默认true)
  56. * @property {Boolean} required 箭头方向,可选值(默认right)
  57. * @property {Boolean} arrow-direction 是否显示左边表示必填的星号(默认false)
  58. * @property {Object} title-style 标题样式,对象形式
  59. * @property {Object} value-style 右侧内容样式,对象形式
  60. * @property {Object} label-style 标题下方描述信息的样式,对象形式
  61. * @property {String} bg-color 背景颜色(默认transparent)
  62. * @property {String Number} index 用于在click事件回调中返回,标识当前是第几个Item
  63. * @property {String Number} title-width 标题的宽度,单位rpx
  64. * @example <u-cell-item icon="integral-fill" title="会员等级" value="新版本"></u-cell-item>
  65. */
  66. export default {
  67. name: 'u-cell-item',
  68. props: {
  69. // 左侧图标名称(只能uView内置图标),或者图标src
  70. icon: {
  71. type: String,
  72. default: ''
  73. },
  74. // 左侧标题
  75. title: {
  76. type: [String, Number],
  77. default: ''
  78. },
  79. // 右侧内容
  80. value: {
  81. type: [String, Number],
  82. default: ''
  83. },
  84. // 标题下方的描述信息
  85. label: {
  86. type: [String, Number],
  87. default: ''
  88. },
  89. // 是否显示内边框
  90. borderBottom: {
  91. type: Boolean,
  92. default: true
  93. },
  94. // 多个cell中,中间的cell显示下划线时,下划线是否给一个到左边的距离
  95. borderGap: {
  96. type: Boolean,
  97. default: true
  98. },
  99. // 是否开启点击反馈,即点击时cell背景为灰色,none为无效果
  100. hoverClass: {
  101. type: String,
  102. default: 'u-cell-hover'
  103. },
  104. // 是否显示右侧箭头
  105. arrow: {
  106. type: Boolean,
  107. default: true
  108. },
  109. // 内容是否垂直居中
  110. center: {
  111. type: Boolean,
  112. default: false
  113. },
  114. // 是否显示左边表示必填的星号
  115. required: {
  116. type: Boolean,
  117. default: false
  118. },
  119. // 标题的宽度,单位rpx
  120. titleWidth: {
  121. type: [Number, String],
  122. default: ''
  123. },
  124. // 右侧箭头方向,可选值:right|up|down,默认为right
  125. arrowDirection: {
  126. type: String,
  127. default: 'right'
  128. },
  129. // 控制标题的样式
  130. titleStyle: {
  131. type: Object,
  132. default() {
  133. return {};
  134. }
  135. },
  136. // 右侧显示内容的样式
  137. valueStyle: {
  138. type: Object,
  139. default() {
  140. return {};
  141. }
  142. },
  143. // 描述信息的样式
  144. labelStyle: {
  145. type: Object,
  146. default() {
  147. return {};
  148. }
  149. },
  150. // 背景颜色
  151. bgColor: {
  152. type: String,
  153. default: 'transparent'
  154. },
  155. // 用于识别被点击的是第几个cell
  156. index: {
  157. type: [String, Number],
  158. default: ''
  159. },
  160. // 是否使用lable插槽
  161. useLabelSlot: {
  162. type: Boolean,
  163. default: false
  164. },
  165. // 左边图标的大小,单位rpx,只对传入icon字段时有效
  166. iconSize: {
  167. type: [Number, String],
  168. default: 34
  169. }
  170. },
  171. inject: {
  172. uCellGroup: {
  173. // 添加默认值,是为了能让u-cell-item组件无需u-cell-group组件嵌套亦可单独使用
  174. default() {
  175. return {
  176. index: 0
  177. }
  178. }
  179. }
  180. },
  181. data() {
  182. return {
  183. itemIndex: 0
  184. };
  185. },
  186. created() {
  187. this.itemIndex = this.uCellGroup.index++;
  188. },
  189. computed: {
  190. arrowStyle() {
  191. let style = {};
  192. if (this.arrowDirection == 'up') style.transform = 'rotate(-90deg)';
  193. else if (this.arrowDirection == 'down') style.transform = 'rotate(90deg)';
  194. else style.transform = 'rotate(0deg)';
  195. return style;
  196. }
  197. },
  198. methods: {
  199. click() {
  200. this.$emit('click', this.index);
  201. }
  202. }
  203. };
  204. </script>
  205. <style lang="scss" scoped>
  206. .u-cell {
  207. position: relative;
  208. display: flex;
  209. box-sizing: border-box;
  210. width: 100%;
  211. padding: 20rpx 32rpx;
  212. font-size: 28rpx;
  213. line-height: 48rpx;
  214. color: $u-content-color;
  215. background-color: #fff;
  216. text-align: left;
  217. }
  218. .u-cell_title {
  219. font-size: 28rpx;
  220. }
  221. .u-cell__left-icon-wrap {
  222. margin-right: 10rpx;
  223. font-size: 32rpx;
  224. }
  225. .u-cell__right-icon-wrap {
  226. margin-left: 10rpx;
  227. color: #969799;
  228. font-size: 28rpx;
  229. }
  230. .u-cell__left-icon-wrap,
  231. .u-cell__right-icon-wrap {
  232. display: flex;
  233. align-items: center;
  234. height: 48rpx;
  235. }
  236. .u-cell-border:after {
  237. position: absolute;
  238. box-sizing: border-box;
  239. content: ' ';
  240. pointer-events: none;
  241. right: 0;
  242. left: 0;
  243. top: 0;
  244. border-bottom: 1px solid $u-border-color;
  245. -webkit-transform: scaleY(0.5);
  246. transform: scaleY(0.5);
  247. }
  248. .u-cell-border {
  249. position: relative;
  250. }
  251. .u-border-gap:after {
  252. left: 32rpx !important;
  253. }
  254. .u-cell__label {
  255. margin-top: 6rpx;
  256. font-size: 26rpx;
  257. line-height: 36rpx;
  258. color: $u-tips-color;
  259. }
  260. .u-cell__value {
  261. overflow: hidden;
  262. text-align: right;
  263. vertical-align: middle;
  264. color: $u-tips-color;
  265. font-size: 26rpx;
  266. }
  267. .u-cell__title,
  268. .u-cell__value {
  269. flex: 1;
  270. }
  271. .u-cell--required {
  272. overflow: visible;
  273. display: flex;
  274. align-items: center;
  275. }
  276. .u-cell--required:before {
  277. position: absolute;
  278. content: '*';
  279. left: 8px;
  280. margin-top: 4rpx;
  281. font-size: 14px;
  282. color: $u-type-error;
  283. }
  284. </style>