React微信小程序自定义tabbar

下方tabbar因其特殊性,现在需要实现自定义

1.以前定义的config中的tabbar下的custom设置改为false

bash 复制代码
"tabBar": {
   "custom": true,
    "color": "#b5b5b5",
    "selectedColor": "#000",
    "backgroundColor": "#fff",
    "borderStyle": "white",
    "list": [
        {
            "pagePath": "pages/home/index",
            "text": "首页",
            "iconPath": "resources/images/tabs/home.png",
            "selectedIconPath": "resources/images/tabs/home-active.png"
        },
        {
            "pagePath": "pages/achievement/index",
            "text": "业绩",
            "iconPath": "resources/images/tabs/achievement.png",
            "selectedIconPath": "resources/images/tabs/achievement_active.png"
        }
    ]
},

2.自己封装一个组件custom-tab-bar文件

index.tsx

bash 复制代码
import Taro, { Component } from '@tarojs/taro';
import { Image, Text, View } from '@tarojs/components';
import { inject, observer } from '@tarojs/mobx';
import { CusTomTabBarPageType } from '@/interfaces';
import ICON_HOME from '@/resources/images/tabs/home.png';
import ICON_HOME_ACTIVE from '@/resources/images/tabs/home-active.png';
import ICON_ACHIEVEMENT from '@/resources/images/tabs/achievement.png';
import ICON_ACHIEVEMENT_ACTIVE from '@/resources/images/tabs/achievement_active.png';
import ICON_CONSUME from '@/resources/images/tabs/consume.png';
import './index.scss';

interface CusTomTabBarProps {
    pageType: CusTomTabBarPageType;
}

const prefix = 'custom-tab-tab';
const innerClassNames = {
    consume: `${prefix}__consume`,
    tabs: `${prefix}__tabs`,
    tab: `${prefix}__tab`,
    tabImg: `${prefix}__tab__img`,
    tabName: `${prefix}__tab__name`,
    tabConsume: `${prefix}__tab__consume`,
    tabConsumeImg: `${prefix}__tab__consume__img`
};

/**
 * 导购员信息
 */
@inject('guideStore', 'consumeStore')
@observer
export default class CusTomTabBar extends Component<CusTomTabBarProps, {}> {
    constructor(props) {
        super(props);
    }

    /**
     * 是否展示tab公用核销入口
     */
    getShowConsume() {
        return true;
    }

    onClickTab = (path: string) => {
        Taro.switchTab({
            url: path
        });
    };

    onClickConsume = async () => {
        //核销方法
    };
    

    render() {
        const { pageType } = this.props;
        const showConsume = this.getShowConsume();
        const homeUrl = '/pages/home/index';
        const achievementUrl = '/pages/achievement/index';
        const tabStyle = {
            width: showConsume ? `${100 / 3}%` : `${100 / 2}%`
        };
        return (
            <View className={prefix}>
                <View className={innerClassNames.tabs}>
                    <View
                        className={innerClassNames.tab}
                        style={tabStyle}
                        onClick={this.onClickTab.bind(this, homeUrl)}
                    >
                        <Image
                            className={innerClassNames.tabImg}
                            src={
                                pageType === CusTomTabBarPageType.Home
                                    ? ICON_HOME_ACTIVE
                                    : ICON_HOME
                            }
                        />
                        <Text
                            className={innerClassNames.tabName}
                            style={{
                                color:
                                    pageType === CusTomTabBarPageType.Home
                                        ? '#000'
                                        : undefined
                            }}
                        >
                            首页
                        </Text>
                    </View>
                    {showConsume && (
                        <View className={innerClassNames.tab} style={tabStyle}>
                            <View
                                className={innerClassNames.tabConsume}
                                onClick={this.onClickConsume}
                            >
                                <Image
                                    className={innerClassNames.tabConsumeImg}
                                    src={ICON_CONSUME}
                                />
                            </View>
                        </View>
                    )}
                    <View
                        className={innerClassNames.tab}
                        style={tabStyle}
                        onClick={this.onClickTab.bind(this, achievementUrl)}
                    >
                        <Image
                            className={innerClassNames.tabImg}
                            src={
                                pageType === CusTomTabBarPageType.Achievement
                                    ? ICON_ACHIEVEMENT_ACTIVE
                                    : ICON_ACHIEVEMENT
                            }
                        />
                        <Text
                            className={innerClassNames.tabName}
                            style={{
                                color:
                                    pageType ===
                                    CusTomTabBarPageType.Achievement
                                        ? '#000'
                                        : undefined
                            }}
                        >
                            业绩
                        </Text>
                    </View>
                </View>
            </View>
        );
    }
}

index.scss

bash 复制代码
.custom-tab-tab {
    &__tabs {
        display: flex;
        flex-direction: row;
    }
    &__tabs {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        padding: 10px 0 20px 0;
        width: 100%;
        background-color: #fff;
    }
    &__tab {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        position: relative;
        height: 100px;
        &__img {
            width: 48px;
            height: 48px;
        }
        &__name {
            font-size: 20px;
            font-weight: bold;
            color: rgba(0, 0, 0, 0.32);
        }
        &__consume {
            position: absolute;
            top: -40rpx;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 120px;
            height: 120px;
            border-radius: 100px;
            background: linear-gradient(114deg, #e1ba8b 1.76%, #ba8848 84.66%);
            box-shadow: 0 5px 20px 0 rgba(0, 0, 0, 0.15);
            &__img {
                width: 64px;
                height: 64px;
            }
        }
    }
}

3.对应的tab页面引用

bash 复制代码
<View className='home'>
    <ScrollView scrollY className='home__scroll'>
        <View className='home-page'></View>
    </ScrollView>
    <CusTomTabBar pageType={CusTomTabBarPageType.Home} />
</View>

.home {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background-color: #f6f6f6;
    &__scroll {
        flex: 1;
        overflow-x: auto;
    }
}
.home-page {
    padding: 20px;
}

希望对大家有帮助 ~ ❤️

相关推荐
GDAL3 分钟前
Tailwind CSS 响应式设计实战指南:从零搭建书签篮自适应页面
前端·css·tailwindcss·书签篮
L-岁月染过的梦4 分钟前
前端使用JS实现端口探活
开发语言·前端·javascript
DsirNg13 分钟前
CategoryTree 性能优化完整演进史
开发语言·前端
小安同学iter16 分钟前
Vue3 进阶核心:高级响应式工具 + 特殊内置组件核心解析
前端·javascript·vue.js·vue3·api
Roc.Chang21 分钟前
Vue 3 setup 语法糖 computed 的深度使用
前端·javascript·vue.js
玄尺_00724 分钟前
uniapp h5端使浏览器弹出下载框
前端·javascript·uni-app
军军君0130 分钟前
Three.js基础功能学习三:纹理与光照
前端·javascript·3d·前端框架·three·三维·三维框架
淡笑沐白31 分钟前
Vue3基础语法教程
前端·javascript·vue.js
JIngJaneIL38 分钟前
基于java + vue连锁门店管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
Mintopia38 分钟前
🧠 从零开始:纯手写一个支持流式 JSON 解析的 React Renderer
前端·数据结构·react.js