Skip to content
This repository was archived by the owner on Oct 27, 2021. It is now read-only.

Commit 9709c03

Browse files
author
pengshanglong
committed
feat: add Grid
1 parent 43c3757 commit 9709c03

File tree

3 files changed

+167
-1
lines changed

3 files changed

+167
-1
lines changed

src/components/grid/index.jsx

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import classNames from 'classnames'
2+
import _chunk from 'lodash/chunk'
3+
4+
export default {
5+
name: 'Grid',
6+
props: {
7+
mode: {
8+
type: String,
9+
default: 'square',
10+
validator: (val) => ['rect', 'square'].includes(val),
11+
},
12+
hasBorder: {
13+
type: Boolean,
14+
default: true,
15+
},
16+
onClick: {
17+
type: Function,
18+
default: () => () => {},
19+
},
20+
columnNum: {
21+
type: Number,
22+
default: 3,
23+
},
24+
data: {
25+
type: Array,
26+
default: () => [],
27+
},
28+
className: {
29+
type: [Object, String],
30+
default: '',
31+
},
32+
},
33+
methods: {
34+
/**
35+
*
36+
* @param {AtGridItem} item
37+
* @param {number} index
38+
* @param {number} row
39+
* @param {event} event
40+
*/
41+
handleClick(item, index, row, event) {
42+
const { onClick, columnNum } = this.props
43+
if (typeof onClick === 'function') {
44+
const clickIndex = row * columnNum + index
45+
onClick(item, clickIndex, event)
46+
}
47+
},
48+
},
49+
render() {
50+
const { data, mode, columnNum, hasBorder } = this
51+
52+
if (Array.isArray(data) && data.length === 0) {
53+
return null
54+
}
55+
56+
const gridGroup = _chunk(data, columnNum)
57+
58+
const bodyClass = classNames(['at-grid__flex-item', 'at-grid-item', `at-grid-item--${mode}`], {
59+
'at-grid-item--no-border': !hasBorder,
60+
})
61+
62+
return (
63+
<view class={classNames('at-grid', this.className)}>
64+
{gridGroup.map((item, i) => (
65+
<view class="at-grid__flex" key={`at-grid-group-${i}`}>
66+
{item.map((childItem, index) => (
67+
<view
68+
key={`at-grid-item-${index}`}
69+
class={classNames(bodyClass, {
70+
'at-grid-item--last': index === columnNum - 1,
71+
})}
72+
onClick={this.handleClick.bind(this, childItem, index, i)}
73+
style={{
74+
flex: `0 0 ${100 / columnNum}%`,
75+
}}>
76+
<view class="at-grid-item__content">
77+
<view class="at-grid-item__content-inner">
78+
<view class="content-inner__icon">
79+
{childItem.image && (
80+
<image
81+
class="content-inner__img"
82+
src={childItem.image}
83+
mode="scaleToFill"
84+
/>
85+
)}
86+
{childItem.iconInfo && !childItem.image && (
87+
<view
88+
class={classNames(
89+
childItem.iconInfo.prefixClass || 'at-icon',
90+
{
91+
[`${childItem.iconInfo.prefixClass || 'at-icon'}-${
92+
childItem.iconInfo.value
93+
}`]: childItem.iconInfo.value,
94+
},
95+
childItem.iconInfo.className
96+
)}
97+
style={this.$mergeStyle(
98+
{
99+
color: childItem.iconInfo.color,
100+
fontSize: `${childItem.iconInfo.size || 24}px`,
101+
},
102+
childItem.iconInfo.customStyle
103+
)}
104+
/>
105+
)}
106+
</view>
107+
<view class="content-inner__text">{childItem.value}</view>
108+
</view>
109+
</view>
110+
</view>
111+
))}
112+
</view>
113+
))}
114+
</view>
115+
)
116+
},
117+
}

src/components/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Grid from './grid/index'
2+
3+
export { Grid }

src/pages/index/index.vue

+47-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22
<view
33
class="index"
44
>
5+
<Grid
6+
mode="square"
7+
:data="gridData"
8+
/>
9+
<view class="at-row">
10+
<view class="at-col at-col-3">
11+
A
12+
</view>
13+
<view class="at-col at-col-6">
14+
B
15+
</view>
16+
<view class="at-col at-col-2">
17+
C
18+
</view>
19+
<view class="at-col at-col-1">
20+
D
21+
</view>
22+
</view>
523
<Message />
624
<button @tap="showMessage">
725
show Message
@@ -164,6 +182,7 @@ import ModalAction from '../../components/modal/action/index.jsx'
164182
import Toast from '../../components/toast/index.jsx'
165183
import SwipeAction from '../../components/swipe-action/index.jsx'
166184
import Message from '../../components/message/index.jsx'
185+
import { Grid } from '../../components/index'
167186
168187
export default {
169188
name: 'Index',
@@ -190,6 +209,7 @@ export default {
190209
Toast,
191210
SwipeAction,
192211
Message,
212+
Grid,
193213
},
194214
data() {
195215
return {
@@ -208,7 +228,33 @@ export default {
208228
backgroundColor: '#FF4949'
209229
}
210230
}
211-
]
231+
],
232+
gridData: [
233+
{
234+
image: 'https://img12.360buyimg.com/jdphoto/s72x72_jfs/t6160/14/2008729947/2754/7d512a86/595c3aeeNa89ddf71.png',
235+
value: '领取中心'
236+
},
237+
{
238+
image: 'https://img20.360buyimg.com/jdphoto/s72x72_jfs/t15151/308/1012305375/2300/536ee6ef/5a411466N040a074b.png',
239+
value: '找折扣'
240+
},
241+
{
242+
image: 'https://img10.360buyimg.com/jdphoto/s72x72_jfs/t5872/209/5240187906/2872/8fa98cd/595c3b2aN4155b931.png',
243+
value: '领会员'
244+
},
245+
{
246+
image: 'https://img12.360buyimg.com/jdphoto/s72x72_jfs/t10660/330/203667368/1672/801735d7/59c85643N31e68303.png',
247+
value: '新品首发'
248+
},
249+
{
250+
image: 'https://img14.360buyimg.com/jdphoto/s72x72_jfs/t17251/336/1311038817/3177/72595a07/5ac44618Na1db7b09.png',
251+
value: '领京豆'
252+
},
253+
{
254+
image: 'https://img30.360buyimg.com/jdphoto/s72x72_jfs/t5770/97/5184449507/2423/294d5f95/595c3b4dNbc6bc95d.png',
255+
value: '手机馆'
256+
}
257+
]
212258
}
213259
},
214260
methods: {

0 commit comments

Comments
 (0)