I am using the Material UI Modal component in my React app, and it will take up the majority of the screen (about 95%). As a result, I would like to give users a more intuitive way of closing the modal by adding a small "X" icon in the upper right of the modal and allowing that to close it. I am passing the same handleClose function down to this icon as I am to the Modal itself, but the function isn't even getting called when I click the icon. I checked all the props and the function is getting passed down correctly, it just isn't getting evaluated on the CloseIcon component's onClick.
Page.js
const Page = props => {
const [open, setOpen] = React.useState(false);
const handleOpen = () => {
setOpen(true);
};
const handleClose = () => {
console.log('testing')
setOpen(false);
};
return (
<>
<Button type="button" onClick={handleOpen} buttonText="Add Meal" />
<ModalContainer
open={open}
handleClose={handleClose}
>
</ModalContainer>
</>
)
};
ModalContainer.js
const ModalContainer = ({
open,
handleClose,
...props
}) => {
return (
<div>
<Modal
open={open}
onClose={handleClose}
>
<StyledDialogContent>
<ModalContent handleClose={handleClose} />
</StyledDialogContent>
</Modal>
</div>
)
};
ModalContent.js
class ModalContent extends React.Component {
render() {
const { handleClose } = this.props;
return (
<Container justify="center" alignItems="center">
<ModalBody flexDirection="column" >
<TopBar justify="flex-end" alignItems="center">
<CloseIcon onClick={handleClose} />
</TopBar>
<BodyContainer>
<FlexContainer>
<RecipeCard />
</FlexContainer>
<FlexContainer>
<MenuCard
title="Custom Food"
icon="https://nutriology-storage.s3.amazonaws.com/Custom-Food.svg"
link=""
/>
</FlexContainer>
</BodyContainer>
</ModalBody>
</Container>
)
}
};
CloseIcon.js
const CloseIcon = props => (
<Circle justify="center" alignItems="center">
<Icon
viewBox="0 0 26 26"
xmlns="http://www.w3.org/2000/svg"
xmlnsXlink="http://www.w3.org/1999/xlink"
>
<g id="Style-Guide" stroke="none" strokeWidth="1" fillRule="evenodd">
<g
id="Style-Guide---Elements"
transform="translate(-198.000000, -5239.000000)"
strokeWidth="1.5"
>
<g
id="Remove-X-icon-Default"
transform="translate(199.000000, 5240.000000)"
>
<g
id="Group"
transform="translate(12.000000, 12.000000) rotate(-315.000000) translate(-12.000000, -12.000000) translate(6.000000, 6.000000)"
strokeLinecap="round"
>
<path d="M0,6 L12,6" id="Line-2"></path>
<path d="M6,0 L6,12" id="Line-2-Copy"></path>
</g>
</g>
</g>
</g>
</Icon>
</Circle>
);
How can I make this CloseIcon component actually call the handleClose function and close the modal?
EDIT: I added the CloseIcon.js component here for reference. However, the onClick event is firing correctly -- I tested this by replacing the handleClose function with a simple console.log and it fired appropriately on click.
Your CloseIcon component doesn't handle onClick event.
add onClick prop to Circle or Icon
<Circle onClick={props.onClick} justify="center" alignItems="center">
or
<Icon
onClick={props.onClick}
viewBox="0 0 26 26"
xmlns="http://www.w3.org/2000/svg"
xmlnsXlink="http://www.w3.org/1999/xlink"
>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With