[common/components] refactor: 메뉴 활성화 조건 변경(menuCd)
This commit is contained in:
@@ -42,6 +42,7 @@ export function LNB({
|
||||
handleMenuClick?: (item: MenuItem) => void;
|
||||
}) {
|
||||
const [defaultOpenMenuIds, setDefaultOpenMenuIds] = useState<number[]>([]);
|
||||
const [currentMenuCode, setCurrentMenuCode] = useState<number | null>(null);
|
||||
const prevPathRef = useRef(currentPath);
|
||||
|
||||
const cookieSiteId = getCookie("site-id-key") ?? "";
|
||||
@@ -70,6 +71,7 @@ export function LNB({
|
||||
if (!menuList || !currentPath) return;
|
||||
|
||||
const matchedMenuIds: number[] = [];
|
||||
let matchedMenuCode: number | null = null;
|
||||
|
||||
const findMatchedMenus = (items: MenuItem[]) => {
|
||||
items.forEach((item) => {
|
||||
@@ -77,25 +79,32 @@ export function LNB({
|
||||
const parentIds = getParentIds(item.menuCd)(menuList);
|
||||
matchedMenuIds.push(...parentIds, item.menuCd);
|
||||
}
|
||||
if (item.children) findMatchedMenus(item.children);
|
||||
});
|
||||
};
|
||||
|
||||
if (item.children) {
|
||||
findMatchedMenus(item.children);
|
||||
}
|
||||
const findMatchedMenuCode = (items: MenuItem[]) => {
|
||||
items.forEach((item) => {
|
||||
if (matchedMenuCode) return;
|
||||
if (currentPath === item.urlPath) matchedMenuCode = item.menuCd;
|
||||
if (item.children) findMatchedMenuCode(item.children);
|
||||
});
|
||||
};
|
||||
|
||||
findMatchedMenus(menuList);
|
||||
findMatchedMenuCode(menuList);
|
||||
|
||||
if (matchedMenuIds.length > 0) {
|
||||
if (matchedMenuIds.length > 0)
|
||||
setDefaultOpenMenuIds((prev) => [
|
||||
...new Set([...prev, ...matchedMenuIds]),
|
||||
]);
|
||||
}
|
||||
|
||||
if (prevPathRef.current !== currentPath) {
|
||||
setDefaultOpenMenuIds(matchedMenuIds);
|
||||
prevPathRef.current = currentPath;
|
||||
}
|
||||
|
||||
if (matchedMenuCode) setCurrentMenuCode(matchedMenuCode);
|
||||
}, [menuList, currentPath]);
|
||||
|
||||
function MenuItem({
|
||||
@@ -106,7 +115,8 @@ export function LNB({
|
||||
isSubMenu?: boolean;
|
||||
}) {
|
||||
const { open: isSidebarOpen, setOpen } = useSidebar();
|
||||
const isMatched = isPathMatched(item.urlPath);
|
||||
const isMatched =
|
||||
isPathMatched(item.urlPath) && currentMenuCode === item.menuCd;
|
||||
|
||||
const handleClick = () => {
|
||||
if (!isSidebarOpen) setOpen(true);
|
||||
|
||||
Reference in New Issue
Block a user