Skip to content

Commit 2ba9db9

Browse files
author
Gianmarco Manni
committed
Fix settings sidebar to not disappear, fix style of nav-item icon to be the same in case of Children and not
1 parent d6a5b05 commit 2ba9db9

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

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

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

1111
export interface PanelSideBarContextProps<TPanelItem, TMenuItem> {
12+
activePanelId: string;
1213
/**
1314
* The global panel items.
1415
*/
1516
globalItems: PanelItem<TPanelItem, TMenuItem>[];
1617
/**
1718
* The component used to render the menu item links.
1819
*/
20+
setActivePanel: (panelId: string) => void;
1921
LinkRenderer: ComponentType<PanelLinkRendererProps<TMenuItem>>;
2022
toggledMenuItemIds: string[];
2123
toggleMenuItem: MenuItemToggleFn<TMenuItem>;
@@ -34,8 +36,13 @@ export interface PanelSideBarMenuProviderProps<TPanelItem, TMenuItem>
3436
export const PanelSideBarProvider = <TPanelItem, TMenuItem>(props: PanelSideBarMenuProviderProps<TPanelItem, TMenuItem>) => {
3537
const { children, globalItems, LinkRenderer } = props;
3638

39+
const getFirstPanelId = () => globalItems.find((x) => x.id)?.id ?? "";
40+
const [activePanelId, setActivePanelId] = useState(getFirstPanelId());
41+
3742
const [toggledMenuItemIds, setToggledMenuItemIds] = useState<string[]>([]);
3843

44+
const setActivePanel = (panelId: string) => setActivePanelId(panelId);
45+
3946
const toggleMenuItem: MenuItemToggleFn<TMenuItem> = (menuItem) => {
4047
setToggledMenuItemIds((prev) => {
4148
const idExists = !!prev.find((id) => id == menuItem.id);
@@ -51,8 +58,10 @@ export const PanelSideBarProvider = <TPanelItem, TMenuItem>(props: PanelSideBarM
5158
return (
5259
<PanelSideBarContext.Provider
5360
value={{
61+
activePanelId,
5462
globalItems,
5563
LinkRenderer,
64+
setActivePanel,
5665
toggledMenuItemIds,
5766
toggleMenuItem,
5867
}}

src/Layout/PanelSideBarLayout/PanelSideBar/PanelSideBarItem.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const PanelSideBarItem = (props: PanelSideBarItemProps) => {
3535
{hasChildren ? (
3636
<div className={classNames({ dropend: !isOpen })}>
3737
<a role="button" className={classNames("nav-link", { "dropdown-toggle": hasChildren })}>
38-
{item.icon && <FontAwesomeIcon icon={item.icon as IconProp} fixedWidth />}
38+
{item.icon && <FontAwesomeIcon className="me-2" icon={item.icon as IconProp} />}
3939
<div className="text-justify">{item.title}</div>
4040
</a>
4141
</div>

src/Layout/PanelSideBarLayout/PanelSideBar/PanelSidebar.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,32 @@ interface PanelSideBarProps {
1212
export const PanelSideBar = (props: PanelSideBarProps) => {
1313
const { localItems = [] } = props;
1414

15-
const { globalItems, LinkRenderer, toggledMenuItemIds, toggleMenuItem } = usePanelSideBarContext();
15+
let localMenuId: string | null = null;
1616

17-
let initialActivePanel: string;
1817
if (localItems?.length > 0) {
1918
const [firstLocalItem] = localItems;
20-
initialActivePanel = firstLocalItem.id;
21-
} else {
22-
initialActivePanel = globalItems.find((x) => x.id)?.id ?? "";
19+
localMenuId = firstLocalItem.id;
2320
}
2421

25-
const [activePanelId, setActivePanelId] = useState<string>(initialActivePanel);
22+
const [localItemId, setLocalItemId] = useState<string | null>(localMenuId);
2623

27-
const activePanel: PanelItem = globalItems.find((x) => x.id === activePanelId);
28-
const localActivePanel: PanelItem | undefined = localItems?.find((x) => x.id === activePanelId);
24+
const { activePanelId, globalItems, LinkRenderer, setActivePanel, toggledMenuItemIds, toggleMenuItem } = usePanelSideBarContext();
25+
26+
const localActivePanelId = localItemId ?? activePanelId;
27+
const activePanel: PanelItem = globalItems.find((x) => x.id === localActivePanelId);
28+
const localActivePanel: PanelItem | undefined = localItems?.find((x) => x.id === localActivePanelId);
2929

3030
const panelItemsRenderer = (items: PanelItem[]) =>
3131
items?.map(({ disabled, icon, id, title }) => (
3232
<Button
3333
key={id}
3434
color="primary"
3535
outline
36-
className={classNames("tile", { active: activePanelId === id })}
37-
onClick={() => setActivePanelId(id)}
36+
className={classNames("tile", { active: localActivePanelId === id })}
37+
onClick={() => {
38+
setLocalItemId(null);
39+
setActivePanel(id);
40+
}}
3841
title={title}
3942
disabled={disabled}
4043
>

0 commit comments

Comments
 (0)