antd-mobile
是 Ant Design 的移动规范的 React 实现,服务于蚂蚁及口碑无线业务。创建一个项目
安装
使用
// 导入组件 在App.js根组件中导入要使用的组件
import { Button } from ‘antd-mobile‘;
// 导入样式 在index.js 中导入组件样式
import ‘antd-mobile/dist/antd-mobile.css‘; // or ‘antd-mobile/dist/antd-mobile.less‘
ReactDOM.render(<Button>Start</Button>, mountNode);
</>
显示源码<TabBar.Item
title="首页"
...
>
</TabBar.Item>
<TabBar.Item
title="找房"
...
>
</TabBar.Item>
<TabBar.Item
title="咨询"
...
>
</TabBar.Item>
<TabBar.Item
title="我的"
...
>
</TabBar.Item>
<TabBar
tintColor="#21b97a"
barTintColor="white"
>
...
</TabBar>
<TabBar.Item
{/*默认的图标*/}
icon={
<i className="iconfont icon-ind"></i>
}
{/*选中图标*/}
selectedIcon={<i className="iconfont icon-ind"></i>
}
...
>
</TabBar.Item>
<TabBar.Item
icon={
<i className="iconfont icon-findHouse"></i>
}
selectedIcon={
<i className="iconfont icon-findHouse"></i>
}
...
>
</TabBar.Item>
<TabBar.Item
icon={
<i className="iconfont icon-infom"></i>
}
selectedIcon={
<i className="iconfont icon-infom"></i>
}
...
>
</TabBar.Item>
<TabBar.Item
icon={
<i className="iconfont icon-my"></i>
}
selectedIcon={<i className="iconfont icon-my"></i>}
...
>
</TabBar.Item>
修改TabBar菜单项的图标大小
调整TabBar的位置,固定在最底部
去掉TabBar的徽章
<TabBar
...
noRenderContent = "true"
>
<TabBar.Item
...
onPress={() => {
this.setState({
selectedTab: ‘blueTab‘,
});
{/* 切换路由 */}
this.props.history.push(‘/home/index‘)
}}
>
</TabBar.Item>
<TabBar.Item
...
onPress={() => {
this.setState({
selectedTab: ‘redTab‘,
});
this.props.history.push(‘/home/list‘)
}}
>
</TabBar.Item>
<TabBar.Item
...
onPress={() => {
this.setState({
selectedTab: ‘greenTab‘,
});
this.props.history.push(‘/home/news‘)
}}
>
</TabBar.Item>
<TabBar.Item
...
onPress={() => {
this.setState({
selectedTab: ‘yellowTab‘,
});
this.props.history.push(‘/home/profile‘)
}}
>
</TabBar.Item>
{/* 配置路由信息 */}
<Route path="/home/index" component={Index}></Route>
<Route path="/home/list" component={HouseList}></Route>
<Route path="/home/news" component={News}></Route>
<Route path="/home/profile" component={Profile}></Route>
给菜单项添加selected属性,设置当前匹配的菜单项高亮
state = {
// 选中的菜单项,记录当前的pathname来匹配对应的tab
selectedTab: this.props.location.pathname,
}
<TabBar.Item
selected={this.state.selectedTab === ‘/home/index‘}
onPress={() => {
this.setState({
selectedTab: ‘/home/index‘,
});
this.props.history.push(‘/home/index‘)
}}
...
>
</TabBar.Item>
<TabBar.Item
selected={this.state.selectedTab === ‘/home/list‘}
onPress={() => {
this.setState({
selectedTab: ‘/home/list‘,
});
this.props.history.push(‘/home/list‘)
}}
...
>
</TabBar.Item>
<TabBar.Item
selected={this.state.selectedTab === ‘/home/news‘}
onPress={() => {
this.setState({
selectedTab: ‘/home/news‘,
});
this.props.history.push(‘/home/news‘)
}}
...
>
</TabBar.Item>
<TabBar.Item
selected={this.state.selectedTab === ‘/home/profile‘}
onPress={() => {
this.setState({
selectedTab: ‘/home/profile‘,
});
this.props.history.push(‘/home/profile‘)
}}
...
>
</TabBar.Item>
声明一下数据源
const tabItems = [{
title: ‘首页‘,
icon: ‘icon-ind‘,
path: ‘/home/index‘
},
{
title: ‘找房‘,
icon: ‘icon-findHouse‘,
path: ‘/home/list‘
},
{
title: ‘资讯‘,
icon: ‘icon-infom‘,
path: ‘/home/news‘
},
{
title: ‘我的‘,
icon: ‘icon-my‘,
path: ‘/home/profile‘
}]
封装一个函数来遍历渲染
renderTabBarItem() {
return tabItems.map(item => {
return (
<TabBar.Item
title={item.title}
key={item.title}
icon={
<i className={`iconfont ${item.icon}`}></i>
}
selectedIcon={<i className={`iconfont ${item.icon}`}></i>
}
selected={this.state.selectedTab === item.path}
onPress={() => {
this.setState({
selectedTab: item.path,
});
this.props.history.push(item.path)
}}
>
</TabBar.Item>
)
})
}
在render方法中调用即可
render() {
return (<div>
{/* 配置路由信息 */}
<Route path="/home/index" component={Index}></Route>
<Route path="/home/list" component={HouseList}></Route>
<Route path="/home/news" component={News}></Route>
<Route path="/home/profile" component={Profile}></Route>
{/* 底部导航栏 */}
<TabBar
tintColor="#21b97a"
barTintColor="white"
noRenderContent="true"
>
{this.renderTabBarItem()}
</TabBar>
</div>)
}
{/* 配置默认路由 */}
<Route path="/" exact render={() => <Redirect to="/home"></Redirect>}></Route>
原文:https://www.cnblogs.com/wangyuwei5955616/p/13184782.html