Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5699: MegaMenu strict mode fix #5700

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 85 additions & 83 deletions components/lib/megamenu/MegaMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,6 @@ export const MegaMenu = React.memo(
}
};

useUpdateEffect(() => {
if (mobileActiveState) {
bindListeners();
} else {
unbindListeners();
}
}, [mobileActiveState]);

const isOutsideClicked = (event) => {
return elementRef.current && !(elementRef.current.isSameNode(event.target) || elementRef.current.contains(event.target) || (menuButtonRef.current && menuButtonRef.current.contains(event.target)));
};
Expand All @@ -260,6 +252,90 @@ export const MegaMenu = React.memo(
}
});

useUpdateEffect(() => {
if (attributeSelectorState && elementRef.current) {
elementRef.current.setAttribute(attributeSelectorState, '');
createStyle();
}

return () => {
destroyStyle();
};
}, [attributeSelectorState, props.breakpoint]);

useUpdateEffect(() => {
if (mobileActiveState) {
bindListeners();
} else {
unbindListeners();
}
}, [mobileActiveState]);

useUpdateEffect(() => {
if (focusTrigger) {
const itemIndex = focusedItemInfo.index !== -1 ? findNextItemIndex(focusedItemInfo.index) : findFirstFocusedItemIndex();

changeFocusedItemInfo(itemIndex);
setFocusTrigger(false);
}
}, [focusTrigger]);

useUpdateEffect(() => {
const currentPanel = DomHandler.findSingle(elementRef.current, '.p-menuitem-active > .p-megamenu-panel');

if (activeItemState) {
bindListeners();

if (!isMobileMode) {
ZIndexUtils.set('menu', currentPanel, (context && context.autoZIndex) || PrimeReact.autoZIndex, (context && context.zIndex['menu']) || PrimeReact.zIndex['menu']);
}
} else {
unbindListeners();
}

if (isMobileMode) {
currentPanel && currentPanel.previousElementSibling.scrollIntoView({ block: 'nearest', inline: 'nearest' });
}

return () => {
unbindListeners();
ZIndexUtils.clear(currentPanel);
};
}, [activeItemState, isMobileMode]);

useUpdateEffect(() => {
const _focusedItemId = ObjectUtils.isNotEmpty(focusedItemInfo.key) ? `${idState}_${focusedItemInfo.key}` : null;

setFocusedItemId(_focusedItemId);
}, [focusedItemInfo]);

React.useEffect(() => {
const itemsToProcess = props.model || [];
const processed = createProcessedItems(itemsToProcess, 0, null, '');

setProcessedItems(processed);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.model]);

useUpdateEffect(() => {
const processedItem = ObjectUtils.isNotEmpty(activeItemState) ? activeItemState : null;

const _visibleItems =
processedItem && processedItem.key === focusedItemInfo.parentKey
? processedItem.items.reduce((items, col) => {
col.forEach((submenu) => {
submenu.items.forEach((a) => {
items.push(a);
});
});

return items;
}, [])
: processedItems;

setVisibleItems(_visibleItems);
}, [focusedItemInfo, activeItemState, processedItems]);

const onFocus = (event) => {
setFocused(true);

Expand Down Expand Up @@ -329,6 +405,7 @@ export const MegaMenu = React.memo(
case 'PageUp':
case 'Backspace':
case 'ShiftLeft':
focusedItemInfo;
case 'ShiftRight':
//NOOP
break;
Expand All @@ -342,15 +419,6 @@ export const MegaMenu = React.memo(
}
};

useUpdateEffect(() => {
if (focusTrigger) {
const itemIndex = focusedItemInfo.index !== -1 ? findNextItemIndex(focusedItemInfo.index) : findFirstFocusedItemIndex();

changeFocusedItemInfo(itemIndex);
setFocusTrigger(false);
}
}, [focusTrigger]);

const onArrowDownKey = (event) => {
event.preventDefault();

Expand Down Expand Up @@ -670,61 +738,6 @@ export const MegaMenu = React.memo(
return index - props.model.slice(0, index).filter((processedItem) => isItemVisible(processedItem) && getItemProp(processedItem, 'separator')).length + 1;
};

useUpdateEffect(() => {
const currentPanel = DomHandler.findSingle(elementRef.current, '.p-menuitem-active > .p-megamenu-panel');

if (activeItemState) {
bindListeners();

if (!isMobileMode) {
ZIndexUtils.set('menu', currentPanel, (context && context.autoZIndex) || PrimeReact.autoZIndex, (context && context.zIndex['menu']) || PrimeReact.zIndex['menu']);
}
} else {
unbindListeners();
}

if (isMobileMode) {
currentPanel && currentPanel.previousElementSibling.scrollIntoView({ block: 'nearest', inline: 'nearest' });
}

return () => {
unbindListeners();
ZIndexUtils.clear(currentPanel);
};
}, [activeItemState]);

useUpdateEffect(() => {
const _focusedItemId = ObjectUtils.isNotEmpty(focusedItemInfo.key) ? `${idState}_${focusedItemInfo.key}` : null;

setFocusedItemId(_focusedItemId);
}, [focusedItemInfo]);

useUpdateEffect(() => {
const itemsToProcess = props.model || [];
const processed = createProcessedItems(itemsToProcess, 0, null, '');

setProcessedItems(processed);
}, [props.model]);

useUpdateEffect(() => {
const processedItem = ObjectUtils.isNotEmpty(activeItemState) ? activeItemState : null;

const _visibleItems =
processedItem && processedItem.key === focusedItemInfo.parentKey
? processedItem.items.reduce((items, col) => {
col.forEach((submenu) => {
submenu.items.forEach((a) => {
items.push(a);
});
});

return items;
}, [])
: processedItems;

setVisibleItems(_visibleItems);
}, [focusedItemInfo, activeItemState, processedItems]);

const createProcessedItems = (items, level = 0, parent = {}, parentKey = '', columnIndex) => {
const _processedItems = [];

Expand Down Expand Up @@ -1088,17 +1101,6 @@ export const MegaMenu = React.memo(
styleElementRef.current = DomHandler.removeInlineStyle(styleElementRef.current);
};

useUpdateEffect(() => {
if (attributeSelectorState && elementRef.current) {
elementRef.current.setAttribute(attributeSelectorState, '');
createStyle();
}

return () => {
destroyStyle();
};
}, [attributeSelectorState, props.breakpoint]);

const createCategory = (processedItem, index) => {
const category = processedItem.item;
const iconProps = mergeProps(
Expand Down
Loading