Skip to content

Commit

Permalink
Merge pull request #48 from sc420/36-add-tooltip-when-hovering-on-the…
Browse files Browse the repository at this point in the history
…-collapsed-feature-icons

show tooltip when collapsed
  • Loading branch information
sc420 authored Jan 20, 2024
2 parents 9099861 + 9bf53d4 commit e8af247
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
1 change: 1 addition & 0 deletions interactive-computational-graph/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const App: FunctionComponent = () => {
{/* Sidebar */}
<Sidebar isSidebarOpen={isSidebarOpen} onToggleSidebar={toggleSidebar}>
<FeatureNavigator
isSidebarOpen={isSidebarOpen}
selectedItem={selectedFeature}
onItemClick={toggleFeature}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import FeatureNavigator from "./FeatureNavigator";
test("should render the selected item", () => {
const handleItemClick = jest.fn();
render(
<FeatureNavigator selectedItem="add-nodes" onItemClick={handleItemClick} />,
<FeatureNavigator
isSidebarOpen={true}
selectedItem="add-nodes"
onItemClick={handleItemClick}
/>,
);

const addNodesItem = screen.getByText("Add Nodes");
Expand All @@ -14,7 +18,11 @@ test("should render the selected item", () => {
test("should trigger event when clicking the non-selected item", () => {
const handleItemClick = jest.fn();
render(
<FeatureNavigator selectedItem={null} onItemClick={handleItemClick} />,
<FeatureNavigator
isSidebarOpen={true}
selectedItem={null}
onItemClick={handleItemClick}
/>,
);

const addNodesItem = screen.getByText("Add Nodes");
Expand All @@ -25,7 +33,11 @@ test("should trigger event when clicking the non-selected item", () => {
test("should trigger event when clicking the selected item", () => {
const handleItemClick = jest.fn();
render(
<FeatureNavigator selectedItem="add-nodes" onItemClick={handleItemClick} />,
<FeatureNavigator
isSidebarOpen={true}
selectedItem="add-nodes"
onItemClick={handleItemClick}
/>,
);

const addNodesItem = screen.getByText("Add Nodes");
Expand Down
54 changes: 37 additions & 17 deletions interactive-computational-graph/src/components/FeatureNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import {
ListItemButton,
ListItemIcon,
ListItemText,
Tooltip,
} from "@mui/material";
import { type FunctionComponent } from "react";
import { useCallback, type FunctionComponent } from "react";
import type SelectedFeature from "../features/SelectedFeature";

interface FeatureNavigatorProps {
isSidebarOpen: boolean;
selectedItem: string | null;
onItemClick: (item: SelectedFeature | null) => void;
}
Expand All @@ -25,6 +27,7 @@ interface FeatureItem {
}

const FeatureNavigator: FunctionComponent<FeatureNavigatorProps> = ({
isSidebarOpen,
selectedItem,
onItemClick,
}) => {
Expand Down Expand Up @@ -61,25 +64,42 @@ const FeatureNavigator: FunctionComponent<FeatureNavigatorProps> = ({
},
];

const handleItemClick = (id: SelectedFeature): void => {
const newSelectedItem = id !== selectedItem ? id : null;
onItemClick(newSelectedItem);
};
const handleItemClick = useCallback(
(id: SelectedFeature): void => {
const newSelectedItem = id !== selectedItem ? id : null;
onItemClick(newSelectedItem);
},
[onItemClick, selectedItem],
);

return (
<List component="nav">
{featureItems.map((item) => (
<ListItemButton
key={item.id}
selected={selectedItem === item.id}
onClick={() => {
handleItemClick(item.id);
}}
>
<ListItemIcon>{item.icon}</ListItemIcon>
<ListItemText primary={item.text} />
</ListItemButton>
))}
{featureItems.map((item) =>
isSidebarOpen ? (
<ListItemButton
key={item.id}
selected={selectedItem === item.id}
onClick={() => {
handleItemClick(item.id);
}}
>
<ListItemIcon>{item.icon}</ListItemIcon>
<ListItemText primary={item.text} />
</ListItemButton>
) : (
<Tooltip key={item.id} title={item.text} placement="right">
<ListItemButton
selected={selectedItem === item.id}
onClick={() => {
handleItemClick(item.id);
}}
>
<ListItemIcon>{item.icon}</ListItemIcon>
<ListItemText primary={item.text} />
</ListItemButton>
</Tooltip>
),
)}
</List>
);
};
Expand Down

0 comments on commit e8af247

Please sign in to comment.