# Tabbar 标签栏

### 引入

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

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

## 代码演示

### 基础用法

```html
<van-tabbar  active={this.state.active}
             dataKey="active"
             customClass="tabbar-position"
             safeAreaInsetBottom={false}
             onChange={(e) => this.onChange(e)}
>
  <van-tabbar-item icon="home-o">标签</van-tabbar-item>
  <van-tabbar-item icon="search">标签</van-tabbar-item>
  <van-tabbar-item icon="friends-o">标签</van-tabbar-item>
  <van-tabbar-item icon="setting-o">标签</van-tabbar-item>
</van-tabbar>
```

```javascript

  state= {
    active: 0,
  };
  onChange(event) {
    // event.detail 的值为当前选中项的索引
    this.setState({ active: event.detail });
  }

```

### 通过名称匹配

在标签指定`name`属性的情况下，`v-model`的值为当前标签的`name`

```html
<van-tabbar   active={this.state.active2}
              data-key="active2"
              customClass="tabbar-position"
              safeAreaInsetBottom={false}
              onChange={(e) => this.onChange(e)}>
  <van-tabbar-item name="home" icon="home-o">标签</van-tabbar-item>
  <van-tabbar-item name="search" icon="search">标签</van-tabbar-item>
  <van-tabbar-item name="friends" icon="friends-o">标签</van-tabbar-item>
  <van-tabbar-item name="setting" icon="setting-o">标签</van-tabbar-item>
</van-tabbar>
```

```javascript

  state= {
    active: 'home',
  }
  onChange(event) {
    this.setState({ active: event.detail });
  },

```

### 显示徽标

```html
<van-tabbar  active={this.state.active3}
             data-key="active3"
             customClass="tabbar-position"
             safeAreaInsetBottom={false}
             onChange={(e) => this.onChange(e)}
>
  <van-tabbar-item icon="home-o">标签</van-tabbar-item>
  <van-tabbar-item icon="search" dot>标签</van-tabbar-item>
  <van-tabbar-item icon="friends-o" info="5">标签</van-tabbar-item>
  <van-tabbar-item icon="setting-o" info="20">标签</van-tabbar-item>
</van-tabbar>
```

### 自定义图标

可以通过 slot 自定义图标，其中 icon slot 代表未选中状态下的图标，icon-active slot 代表选中状态下的图标

```html
<van-tabbar  active={this.state.active4}
             data-key="active4"
             customClass="tabbar-position"
             safeAreaInsetBottom={false}
             onChange={(e) => this.onChange(e)}>
  <van-tabbar-item info="3">
         <image slot="icon-active"
                        src={this.state.icon.active}
                        mode="aspectFit"
                        style="width: 30px; height: 18px;"/>
    自定义
  </van-tabbar-item>
  <van-tabbar-item icon="search">标签</van-tabbar-item>
  <van-tabbar-item icon="setting-o">标签</van-tabbar-item>
</van-tabbar>
```

```javascript

  state= {
    active: 0,
    icon: {
      normal: 'https://img.yzcdn.cn/vant/user-inactive.png',
      active: 'https://img.yzcdn.cn/vant/user-active.png',
    },
  }

  onChange(event) {
    this.setData({ active: event.detail });
  }

```

### 自定义颜色

```html
<van-tabbar
    active={this.state.active5}
    data-key="active5"
    customClass="tabbar-position"
    activeClor="#07c160"
    inactiveColor="#000"
    safeAreaInsetBottom={false}
    onChange={(e) => this.onChange(e)}
>
  <van-tabbar-item icon="home-o">标签</van-tabbar-item>
  <van-tabbar-item icon="search">标签</van-tabbar-item>
  <van-tabbar-item icon="friends-o">标签</van-tabbar-item>
  <van-tabbar-item icon="setting-o">标签</van-tabbar-item>
</van-tabbar>
```

```javascript

  state= {
    active: 0,
  };
  onChange(event) {
    this.setData({ active: event.detail });
  }

```

### 切换标签事件

```html
<van-tabbar  active={this.state.active6}
             data-key="active6"
             customClass="tabbar-position"
             fixed={true}
             safeAreaInsetBottom={false}
             onChange={(e) => this.handleChange(e)}
>
  <van-tabbar-item icon="home-o">标签1</van-tabbar-item>
  <van-tabbar-item icon="search">标签2</van-tabbar-item>
  <van-tabbar-item icon="friends-o">标签3</van-tabbar-item>
  <van-tabbar-item icon="setting-o">标签4</van-tabbar-item>
</van-tabbar>
```

```javascript

  state= {
    active: 0,
  };

  onClick(event) {
    wx.showToast({
      title: `点击标签 ${event.detail + 1}`,
      icon: 'none',
    });
  }

```

### 结合自定义 tabBar

请参考 [微信官方文档](https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html) 与 [代码片段](https://developers.weixin.qq.com/s/FjLU4mmp7r9s)

## 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>
```
### Tabbar Props

| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| active | 当前选中标签的索引 | _number_ | - | - |
| fixed | 是否固定在底部 | _boolean_ | `true` | - |
| border | 是否展示外边框 | _boolean_ | `true` | - |
| z-index | 元素 z-index | _number_ | `1` | - |
| active-color | 选中标签的颜色 | _string_ | `#1989fa` | - |
| inactive-color | 未选中标签的颜色 | _string_ | `#7d7e80` | - |
| safe-area-inset-bottom | 是否为 iPhoneX 留出底部安全距离 | _boolean_ | `true` | - |

### Tabbar Event

| 事件名      | 说明           | 参数                                     |
| ----------- | -------------- | ---------------------------------------- |
| bind:change | 切换标签时触发 | event.detail: 当前选中标签的名称或索引值 |

### TabbarItem Props

| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| name | 标签名称，作为匹配的标识符 | _string \| number_ | 当前标签的索引值 | - |
| icon | 图标名称或图片链接，可选值见 [Icon 组件](#/icon) | _string_ | - | - |
| dot | 是否显示小红点 | _boolean_ | - | - |
| info | 图标右上角提示信息 | _string \| number_ | - | - |

### TabbarItem Slot

| 名称        | 说明           |
| ----------- | -------------- |
| icon        | 未选中时的图标 |
| icon-active | 选中时的图标   |
