# Grid 宫格

### 介绍

宫格可以在水平方向上把页面分隔成等宽度的区块，用于展示内容或进行页面导航

### 引入

在`index.config.js中引入组件

```json
"usingComponents": {
  "van-grid": "@vant/weapp/grid/index",
  "van-grid-item": "@vant/weapp/grid-item/index"
}
```



## 代码演示

### 基本用法

通过`icon`属性设置格子内的图标，`text`属性设置文字内容

```html
<van-grid>
  <van-grid-item icon="photo-o" text="文字" />
  <van-grid-item icon="photo-o" text="文字" />
  <van-grid-item icon="photo-o" text="文字" />
  <van-grid-item icon="photo-o" text="文字" />
</van-grid>
```

### 自定义列数

默认一行展示四个格子，可以通过`column-num`自定义列数

```html
 <van-grid columnNum="3">
            {
              this.state.array6.map((item, index) => {
                return (
                  <van-grid-item icon="photo-o" text="文字" key={item}/>
                )
              })
            }
</van-grid>
```

```javascript

    state = {
      array3: [0, 1, 2],
      array4: [0, 1, 2, 3],
      array6: [0, 1, 2, 3, 4, 5],
      array8: [0, 1, 2, 3, 4, 5, 6, 7],
    };

```


### 自定义内容

通过插槽可以自定义格子展示的内容

```html
<van-grid columnNum="3" border={false}>
            {
              this.state.array3.map((item, index) => {
                return (
                  <van-grid-item icon="photo-o" text="文字" key={item}>
                    <image style="width: 100%; height: 90px;"
                           src={'https://img.yzcdn.cn/vant/apple-'+index + 1 +'.jpg'}/>
                  </van-grid-item>
                )
              })
            }
</van-grid>
```

### 正方形格子

设置`square`属性后，格子的高度会和宽度保持一致

```html
 <van-grid square>
            {
              this.state.array8.map((item, index) => {
                return (
                  <van-grid-item icon="photo-o" text="文字" key={item}/>
                )
              })
            }
</van-grid>
```

### 格子间距

通过`gutter`属性设置格子之间的距离

```html
<van-grid gutter={10} columnNum="4">
            {
              this.state.array8.map((item, index) => {
                return (
                  <van-grid-item icon="photo-o" text="文字" key={item}/>
                )
              })
            }
</van-grid>
```

### 内容横排

将`direction`属性设置为`horizontal`，可以让宫格的内容呈横向排列

```html
<van-grid direction="horizontal" columnNum="3">
            {
              this.state.array3.map((item, index) => {
                return (
                  <van-grid-item icon="photo-o" text="文字" key={item}/>
                )
              })
            }
</van-grid>
```

### 页面跳转

可以通过`url`属性进行页面跳转，通过`link-type`属性控制跳转类型

```html
<van-grid clickable column-num="2">
    <van-grid-item icon="home-o" linkType="navigateTo" url="/pages/dashboard/index" text="Navigate 跳转"/>
    <van-grid-item icon="search" linkType="reLaunch" url="/pages/dashboard/index" text="ReLaunch 跳转"/>
</van-grid>
```

### 提示信息

设置`dot`属性后，会在图标右上角展示一个小红点。设置`badge`属性后，会在图标右上角展示相应的徽标

```html
  <van-grid columnNum="2">
            <van-grid-item icon="home-o" text="文字" dot/>
            <van-grid-item icon="search" text="文字" badge={"99+"}/>
  </van-grid>
```

## API
### 注:属性应用在组件上需要统一使用驼峰式命名，如custom-class应变为
```html
<van-button type="primary" customClass='myClass' size="large">大号按钮</van-button>
```
### 注:事件应用在组件上需要统一使用驼峰式命名，如bind:click/bind:search/bind:before-enter应变为
```html
<van-button type="primary" onClick={()=>this.onClick()}>大号按钮</van-button>
<van-search type="primary" onSearch={()=>this.onSearch()}>大号按钮</van-search>
<van-transition type="primary" onBeforeEnter={()=>this.onBeforeEnter()}>大号按钮</van-transition>
```
### Grid Props

| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| column-num | 列数 | _number_ | `4` | - |
| icon-size `v1.3.2` | 图标大小，默认单位为`px` | _string_ | `28px` |
| gutter | 格子之间的间距，默认单位为`px` | _string \| number_ | `0` | - |
| border | 是否显示边框 | _boolean_ | `true` | - |
| center | 是否将格子内容居中显示 | _boolean_ | `true` | - |
| square | 是否将格子固定为正方形 | _boolean_ | `false` | - |
| clickable | 是否开启格子点击反馈 | _boolean_ | `false` | - |
| direction | 格子内容排列的方向，可选值为 `horizontal` | _string_ | `vertical` | - |
| use-slot | 是否使用自定义内容的插槽 | _boolean_ | `false` |

### Grid 外部样式类

| 类名         | 说明         |
| ------------ | ------------ |
| custom-class | 根节点样式类 |

### GridItem Props

| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| text | 文字 | _string_ | - | - |
| icon | 图标名称或图片链接，可选值见 [Icon 组件](#/icon) | _string_ | - | - |
| icon-color | 图标颜色 | _string_ | - | - |
| dot | 是否显示图标右上角小红点 | _boolean_ | `false` | - |
| badge | 图标右上角徽标的内容 | _string \| number_ | - | - |
| url | 点击后跳转的链接地址 | _string_ | - | - |
| link-type | 链接跳转类型，可选值为 `redirectTo` `switchTab` `reLaunch` | _string_ | `navigateTo` | - |

### GridItem Events

| 事件名     | 说明           | 回调参数 |
| ---------- | -------------- | -------- |
| bind:click | 点击格子时触发 | -        |

### GridItem Slots

| 名称 | 说明                                                   |
| ---- | ------------------------------------------------------ |
| -    | 自定义宫格的所有内容，需要设置`use-slot`属性           |
| icon | 自定义图标，如果设置了`use-slot`或者`icon`属性则不生效 |
| text | 自定义文字，如果设置了`use-slot`或者`text`属性则不生效 |

### GridItem 外部样式类

| 类名          | 说明         |
| ------------- | ------------ |
| custom-class  | 根节点样式类 |
| content-class | 内容样式类   |
| icon-class    | 图标样式类   |
| text-class    | 文本样式类   |
