Skip to content

Commit f8d69ae

Browse files
committed
Implement active id changes
1 parent 217689f commit f8d69ae

2 files changed

Lines changed: 13 additions & 11 deletions

File tree

src/Layout/PanelSideBarLayout/PanelSideBar/Context/PanelSideBarContext.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export interface PanelLinkRendererProps<T> {
99
export type MenuItemToggleFn<TMenuItem> = (menuItem: PanelMenuItem<TMenuItem>) => void;
1010

1111
export interface PanelSideBarContextProps<TPanelItem, TMenuItem> {
12-
activePanelId: string;
1312
/**
1413
* The global panel items.
1514
*/
@@ -18,7 +17,6 @@ export interface PanelSideBarContextProps<TPanelItem, TMenuItem> {
1817
* The component used to render the menu item links.
1918
*/
2019
LinkRenderer: ComponentType<PanelLinkRendererProps<TMenuItem>>;
21-
setActivePanel: (panelId: string) => void;
2220
toggledMenuItemIds: string[];
2321
toggleMenuItem: MenuItemToggleFn<TMenuItem>;
2422
}
@@ -36,8 +34,6 @@ export interface PanelSideBarMenuProviderProps<TPanelItem, TMenuItem>
3634
export const PanelSideBarProvider = <TPanelItem, TMenuItem>(props: PanelSideBarMenuProviderProps<TPanelItem, TMenuItem>) => {
3735
const { children, globalItems, LinkRenderer } = props;
3836

39-
const getFirstPanelId = () => globalItems.find((x) => x.id)?.id ?? "";
40-
const [activePanelId, setActivePanelId] = useState(getFirstPanelId());
4137
const [toggledMenuItemIds, setToggledMenuItemIds] = useState<string[]>([]);
4238

4339
const toggleMenuItem: MenuItemToggleFn<TMenuItem> = (menuItem) => {
@@ -52,15 +48,11 @@ export const PanelSideBarProvider = <TPanelItem, TMenuItem>(props: PanelSideBarM
5248
});
5349
};
5450

55-
const setActivePanel = (panelId: string) => setActivePanelId(panelId);
56-
5751
return (
5852
<PanelSideBarContext.Provider
5953
value={{
60-
activePanelId,
6154
globalItems,
6255
LinkRenderer,
63-
setActivePanel,
6456
toggledMenuItemIds,
6557
toggleMenuItem,
6658
}}

src/Layout/PanelSideBarLayout/PanelSideBar/PanelSidebar.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@ import { Button } from "reactstrap";
44
import { usePanelSideBarContext } from "./Context/PanelSideBarContext";
55
import { PanelItem } from "./Definitions/PanelSideBarMenuItem";
66
import { PanelSideBarItem } from "./PanelSideBarItem";
7-
7+
import { useState } from "react";
88
interface PanelSideBarProps {
99
localItems?: PanelItem[];
1010
}
1111

1212
export const PanelSideBar = (props: PanelSideBarProps) => {
1313
const { localItems = [] } = props;
1414

15-
const { activePanelId, globalItems, LinkRenderer, setActivePanel, toggledMenuItemIds, toggleMenuItem } = usePanelSideBarContext();
15+
const { globalItems, LinkRenderer, toggledMenuItemIds, toggleMenuItem } = usePanelSideBarContext();
16+
17+
let initialActivePanel: string;
18+
if (localItems.length > 0) {
19+
const [firstLocalItem] = localItems;
20+
initialActivePanel = firstLocalItem.id;
21+
} else {
22+
initialActivePanel = globalItems.find((x) => x.id)?.id ?? "";
23+
}
24+
25+
const [activePanelId, setActivePanelId] = useState<string>(initialActivePanel);
1626

1727
const activePanel: PanelItem = globalItems.find((x) => x.id === activePanelId);
1828
const localActivePanel: PanelItem | undefined = localItems?.find((x) => x.id === activePanelId);
@@ -24,7 +34,7 @@ export const PanelSideBar = (props: PanelSideBarProps) => {
2434
color="primary"
2535
outline
2636
className={classNames("tile", { active: activePanelId === id })}
27-
onClick={() => setActivePanel(id)}
37+
onClick={() => setActivePanelId(id)}
2838
title={title}
2939
disabled={disabled}
3040
>

0 commit comments

Comments
 (0)