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