Skip to content

Commit

Permalink
Merge pull request #449 from equinor/feature/sidebar-hide-create
Browse files Browse the repository at this point in the history
Add 'showCreate' prop to sidebar
  • Loading branch information
mariush2 authored Feb 20, 2024
2 parents fa918b8 + c346ef6 commit bdac608
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/components/Navigation/SideBar/SideBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,34 @@ test('Render Create button correctly when open', async () => {
const createButton = screen.getAllByRole('button')[0];
expect(createButton).toHaveStyleRule('height', '36px');
});

test('Hides create button when showCreate=false', async () => {
const createLabel = faker.animal.dog();
const handleOnCreate = vi.fn();
window.localStorage.setItem(
'amplify-sidebar-state',
JSON.stringify({ isOpen: false })
);
render(
<SideBar
createLabel={createLabel}
onCreate={handleOnCreate}
showCreate={false}
>
{defaultMenuItems.map((m) => (
<SideBar.Item key={m.name} {...m} />
))}
</SideBar>,
{ wrapper: SideBarProvider }
);
const user = userEvent.setup();

await user.click(screen.getAllByRole('button')[0]);

const icons = screen.getAllByTestId('eds-icon-path');
for (const icon of icons) {
expect(icon).not.toHaveAttribute('d', add.svgPathData);
}

expect(screen.queryByText(createLabel)).not.toBeInTheDocument();
});
5 changes: 3 additions & 2 deletions src/components/Navigation/SideBar/SideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const TopContainer = styled.div`

interface SidebarProps extends HTMLAttributes<HTMLDivElement> {
children: ReactNode;
showCreate?: boolean;
}

interface SideBarWithoutCreate extends SidebarProps {
Expand All @@ -61,7 +62,7 @@ export const SideBar = forwardRef<
HTMLDivElement,
SidebarWithCreate | SideBarWithoutCreate
>((props, ref) => {
const { children } = props;
const { children, showCreate = true } = props;
const { isOpen, setIsOpen } = useSideBar();

const handleToggle = () => {
Expand All @@ -75,7 +76,7 @@ export const SideBar = forwardRef<
data-testid="sidebar"
>
<TopContainer>
{props.onCreate && (
{props.onCreate && showCreate && (
<CreateItem
createLabel={props.createLabel}
onCreate={props.onCreate}
Expand Down

0 comments on commit bdac608

Please sign in to comment.