I'm a beginner in using material-ui. I have seen an example on their documentation about their Accordion. I want to use the component but I want it to make only single select. The default from the documentation has no available props to set the accordion to only single select. Has anyone has an idea how to limit the selection into one select only ?
Here is the code from the documentation.
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Accordion from '@material-ui/core/Accordion';
import AccordionSummary from '@material-ui/core/AccordionSummary';
import AccordionDetails from '@material-ui/core/AccordionDetails';
import Checkbox from '@material-ui/core/Checkbox';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Typography from '@material-ui/core/Typography';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
const useStyles = makeStyles({
root: {
width: '100%',
},
});
export default function ActionsInAccordionSummary() {
const classes = useStyles();
return (
<div className={classes.root}>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
aria-label="Expand"
aria-controls="additional-actions1-content"
id="additional-actions1-header"
>
<FormControlLabel
aria-label="Acknowledge"
onClick={(event) => event.stopPropagation()}
onFocus={(event) => event.stopPropagation()}
control={<Checkbox />}
label="I acknowledge that I should stop the click event propagation"
/>
</AccordionSummary>
<AccordionDetails>
<Typography color="textSecondary">
The click event of the nested action will propagate up and expand the accordion unless
you explicitly stop it.
</Typography>
</AccordionDetails>
</Accordion>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
aria-label="Expand"
aria-controls="additional-actions2-content"
id="additional-actions2-header"
>
<FormControlLabel
aria-label="Acknowledge"
onClick={(event) => event.stopPropagation()}
onFocus={(event) => event.stopPropagation()}
control={<Checkbox />}
label="I acknowledge that I should stop the focus event propagation"
/>
</AccordionSummary>
<AccordionDetails>
<Typography color="textSecondary">
The focus event of the nested action will propagate up and also focus the accordion
unless you explicitly stop it.
</Typography>
</AccordionDetails>
</Accordion>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
aria-label="Expand"
aria-controls="additional-actions3-content"
id="additional-actions3-header"
>
<FormControlLabel
aria-label="Acknowledge"
onClick={(event) => event.stopPropagation()}
onFocus={(event) => event.stopPropagation()}
control={<Checkbox />}
label="I acknowledge that I should provide an aria-label on each action that I add"
/>
</AccordionSummary>
<AccordionDetails>
<Typography color="textSecondary">
If you forget to put an aria-label on the nested action, the label of the action will
also be included in the label of the parent button that controls the accordion
expansion.
</Typography>
</AccordionDetails>
</Accordion>
</div>
);
}
CodePudding user response:
There is no prop for what you are trying to do because it has nothing to do with accordion components. You can store the currently checked checkbox in useState
and based on that you can check which checkbox should be checked.
for eg.
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Accordion from '@material-ui/core/Accordion';
import AccordionSummary from '@material-ui/core/AccordionSummary';
import AccordionDetails from '@material-ui/core/AccordionDetails';
import Checkbox from '@material-ui/core/Checkbox';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Typography from '@material-ui/core/Typography';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
const useStyles = makeStyles({
root: {
width: '100%',
},
});
export default function ActionsInAccordionSummary() {
const classes = useStyles();
const [checked, setChecked] = React.useState("")
return (
<div className={classes.root}>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
aria-label="Expand"
aria-controls="additional-actions1-content"
id="additional-actions1-header"
>
<FormControlLabel
aria-label="Acknowledge"
onChange={(e)=>setChecked(e.target.name)}
onClick={(event) => event.stopPropagation()}
onFocus={(event) => event.stopPropagation()}
control={<Checkbox name="checkbox1" checked={checked==="checkbox1"} />}
label="I acknowledge that I should stop the click event propagation"
/>
</AccordionSummary>
<AccordionDetails>
<Typography color="textSecondary">
The click event of the nested action will propagate up and expand the accordion unless
you explicitly stop it.
</Typography>
</AccordionDetails>
</Accordion>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
aria-label="Expand"
aria-controls="additional-actions2-content"
id="additional-actions2-header"
>
<FormControlLabel
onChange={(e)=>setChecked(e.target.name)}
aria-label="Acknowledge"
onClick={(event) => event.stopPropagation()}
onFocus={(event) => event.stopPropagation()}
control={<Checkbox name="checkbox2" checked={checked==="checkbox2"} />}
label="I acknowledge that I should stop the focus event propagation"
/>
</AccordionSummary>
<AccordionDetails>
<Typography color="textSecondary">
The focus event of the nested action will propagate up and also focus the accordion
unless you explicitly stop it.
</Typography>
</AccordionDetails>
</Accordion>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
aria-label="Expand"
aria-controls="additional-actions3-content"
id="additional-actions3-header"
>
<FormControlLabel
aria-label="Acknowledge"
onChange={(e)=>setChecked(e.target.name)}
onClick={(event) => event.stopPropagation()}
onFocus={(event) => event.stopPropagation()}
control={<Checkbox name="checkbox3" checked={checked==="checkbox3"} />}
label="I acknowledge that I should provide an aria-label on each action that I add"
/>
</AccordionSummary>
<AccordionDetails>
<Typography color="textSecondary">
If you forget to put an aria-label on the nested action, the label of the action will
also be included in the label of the parent button that controls the accordion
expansion.
</Typography>
</AccordionDetails>
</Accordion>
</div>
);
}