npm package: @retailwe/ui-tabbar
Description: ## 引入
Latest version: 0.0.12

# Tabbar 标签栏

## 引入

在`app.json`或`index.json`中引入组件，详细介绍见[快速上手](#/quickstart#yin-ru-zu-jian)

```json
"usingComponents": {
  "wr-tabbar": "@retailwe/ui-tabbar/index",
  "wr-tabbar-item": "@retailwe/ui-tabbar/tabbar-item/index"
}
```

## 代码演示

### 基础用法

```html
<wr-tabbar active="{{ active }}" bind:change="onChange">
  <wr-tabbar-item icon="home-o">标签</wr-tabbar-item>
  <wr-tabbar-item icon="search">标签</wr-tabbar-item>
  <wr-tabbar-item icon="friends-o">标签</wr-tabbar-item>
  <wr-tabbar-item icon="setting-o">标签</wr-tabbar-item>
</wr-tabbar>
```

```javascript
Page({
  data: {
    active: 0,
  },
  onChange(event) {
    // event.detail 的值为当前选中项的索引
    this.setData({ active: event.detail });
  },
});
```

### 通过名称匹配

在标签未指定`name`属性的情况下，`active`的值为当前标签的索引
在标签指定`name`属性的情况下，`active`的值为当前标签的`name`

```html
<wr-tabbar active="{{ active }}" bind:change="onChange">
  <wr-tabbar-item name="home" icon="home-o">标签</wr-tabbar-item>
  <wr-tabbar-item name="search" icon="search">标签</wr-tabbar-item>
  <wr-tabbar-item name="friends" icon="friends-o">标签</wr-tabbar-item>
  <wr-tabbar-item name="setting" icon="setting-o">标签</wr-tabbar-item>
</wr-tabbar>
```

```javascript
Page({
  data: {
    active: 'home',
  },
  onChange(event) {
    this.setData({ active: event.detail });
  },
});
```

### 显示徽标

```html
<wr-tabbar active="{{ active }}" bind:change="onChange">
  <wr-tabbar-item icon="home-o">标签</wr-tabbar-item>
  <wr-tabbar-item icon="search" dot>标签</wr-tabbar-item>
  <wr-tabbar-item icon="friends-o" info="5">标签</wr-tabbar-item>
  <wr-tabbar-item icon="setting-o" info="20">标签</wr-tabbar-item>
</wr-tabbar>
```

### url图标

icon属性也支持传入图片url

```html
<wr-tabbar
    active="{{ active4 }}"
    data-key="active4"
    wr-class="tabbar-position"
    safe-area-inset-bottom="{{ false }}"
    bind:change="onChange"
  >
    <wr-tabbar-item icon="{{active4 === 0 ? icon.active : icon.normal}}" info="3">
      url图标
    </wr-tabbar-item>
    <wr-tabbar-item icon="tabbar-fenlei">标签</wr-tabbar-item>
    <wr-tabbar-item icon="tabbar-gerenzhongxin">标签</wr-tabbar-item>
  </wr-tabbar>
```

### 自定义图标

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

```html
<wr-tabbar
    active="{{ active5 }}"
    bind:change="onChange"
  >
    <wr-tabbar-item info="3">
      <image
        slot="icon"
        src="{{ icon.normal }}"
        mode="aspectFit"
        style="width: 30px; height: 18px;"
      />
      <image
        slot="icon-active"
        src="{{ icon.active }}"
        mode="aspectFit"
        style="width: 30px; height: 18px;"
      />
      <view style="color: {{active5 === 0 ? '#1989fa' : ''}}">自定义</view>
    </wr-tabbar-item>
    <wr-tabbar-item icon="tabbar-fenlei">标签</wr-tabbar-item>
    <wr-tabbar-item icon="tabbar-gerenzhongxin">标签</wr-tabbar-item>
  </wr-tabbar>
```

```javascript
Page({
  data: {
    active: 0,
    icon: {
      normal: 'https://img.yzcdn.cn/wrt/user-inactive.png',
      active: 'https://img.yzcdn.cn/wrt/user-active.png',
    },
  },
  onChange(event) {
    this.setData({ active: event.detail });
  },
});
```

### 自定义颜色

```html
<wr-tabbar
  active="{{ active }}"
  active-color="#07c160"
  inactive-color="#000"
  bind:change="onChange"
>
  <wr-tabbar-item icon="home-o">标签</wr-tabbar-item>
  <wr-tabbar-item icon="search">标签</wr-tabbar-item>
  <wr-tabbar-item icon="friends-o">标签</wr-tabbar-item>
  <wr-tabbar-item icon="setting-o">标签</wr-tabbar-item>
</wr-tabbar>
```

```javascript
Page({
  data: {
    active: 0,
  },
  onChange(event) {
    this.setData({ active: event.detail });
  },
});
```

### 切换标签事件

```html
<wr-tabbar active="{{ active }}" bind:change="onChange">
  <wr-tabbar-item icon="home-o">标签1</wr-tabbar-item>
  <wr-tabbar-item icon="search">标签2</wr-tabbar-item>
  <wr-tabbar-item icon="friends-o">标签3</wr-tabbar-item>
  <wr-tabbar-item icon="setting-o">标签4</wr-tabbar-item>
</wr-tabbar>
```

```javascript
Page({
  data: {
    active: 0,
  },
  onChange(event) {
    wx.showToast({
      title: `点击标签 ${event.detail + 1}`,
      icon: 'none',
    });
  },
});
```

## API

### 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 | 图标名称或图片链接 | _string_ | - | - |
| dot | 是否显示小红点 | _boolean_ | - | - |
| info | 图标右上角提示信息 | _string \| number_ | - | - |

### TabbarItem Slot

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