guangchao.xu | 070005a | 2020-12-07 09:56:40 +0800 | [diff] [blame^] | 1 | <template> |
| 2 | <view class="u-grid-item" :hover-class="parentData.hoverClass" |
| 3 | :hover-stay-time="200" @tap="click" :style="{ |
| 4 | background: bgColor, |
| 5 | width: width, |
| 6 | }"> |
| 7 | <view class="u-grid-item-box" :style="[customStyle]" :class="[parentData.border ? 'u-border-right u-border-bottom' : '']"> |
| 8 | <slot /> |
| 9 | </view> |
| 10 | </view> |
| 11 | </template> |
| 12 | |
| 13 | <script> |
| 14 | /** |
| 15 | * gridItem 提示 |
| 16 | * @description 宫格组件一般用于同时展示多个同类项目的场景,可以给宫格的项目设置徽标组件(badge),或者图标等,也可以扩展为左右滑动的轮播形式。搭配u-grid使用 |
| 17 | * @tutorial https://www.uviewui.com/components/grid.html |
| 18 | * @property {String} bg-color 宫格的背景颜色(默认#ffffff) |
| 19 | * @property {String Number} index 点击宫格时,返回的值 |
| 20 | * @property {Object} custom-style 自定义样式,对象形式 |
| 21 | * @event {Function} click 点击宫格触发 |
| 22 | * @example <u-grid-item></u-grid-item> |
| 23 | */ |
| 24 | export default { |
| 25 | name: "u-grid-item", |
| 26 | props: { |
| 27 | // 背景颜色 |
| 28 | bgColor: { |
| 29 | type: String, |
| 30 | default: '#ffffff' |
| 31 | }, |
| 32 | // 点击时返回的index |
| 33 | index: { |
| 34 | type: [Number, String], |
| 35 | default: '' |
| 36 | }, |
| 37 | // 自定义样式,对象形式 |
| 38 | customStyle: { |
| 39 | type: Object, |
| 40 | default() { |
| 41 | return { |
| 42 | padding: '30rpx 0' |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | }, |
| 47 | data() { |
| 48 | return { |
| 49 | parentData: { |
| 50 | hoverClass: '', // 按下去的时候,是否显示背景灰色 |
| 51 | col: 3, // 父组件划分的宫格数 |
| 52 | border: true, // 是否显示边框,根据父组件决定 |
| 53 | } |
| 54 | }; |
| 55 | }, |
| 56 | created() { |
| 57 | // 父组件的实例 |
| 58 | this.updateParentData(); |
| 59 | // this.parent在updateParentData()中定义 |
| 60 | this.parent.children.push(this); |
| 61 | }, |
| 62 | computed: { |
| 63 | // 每个grid-item的宽度 |
| 64 | width() { |
| 65 | return 100 / Number(this.parentData.col) + '%'; |
| 66 | }, |
| 67 | }, |
| 68 | methods: { |
| 69 | // 获取父组件的参数 |
| 70 | updateParentData() { |
| 71 | // 此方法写在mixin中 |
| 72 | this.getParentData('u-grid'); |
| 73 | }, |
| 74 | click() { |
| 75 | this.$emit('click', this.index); |
| 76 | this.parent && this.parent.click(this.index); |
| 77 | } |
| 78 | } |
| 79 | }; |
| 80 | </script> |
| 81 | |
| 82 | <style scoped lang="scss"> |
| 83 | @import "../../libs/css/style.components.scss"; |
| 84 | |
| 85 | .u-grid-item { |
| 86 | box-sizing: border-box; |
| 87 | background: #fff; |
| 88 | @include vue-flex; |
| 89 | align-items: center; |
| 90 | justify-content: center; |
| 91 | position: relative; |
| 92 | flex-direction: column; |
| 93 | |
| 94 | /* #ifdef MP */ |
| 95 | position: relative; |
| 96 | float: left; |
| 97 | /* #endif */ |
| 98 | } |
| 99 | |
| 100 | .u-grid-item-hover { |
| 101 | background: #f7f7f7 !important; |
| 102 | } |
| 103 | |
| 104 | .u-grid-marker-box { |
| 105 | position: absolute; |
| 106 | /* #ifndef APP-NVUE */ |
| 107 | display: inline-flex; |
| 108 | /* #endif */ |
| 109 | line-height: 0; |
| 110 | } |
| 111 | |
| 112 | .u-grid-marker-wrap { |
| 113 | position: absolute; |
| 114 | } |
| 115 | |
| 116 | .u-grid-item-box { |
| 117 | padding: 30rpx 0; |
| 118 | @include vue-flex; |
| 119 | align-items: center; |
| 120 | justify-content: center; |
| 121 | flex-direction: column; |
| 122 | flex: 1; |
| 123 | width: 100%; |
| 124 | height: 100%; |
| 125 | } |
| 126 | </style> |