Refactor DashboardSidebar to support recursive item mapping
- Introduced a recursive function to map sidebar items and their children, enhancing the structure and maintainability of the component. - Added onClick functionality for items with paths, improving navigation capabilities. - Improved code readability by restructuring the item mapping logic.
This commit is contained in:
parent
b74a4bb174
commit
df1115022a
@ -38,18 +38,35 @@ const DashboardSidebar = ({
|
||||
if (onCollapse) onCollapse(newCollapsed)
|
||||
}
|
||||
|
||||
// Add onClick to each item
|
||||
const _items = items.map((item) => {
|
||||
if (item?.type == 'divider') {
|
||||
// Recursive function to map items and their children
|
||||
const mapItemsRecursively = (items) => {
|
||||
return items.map((item) => {
|
||||
if (item?.type === 'divider') {
|
||||
return item
|
||||
}
|
||||
return {
|
||||
|
||||
const mappedItem = {
|
||||
key: item.key,
|
||||
icon: item.icon,
|
||||
label: item.label,
|
||||
onClick: () => navigate(item.path)
|
||||
label: item.label
|
||||
}
|
||||
|
||||
// Add onClick for items with paths (leaf nodes)
|
||||
if (item.path) {
|
||||
mappedItem.onClick = () => navigate(item.path)
|
||||
}
|
||||
|
||||
// Recursively map children if they exist
|
||||
if (item?.children && Array.isArray(item.children)) {
|
||||
mappedItem.children = mapItemsRecursively(item.children)
|
||||
}
|
||||
|
||||
return mappedItem
|
||||
})
|
||||
}
|
||||
|
||||
// Map items recursively
|
||||
const _items = mapItemsRecursively(items)
|
||||
|
||||
if (isMobile) {
|
||||
return (
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user