我想添加一个 x 按钮来关闭材料波普尔



我做了一个材料 ui popper 我添加了一个按钮 (x( 来关闭波普尔。

我添加了一个按钮和一个 const 句柄关闭,应该在单击时关闭波普尔,但这不起作用(什么也没发生(每个波普尔都有自己的类

这是我的经历,我的波普尔是在创建的地方

const Experiences = memo(
(props) => {
const { className } = props;
const classes = useStyles(props);
const [anchorEl, setAnchorEl] = React.useState(null);
const handleClick = (event) => {
setAnchorEl(event.currentTarget);
};
// const open = Boolean(anchorEl);
const handleClose = () => {
setAnchorEl({
open: false
});
};
const experience = (img, title, id, popoverCategory) => (
<div
className="experience"
aria-describedby={id}
id={id}
onClick={handleClick}
onKeyDown={handleClick}
role="button"
tabIndex="0"
>
<img
data-sizes="auto"
className="lazyload"
data-src={img}
alt={title}
/>
<div className="experience-title">
<Typography
color="textSecondary"
variant="subtitle2"
className="highlight highlight1"
display="inline"
>
{ title }
</Typography>
</div>
<Popper
id={id}
open={anchorEl && anchorEl.id === id}
anchorEl={anchorEl}
className={clsx(classes[id])}
modifiers={{
flip: {
enabled: false,
},
}}
>         
<Button onClickAway={handleClose}>x</Button>
<div className={clsx(classes.paper)}>
{
popoverCategory.map(url => (
<img
key={id}
data-sizes="auto"
className="lazyload"
src={url}
alt={title}
/>
))
}
</div>
</Popper>
</div>
);

使用此方法:

import React, { memo } from 'react';
import PropTypes from 'prop-types';
import { makeStyles } from '@material-ui/styles';
import Typography from '@material-ui/core/Typography';
import clsx from 'clsx';
import Button from '@material-ui/core/Button';
import Popper from '@material-ui/core/Popper';
import gastronomia from 'assets/experiences/gastronomia.jpg';
import productos from 'assets/experiences/productos.jpg';
import giftcard from 'assets/experiences/giftcard.jpg';
import diversion from 'assets/experiences/diversion.jpg';
import deporte from 'assets/experiences/deporte.jpg';
import belleza from 'assets/experiences/belleza.jpg';
import gastronomiaExperiences from 'data/gastronomia';
import productosExperiences from 'data/productos';
import giftcardExperiences from 'data/giftcard';
import diversionExperiences from 'data/diversion';
import deporteExperiences from 'data/deporte';
import bellezaExperiences from 'data/belleza';

// Proptypes definitions to the component.
const propTypes = {
/** Custom root className. */
className: PropTypes.string,
};
// Default props definitions.
const defaultProps = {
className: null,
};
// Component's styles
const useStyles = makeStyles(theme => ({
root: {
display: 'block',
margin: '0 auto',
maxWidth: '50%',
[theme.breakpoints.down('md')]: {
maxWidth: '70%',
},
[theme.breakpoints.down('sm')]: {
maxWidth: '100%',
},
'& .experiences-column': {
display: 'inline-block',
verticalAlign: 'top',
textAlign: 'center',
'&.col1': {
width: '36.31%',
[theme.breakpoints.down('sm')]: {
width: 'initial',
},
},
'&.col2': {
width: '63.69%',
[theme.breakpoints.down('sm')]: {
width: 'initial',
},
},
'& .experience': {
padding: 2,
position: 'relative',
'& img': {
width: '100%',
display: 'block',
},
'& .experience-title': {
position: 'absolute',
bottom: 30,
left: 0,
right: 0,
textAlign: 'center',
},
},
},
},
gastronomia: {
backgroundColor: 'rgba(0,185,208,0.9)',
position: 'absolute',
marginTop: '-220px',
marginLeft: '25%',
width: '50%',
height: '220px',
},
giftcard: {
backgroundColor: 'rgba(221,165,174,0.9)',
position: 'absolute',
marginTop: '-320px',
marginLeft: '25%',
width: '50%',
height: '320px',
},
deporte: {
backgroundColor: 'rgba(189,143,205,0.9)',
position: 'absolute',
marginTop: '-320px',
marginLeft: '25%',
width: '50%',
height: '320px',
},
productos: {
backgroundColor: 'rgba(221,165,174,0.9)',
position: 'absolute',
marginTop: '-220px',
marginRight: '18%',
width: '50%',
height: '220px',
},
diversion: {
backgroundColor: 'rgba(255,176,10,0.9)',
position: 'absolute',
marginTop: '-320px',
marginRight: '18%',
width: '50%',
height: '320px',
},
belleza: {
backgroundColor: 'rgba(229,166,187,0.9)',
position: 'absolute',
marginTop: '-320px',
marginRight: '18%',
width: '50%',
height: '320px',
},
paper: {
'& img': {
width: '180px',
padding: '0 10px 0 10px',
},
},
}), { name: 'ExperiencesStyle' });

/**
* Component used to render a grid of experiences.
*
* @param {object} props - The component's props.
* @returns {object} React element.
*/
const Experiences = memo(
(props) => {
const { className } = props;
const classes = useStyles(props);
const [poperOpen, setPoperOpen] = React.useState([]); // array of popers states
const [justChange, setJustChange] = React.useState(false); // array of popers states
// one handle click for open/close    
const handleClick = (e, _id, _open) => {
let idx = poperOpen.findIndex(x => x.id === _id);
// for show just one comment this lines : 
//let a = poperOpen;
//if(idx > -1 ){
a.splice(idx, 1);
//}
let a = [];
a.push({id: _id, open: _open, anchorEl: e.currentTarget}); 
setPoperOpen(a);
setJustChange(!justChange);
};
const experience = (img, title, id, popoverCategory, dontRemoveMe) => (
<div>
<div
className="experience"
aria-describedby={id}
id={id}
onClick={(e)=>handleClick(e, id, true)} 
onKeyDown={(e)=>handleClick(e, id, true)}
role="button"
tabIndex="0"
>
<img
data-sizes="auto"
className="lazyload"
data-src={img}
alt={title}
/>
<div className="experience-title">
<Typography
color="textSecondary"
variant="subtitle2"
className="highlight highlight1"
display="inline"
>
{ title }
</Typography>
</div>
</div>
<Popper
id={id}
open={poperOpen.findIndex(x => x.id === id) > -1 && poperOpen.find(x => x.id === id).open }
anchorEl={poperOpen.findIndex(x => x.id === id) > -1 ? poperOpen.find(x => x.id === id).anchorEl : undefined} 
className={clsx(classes[id])}
modifiers={{
flip: {
enabled: false,
},
}}
>         
<Button onClick={(e)=>handleClick(e, id, false)}>x</Button>
<div className={clsx(classes.paper)}>
{
popoverCategory.map(url => (
<img
key={id}
data-sizes="auto"
className="lazyload"
src={url}
alt={title}
/>
))
}
</div>
</Popper>
</div>
);
return (
<div className={clsx(classes.root, className)}>
<div className="experiences-column col1">
{experience(gastronomia, 'GASTRONOMÍA', 'gastronomia', gastronomiaExperiences, justChange)}
{experience(giftcard, 'GIFT CARD', 'giftcard', giftcardExperiences, justChange)}
{experience(deporte, 'DEPORTE', 'deporte', deporteExperiences, justChange)}
</div>
<div className="experiences-column col2">
{experience(productos, 'PRODUCTOS', 'productos', productosExperiences, justChange)}
{experience(diversion, 'DIVERSIÓN', 'diversion', diversionExperience, justChanges)}
{experience(belleza, 'BELLEZA', 'belleza', bellezaExperiences, justChange)}
</div>
</div>
);
},
);
// Component proptypes.
Experiences.propTypes = propTypes;
// Component default props.
Experiences.defaultProps = defaultProps;
export default Experiences;

简单演示:这里

最新更新