u-cell-item.vue 7.5 KB

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