Skip to content

Commit 1c80ce1

Browse files
author
Gianmarco Manni
committed
stateless menu items
1 parent 6d6cfdd commit 1c80ce1

4 files changed

Lines changed: 12 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
- Possibility to set fully control `menuItems`
1312
- Possibility to provide custom render component, e.g. skeleton, for mananing special case like asynchronus render (TODO)
1413
- Possibility to dinamically open or close `sidebar`
1514

@@ -21,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2120
### Fixed
2221

2322
- When `footer` is null, the whole section will not be rendered
23+
- Unique key prop in a list warning
2424

2525
### Removed
2626

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Context, createContext, useCallback, useContext, useState } from "react";
1+
import React, { Context, createContext, useContext, useEffect, useMemo, useState } from "react";
22
import { getActivePanel } from "../Utils/getActivePanel";
33
import { PanelSideBarContextProps } from "./PanelSideBarContextProps";
44

@@ -13,7 +13,7 @@ export interface PanelSideBarMenuProviderProps<TPanelItemId extends string, TPan
1313
*/
1414
children: React.ReactNode;
1515

16-
/**
16+
/**
1717
* if the sidebar should be open by default.
1818
*/
1919
sidebarOpenByDefault?: boolean;
@@ -23,14 +23,13 @@ export const PanelSideBarProvider = <TPanelItemId extends string, TPanelItem>(
2323
props: PanelSideBarMenuProviderProps<TPanelItemId, TPanelItem>,
2424
) => {
2525
const { children, defaultActivePanelId, sidebarOpenByDefault = true, menuItems: defaultMenuItems } = props;
26-
const [menuItems, setMenuItems] = useState(defaultMenuItems);
26+
const menuItems = useMemo(() => defaultMenuItems, [defaultMenuItems]);
2727

2828
const [isSidebarOpen, setIsSidebarOpen] = useState(sidebarOpenByDefault);
2929
const toggleSidebar = () => setIsSidebarOpen((prev) => !prev);
3030

3131
const [activePanelId, setActivePanelId] = useState(getActivePanel(menuItems, defaultActivePanelId)?.id);
3232
const setActivePanel = (panelId: TPanelItemId) => setActivePanelId(panelId);
33-
3433
const [toggledMenuItemIds, setToggledMenuItemIds] = useState<TPanelItemId[]>(activePanelId ? [activePanelId] : []);
3534
const toggleMenuItem: MenuItemToggleFn<TPanelItemId> = (menuItemId) => {
3635
setToggledMenuItemIds((prev) => {
@@ -44,24 +43,22 @@ export const PanelSideBarProvider = <TPanelItemId extends string, TPanelItem>(
4443
});
4544
};
4645

47-
const untoggleMenuItems = () => setToggledMenuItemIds([]);
48-
49-
const recomputeActivePanel = useCallback(() => {
46+
useEffect(() => {
5047
const activePanelId = getActivePanel(menuItems, defaultActivePanelId)?.id;
5148
setActivePanelId(activePanelId);
5249
if (activePanelId) {
53-
setToggledMenuItemIds(prev => prev.includes(activePanelId) ? prev : [...prev, activePanelId]);
50+
setToggledMenuItemIds((prev) => (prev.includes(activePanelId) ? prev : [...prev, activePanelId]));
5451
}
55-
}, []);
52+
}, [menuItems]);
53+
54+
const untoggleMenuItems = () => setToggledMenuItemIds([]);
5655

5756
return (
5857
<PanelSideBarContext.Provider
5958
value={{
6059
menuItems,
61-
setMenuItems,
6260
activePanelId,
6361
setActivePanel,
64-
recomputeActivePanel,
6562
toggledMenuItemIds,
6663
toggleMenuItem,
6764
untoggleMenuItems,

src/lib/Layout/PanelSideBarLayout/PanelSideBar/Context/PanelSideBarContextProps.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Dispatch, SetStateAction } from "react";
21
import { PanelItem } from "../Definitions/PanelItem";
32
import { MenuItemToggleFn } from "./PanelSideBarContext";
43

@@ -12,10 +11,6 @@ export interface PanelSideBarContextProps<TPanelItemId extends string, TPanelIte
1211
*/
1312
menuItems: PanelItem<TPanelItemId, TPanelItem>[];
1413

15-
/**
16-
* The setter for menu items.
17-
*/
18-
setMenuItems: Dispatch<SetStateAction<PanelItem<TPanelItemId, TPanelItem>[]>>;
1914
/**
2015
* The function used to set a panel as active
2116
* @param panelId The panel item identifier
@@ -40,11 +35,6 @@ export interface PanelSideBarContextProps<TPanelItemId extends string, TPanelIte
4035
*/
4136
untoggleMenuItems: () => void;
4237

43-
/**
44-
* Function for recomputing active item
45-
*/
46-
recomputeActivePanel: () => void;
47-
4838
/**
4939
* If the sidebar is currently open or not
5040
*/

src/lib/Layout/PanelSideBarLayout/PanelSideBar/PanelSidebar.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ export const PanelSideBar = <TPanelItemId extends string, TPanelItem>(props: Pan
5252
};
5353

5454
const panelItemsRenderer = (items: PanelItem<TPanelItemId, TPanelItem>[]) =>
55-
items?.map((item) =>
55+
items?.map((item, index) =>
5656
renderTilesAsLinks ? (
57-
<LinkRenderer item={item}>
57+
<LinkRenderer key={index} item={item}>
5858
<ButtonIcon item={item} />
5959
</LinkRenderer>
6060
) : (
61-
<ButtonIcon item={item} />
61+
<ButtonIcon key={index} item={item} />
6262
),
6363
);
6464

0 commit comments

Comments
 (0)