我有一个带有两个按钮的 React 组件。当用户单击左键时,它应该改变左键的类,也就是右键的类,它也会渲染 ListView 组件。同样,当用户单击右键时,它应该更改右键的类,以及左键的类,并且它还将呈现 MapView 组件。我在下面的代码中尝试了一些方法。请让我知道,我缺少什么,并让我知道实现这一目标。
提前致谢。
import React, { Component } from 'react';
import { Typography, withStyles, Button} from '@material-ui/core';
import ListView from './listView/ListView';
import MapView from './mapView/mapView';
const styles = {
boardHeader: {
display: 'flex'
},
boardTitle: {
flexGrow: 1,
fontSize: 18,
fontWeight: 500,
color: '#ED8E34'
},
buttonList: {
backgroundColor: '#fff',
borderRadius: 50,
fontSize: 12,
minHeight: 10,
padding: '0px 30px',
boxShadow: 'none'
},
buttonMap: {
backgroundColor: '#fff',
borderRadius: 50,
fontSize: 12,
minHeight: 10,
padding: '0px 30px',
boxShadow: 'none',
marginLeft: '-10px'
},
activeButtonList: {
backgroundColor: '#fff',
borderRadius: 50,
fontSize: 12,
minHeight: 10,
padding: '0px 30px',
zIndex: 1
},
activeButtonMap: {
backgroundColor: '#fff',
borderRadius: 50,
fontSize: 12,
minHeight: 10,
padding: '0px 30px',
marginLeft: '-10px'
},
};
class BoardContent extends Component {
constructor(props){
super(props);
this.state = {
active: 1,
}
}
onClickList = (e, active) => {
this.setState({active})
}
render() {
const { classes } = this.props
const { active } = this.state
return (
<div>
<div className={classes.boardHeader}>
<Typography className={classes.boardTitle}>Select the View</Typography>
<div>
<Button variant="contained" size="small" className={active === 1 ? classes.activeButtonList : classes.buttonList} onClick={this.onClickList}>LIST VIEW</Button>
<Button variant="contained" size="small" className={active === 2 ? classes.activeButtonMap : classes.buttonMap} onClick={this.onClickList}>MAP VIEW</Button>
</div>
</div>
{active === 1 ? <ListView /> : null}
{active === 2 ? <MapView /> : null}
</div>
)
}
}
export default withStyles(styles)(BoardContent);