UNl*_*nel 5 javascript reactjs material-ui popper.js react-popper
我正在开发日历应用程序。
问题:单击弹出器的弹出器会关闭两个弹出器,因为它会触发第一个弹出器的单击外部事件,从而关闭它。
我有一个<Event />使用 Material-UI React 的组件<Popper />,并且它可以很好地工作。将其与<AwayClickListener />单击外侧时关闭,单击弹出器内部时保持打开状态。我创建了<Events />一个<Event />.
当单击+更多文本时,包含当天所有事件的弹出窗口应该出现在单元格顶部。
波普孩子们也是<Events />:
单击某个事件应打开一个包含事件详细信息的弹出窗口,就像在单元格中单击它一样。由于我使用相同的组件<Events />,它确实做到了这一点,但并不完全符合预期:
单击事件详细信息弹出窗口将关闭两个弹出窗口。
这就是问题所在:要求是单击弹出窗口的外侧将关闭弹出窗口,但单击内部将使它们打开并交互
调试显示,单击第二个弹出器会触发第一个弹出器的外部单击事件,从而将其关闭。另外,从第一个弹出器中取出点击离开侦听器功能,使第二个弹出器在大多数点击中保持打开状态 - 单击其中的某些位置,会触发它的点击离开功能,从而将其关闭。例如:单击标题将其关闭,单击位置或摘要 div 则不会。
<ClickAwayListener />。<ClickAwayListener />代码
<Popper>- 材质 ui popper 的服装包装
const popper = ({
placement,
open,
anchorEl,
handleClickAway=null,
title,
handleCloseClick=null,
children,
popperStyle = {},
calendarPopoverClass = ''
}) => {
const useStyles = makeStyles({
Popper: popperStyle
})
const styles = useStyles();
return (
<Popper modifiers={{
flip: {
enabled: false,
},
preventOverflow: {
enabled: false,
boundariesElement: 'scrollParent',
}
}}
className={styles.Popper}
placement={placement}
open={open}
anchorEl={anchorEl}
>
<ClickAwayListener onClickAway={handleClickAway}>
<CalendarPopover className={st(classes[calendarPopoverClass])} isShown withArrow={false} title={title} onClose={handleCloseClick}>
{children}
</CalendarPopover>
</ClickAwayListener>
</Popper>
)
}
Run Code Online (Sandbox Code Playgroud)
<Event />
const event = ({ PROPS }) => {
const [expanded, setExpanded] = React.useState(null);
const closeExpanded = () => setExpanded(null)
return (
<>
<div
className={st(classes.Event, { isTimeShown, isNextWeekFirstFiller, isLastFiller, isMultiDay, isAllDay, isFiller })}
style={inlineStyle}
onClick={onEventClick}
>
<div className={classes.Time}>{timeToDisplay}</div>
<div className={classes.Title}>{title}</div>
</div>
<Popper
placement={popperPlacement}
title={title}
handleCloseClick={closeExpanded}
handleClickAway={closeExpanded}
open={Boolean(expanded)}
anchorEl={expanded}
popperStyle={popperStyle}
calendarPopoverClass='Event'
>
<ExpandedEvent
startDate={startDate}
endDate={endDate}
location={location}
summary={summary}
/>
</Popper>
</>
);
}
Run Code Online (Sandbox Code Playgroud)
<Events />
const Events = ({ events, isTimeShown, localeToggle, popperPlacement, popperStyle, handleShowMoreClick=null }) => {
const eventsToShow: JSX.Element[] = [];
if (events.length > 0) {
let eventsToShowAmount = 3;
const moreEventsCount = events.length - eventsToShowAmount;
eventsToShowAmount = moreEventsCount > 0 ? eventsToShowAmount : events.length;
for (let i = 0; i < eventsToShowAmount; i++) {
eventsToShow.push(
<Event
key={events[i].id}
{...events[i]}
isTimeShown={isTimeShown}
popperPlacement={popperPlacement}
popperStyle={popperStyle}
/>
)
}
if (moreEventsCount > 0) {
eventsToShow.push(<ShowMore key='ShowMore' handleClick={handleShowMoreClick} moreEventsCount={moreEventsCount} />)
}
}
return (
<div className={classes.Events}>
{eventsToShow}
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
<MonthlyCell />
const MonthlyCell = ({
events,
isTimeShown,
popperPlacement,
popperStyle
}) => {
const [expandedEvents, setExpandedEvents] = React.useState(null);
const cell = React.useRef<HTMLDivElement>(null)
const eventsList = (handleShowMoreClick = null) => (
<Events
events={events}
isTimeShown={isTimeShown}
localeToggle={true}
popperPlacement={popperPlacement}
popperStyle={popperStyle}
handleShowMoreClick={handleShowMoreClick}
/>
);
const handleShowMoreClick = () => setExpandedEvents(eventsList());
const closeExpandedEvents = () => {
setExpandedEvents(null);
}
return (
<>
<div ref={cell} className={classes.MonthlyCell} >
{eventsList(handleShowMoreClick)}
</div>
<Popper
placement='left'
open={Boolean(expandedEvents)}
title='hello'
handleClickAway={closeExpandedEvents}
anchorEl={cell.current}
popperStyle={{ left: '17% !important' }}
handleCloseClick={closeExpandedEvents}
>
{eventsList()}
</Popper>
</>
);
}
Run Code Online (Sandbox Code Playgroud)
希望它足够清楚。如果还需要什么,请告诉我。谢谢
编辑1
另一种尝试是给父 popper 更大的 z-index,但没有成功
解决方案是将 popper 孩子们围在一个 div 中。我使用的组件导致了这种不需要的行为,因为它没有forwardRef 支持。所以添加 div 包装器解决了这个问题。
另外,删除修饰符属性:
<Popper
// modifiers={{
// flip: {
// enabled: false,
// },
// preventOverflow: {
// enabled: false,
// boundariesElement: 'scrollParent',
// }
// }}
Run Code Online (Sandbox Code Playgroud)
工作解决方案的链接:https://codesandbox.io/s/popper-in-a-popper-s6dfr ?file=/src/Popper/Popper.js:372-519
| 归档时间: |
|
| 查看次数: |
4608 次 |
| 最近记录: |