-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(projects): the vercal-mix reconstruction is complete
- Loading branch information
Showing
8 changed files
with
303 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/layouts/modules/global-menu/components/HorizontalMenu.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { Route } from '@sa/simple-router'; | ||
import type { MenuInfo } from 'rc-menu/lib/interface'; | ||
import { useRouterPush } from '@/hooks/common/routerPush'; | ||
|
||
function getSelectKey(route: Route) { | ||
const { hideInMenu, activeMenu } = route.meta; | ||
|
||
const name = route.name as string; | ||
|
||
const routeName = (hideInMenu ? activeMenu : name) || name; | ||
|
||
return [routeName]; | ||
} | ||
|
||
const HorizontalMenu = () => { | ||
const route = useRoute(); | ||
|
||
const menus = useMenu(); | ||
|
||
const router = useRouterPush(); | ||
|
||
const selectKey = getSelectKey(route); | ||
|
||
function handleClickMenu(menuInfo: MenuInfo) { | ||
router.menuPush(menuInfo.key); | ||
} | ||
|
||
return ( | ||
<AMenu | ||
mode="horizontal" | ||
items={menus} | ||
inlineIndent={18} | ||
onSelect={handleClickMenu} | ||
selectedKeys={selectKey} | ||
/> | ||
); | ||
}; | ||
|
||
export default HorizontalMenu; |
118 changes: 118 additions & 0 deletions
118
src/layouts/modules/global-menu/components/VerticalMenu.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { SimpleScrollbar } from '@sa/materials'; | ||
import type { Route, RouteRecordNormalized } from '@sa/simple-router'; | ||
import type { MenuInfo } from 'rc-menu/lib/interface'; | ||
import type { MenuProps } from 'antd'; | ||
import { getSiderCollapse } from '@/store/slice/app'; | ||
import { getThemeSettings } from '@/store/slice/theme'; | ||
|
||
interface LevelKeysProps { | ||
key?: string; | ||
children?: LevelKeysProps[]; | ||
} | ||
|
||
const getLevelKeys = (items1: LevelKeysProps[]) => { | ||
const key: Record<string, number> = {}; | ||
const func = (items2: LevelKeysProps[], level = 1) => { | ||
items2.forEach(item => { | ||
if (item.key) { | ||
key[item.key] = level; | ||
} | ||
if (item.children) { | ||
func(item.children, level + 1); | ||
} | ||
}); | ||
}; | ||
func(items1); | ||
return key; | ||
}; | ||
|
||
function getSelectKey(route: Route) { | ||
const { hideInMenu, activeMenu } = route.meta; | ||
|
||
const name = route.name as string; | ||
|
||
const routeName = (hideInMenu ? activeMenu : name) || name; | ||
|
||
return [routeName]; | ||
} | ||
|
||
const getSelectedMenuKeyPath = (matches: RouteRecordNormalized[]) => { | ||
const result = matches.reduce((acc: string[], match, index) => { | ||
if (index < matches.length - 1 && match.name) { | ||
acc.push(match.name); | ||
} | ||
return acc; | ||
}, []); | ||
|
||
return result; | ||
}; | ||
|
||
const VerticalMenu = memo(() => { | ||
const menus = useMenu(); | ||
|
||
const route = useRoute(); | ||
const levelKeys = getLevelKeys(menus as LevelKeysProps[]); | ||
|
||
const themeSettings = useAppSelector(getThemeSettings); | ||
|
||
const selectedKeys = getSelectKey(route); | ||
|
||
const router = useRouterPush(); | ||
const matches = route.matched; | ||
const openKeys = () => { | ||
return getSelectedMenuKeyPath(matches); | ||
}; | ||
|
||
const inlineCollapsed = useAppSelector(getSiderCollapse); | ||
|
||
const [stateOpenKeys, setStateOpenKeys] = useState<string[]>(openKeys()); | ||
|
||
function handleClickMenu(menuInfo: MenuInfo) { | ||
router.menuPush(menuInfo.key); | ||
} | ||
|
||
const onOpenChange: MenuProps['onOpenChange'] = keys => { | ||
if (keys.includes('rc-menu-more')) { | ||
setStateOpenKeys(keys); | ||
return; | ||
} | ||
|
||
const currentOpenKey = keys.find(key => !stateOpenKeys.includes(key)); | ||
|
||
// open | ||
if (currentOpenKey && themeSettings.isOnlyExpandCurrentParentMenu) { | ||
const repeatIndex = keys | ||
.filter(key => key !== currentOpenKey) | ||
.findIndex(key => levelKeys[key] === levelKeys[currentOpenKey]); | ||
|
||
setStateOpenKeys( | ||
keys | ||
// remove repeat key | ||
.filter((_, index) => index !== repeatIndex) | ||
// remove current level all child | ||
.filter(key => levelKeys[key] <= levelKeys[currentOpenKey]) | ||
); | ||
} else { | ||
// close | ||
setStateOpenKeys(keys); | ||
} | ||
}; | ||
|
||
return ( | ||
<SimpleScrollbar> | ||
<AMenu | ||
mode="inline" | ||
items={menus} | ||
inlineCollapsed={inlineCollapsed} | ||
openKeys={stateOpenKeys} | ||
onOpenChange={onOpenChange} | ||
selectedKeys={selectedKeys} | ||
onSelect={handleClickMenu} | ||
className="size-full bg-container transition-300 border-0!" | ||
inlineIndent={18} | ||
/> | ||
</SimpleScrollbar> | ||
); | ||
}); | ||
|
||
export default VerticalMenu; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/layouts/modules/global-menu/modules/VerticalMenu.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { SimpleScrollbar } from '@sa/materials'; | ||
import type { Route, RouteRecordNormalized } from '@sa/simple-router'; | ||
import type { MenuInfo } from 'rc-menu/lib/interface'; | ||
import type { MenuProps } from 'antd'; | ||
import { getSiderCollapse } from '@/store/slice/app'; | ||
import { getThemeSettings } from '@/store/slice/theme'; | ||
|
||
interface LevelKeysProps { | ||
key?: string; | ||
children?: LevelKeysProps[]; | ||
} | ||
|
||
interface Props { | ||
menus: MenuProps['items']; | ||
} | ||
|
||
const getLevelKeys = (items1: LevelKeysProps[]) => { | ||
const key: Record<string, number> = {}; | ||
|
||
if (!items1) return key; | ||
const func = (items2: LevelKeysProps[], level = 1) => { | ||
items2.forEach(item => { | ||
if (item.key) { | ||
key[item.key] = level; | ||
} | ||
if (item.children) { | ||
func(item.children, level + 1); | ||
} | ||
}); | ||
}; | ||
func(items1); | ||
return key; | ||
}; | ||
|
||
function getSelectKey(route: Route) { | ||
const { hideInMenu, activeMenu } = route.meta; | ||
|
||
const name = route.name as string; | ||
|
||
const routeName = (hideInMenu ? activeMenu : name) || name; | ||
|
||
return [routeName]; | ||
} | ||
|
||
const getSelectedMenuKeyPath = (matches: RouteRecordNormalized[]) => { | ||
const result = matches.reduce((acc: string[], match, index) => { | ||
if (index < matches.length - 1 && match.name) { | ||
acc.push(match.name); | ||
} | ||
return acc; | ||
}, []); | ||
|
||
return result; | ||
}; | ||
|
||
const VerticalMenu: FC<Props> = memo(({ menus }) => { | ||
console.log(menus); | ||
|
||
const route = useRoute(); | ||
const levelKeys = getLevelKeys(menus as LevelKeysProps[]); | ||
|
||
const themeSettings = useAppSelector(getThemeSettings); | ||
|
||
const selectedKeys = getSelectKey(route); | ||
|
||
const router = useRouterPush(); | ||
const matches = route.matched; | ||
const openKeys = () => { | ||
return getSelectedMenuKeyPath(matches); | ||
}; | ||
|
||
const inlineCollapsed = useAppSelector(getSiderCollapse); | ||
|
||
const siderCollapse = themeSettings.layout.mode === 'vertical-mix' ? false : inlineCollapsed; | ||
|
||
const [stateOpenKeys, setStateOpenKeys] = useState<string[]>(openKeys()); | ||
|
||
function handleClickMenu(menuInfo: MenuInfo) { | ||
router.menuPush(menuInfo.key); | ||
} | ||
|
||
const onOpenChange: MenuProps['onOpenChange'] = keys => { | ||
if (keys.includes('rc-menu-more')) { | ||
setStateOpenKeys(keys); | ||
return; | ||
} | ||
|
||
const currentOpenKey = keys.find(key => !stateOpenKeys.includes(key)); | ||
|
||
// open | ||
if (currentOpenKey && themeSettings.isOnlyExpandCurrentParentMenu) { | ||
const repeatIndex = keys | ||
.filter(key => key !== currentOpenKey) | ||
.findIndex(key => levelKeys[key] === levelKeys[currentOpenKey]); | ||
|
||
setStateOpenKeys( | ||
keys | ||
// remove repeat key | ||
.filter((_, index) => index !== repeatIndex) | ||
// remove current level all child | ||
.filter(key => levelKeys[key] <= levelKeys[currentOpenKey]) | ||
); | ||
} else { | ||
// close | ||
setStateOpenKeys(keys); | ||
} | ||
}; | ||
|
||
return ( | ||
<SimpleScrollbar> | ||
<AMenu | ||
mode="inline" | ||
items={menus} | ||
inlineCollapsed={siderCollapse} | ||
openKeys={stateOpenKeys} | ||
onOpenChange={onOpenChange} | ||
selectedKeys={selectedKeys} | ||
onSelect={handleClickMenu} | ||
className="size-full bg-container transition-300 border-0!" | ||
inlineIndent={18} | ||
/> | ||
</SimpleScrollbar> | ||
); | ||
}); | ||
|
||
export default VerticalMenu; |
Oops, something went wrong.