u-cell-item.vue 7.8 KB

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