u-cell-item.vue 7.6 KB

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