Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'showCreate' prop to sidebar #449

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading