u-cell-item.vue 7.3 KB

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