I'm trying to add interactivity to a four quadrant chart whereby the user can click a box to highlight it, and the others will deactivate, similar to how radio boxes work in a form.
The idea was to add an onClick event to each card and have a handler function that will check which box was clicked on, activate it, and then deactivate the rest.
The problem I'm having is that e.target seems to be picking up the child nodes of each card instead of the card itself, so I'm having trouble figuring out which card was clicked.
e.g. console log = '>> event.target <li>A</li>' I was hoping to determine which card was picked by doing something like event.target.id
I've tried a bunch of things and nothing has worked... How do people normally set this type of interaction up?
import React from "react"; import Card from "@material-ui/core/Card"; import CardContent from "@material-ui/core/CardContent"; import CardActionArea from '@material-ui/core/CardActionArea'; import Typography from "@material-ui/core/Typography"; import Paper from "@material-ui/core/Paper"; function MyCard({ title }) { return ( <CardActionArea onClick={quadrantClickHandler}> <Card> <CardContent> <Typography>{title}</Typography> </CardContent> </Card> </CardActionArea> ); } function quadrantClickHandler(e) { e.stopPropagation(); console.log('>> event.target ',e.target); //the idea here is that I will click a "card" //and then determine which card was clicked so that //I can highlight it similar to a radio box set in a form. } function Quadrants() { return ( <React.Fragment> <MyCard title={ <ul> <li>A</li> <li>B</li> <li>C</li> <li>D</li> <li>E</li> <li>F</li> <li>G</li> </ul> } /> <MyCard title="Fast but expensive" /> <MyCard title="Slow but Cheap" /> <MyCard title="Slow but Fast" /> </React.Fragment> ); } function FourQuadrants() { const classes = useStyles(); return ( <div> <h2>Make a choice:</h2> <Paper className={classes.paper}> <Typography className={classes.top}>Big</Typography> <Typography className={classes.bottom}>Small</Typography> <Typography align="center" className={classes.left}>Expensive</Typography> <Typography align="center" className={classes.right}>Cheap</Typography> <Quadrants /> </Paper> </div> ); } export default FourQuadrants;