-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use StudioContentMenu in SettingsModal
- Loading branch information
Showing
20 changed files
with
541 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
.leftNavWrapper { | ||
overflow-y: auto; | ||
width: 250px; | ||
} | ||
|
||
.contentWrapper { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...ader/SubHeader/SettingsModalButton/SettingsModal/hooks/useSettingsModalMenuTabConfigs.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
import type { SettingsModalTabId } from '../../../../../../types/SettingsModalTabId'; | ||
import { | ||
InformationSquareIcon, | ||
SidebarBothIcon, | ||
ShieldLockIcon, | ||
TimerStartIcon, | ||
} from '@studio/icons'; | ||
import { useTranslation } from 'react-i18next'; | ||
import type { StudioMenuTabType } from '@studio/components'; | ||
|
||
const aboutTabId: SettingsModalTabId = 'about'; | ||
const setupTabId: SettingsModalTabId = 'setup'; | ||
const policyTabId: SettingsModalTabId = 'policy'; | ||
const accessControlTabId: SettingsModalTabId = 'access_control'; | ||
|
||
export const useSettingsModalMenuTabConfigs = () => { | ||
const { t } = useTranslation(); | ||
|
||
const getMenuTabConfigs = (): StudioMenuTabType<SettingsModalTabId>[] => { | ||
return [ | ||
{ | ||
tabId: aboutTabId, | ||
tabName: t(`settings_modal.left_nav_tab_${aboutTabId}`), | ||
icon: <InformationSquareIcon />, | ||
}, | ||
{ | ||
tabId: setupTabId, | ||
tabName: t(`settings_modal.left_nav_tab_${setupTabId}`), | ||
icon: <SidebarBothIcon />, | ||
}, | ||
{ | ||
tabId: policyTabId, | ||
tabName: t(`settings_modal.left_nav_tab_${policyTabId}`), | ||
icon: <ShieldLockIcon />, | ||
}, | ||
{ | ||
tabId: accessControlTabId, | ||
tabName: t(`settings_modal.left_nav_tab_${accessControlTabId}`), | ||
icon: <TimerStartIcon />, | ||
}, | ||
]; | ||
}; | ||
|
||
return { getMenuTabConfigs }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import type { SettingsModalTab } from './SettingsModalTab'; | ||
import type { SettingsModalTabId } from './SettingsModalTabId'; | ||
|
||
export type SettingsModalHandle = { | ||
openSettings: (tab?: SettingsModalTab) => void; | ||
openSettings: (tab?: SettingsModalTabId) => void; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type SettingsModalTabId = 'about' | 'setup' | 'policy' | 'access_control'; |
4 changes: 4 additions & 0 deletions
4
...tend/libs/studio-components/src/components/StudioContentMenu/StudioContentMenu.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.tabsContainer { | ||
background-color: var(--fds-semantic-surface-action-second-subtle); | ||
height: 100%; | ||
} |
60 changes: 60 additions & 0 deletions
60
...end/libs/studio-components/src/components/StudioContentMenu/StudioContentMenu.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React from 'react'; | ||
import type { Meta, StoryFn } from '@storybook/react'; | ||
import { StudioContentMenu } from './StudioContentMenu'; | ||
import { BookIcon, VideoIcon, QuestionmarkDiamondIcon, ExternalLinkIcon } from '@studio/icons'; | ||
import { StudioContentMenuWrapper } from './StudioContentMenuWrapper'; | ||
|
||
type Story = StoryFn<typeof StudioContentMenu>; | ||
|
||
const meta: Meta = { | ||
title: 'Components/StudioContentMenu', | ||
component: StudioContentMenu, | ||
argTypes: { | ||
contentTabs: { | ||
control: 'object', | ||
description: | ||
'Array of menu tabs with icons, names, and ids. Add prop `to` if tab should navigate to a different url', | ||
table: { | ||
type: { summary: 'StudioMenuTabType<TabId>[]' }, | ||
}, | ||
}, | ||
selectedTabId: { | ||
table: { disable: true }, | ||
}, | ||
onChangeTab: { | ||
table: { disable: true }, | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
export const Preview: Story = (args) => ( | ||
<StudioContentMenuWrapper {...args}></StudioContentMenuWrapper> | ||
); | ||
|
||
Preview.args = { | ||
contentTabs: [ | ||
{ | ||
tabId: 'booksTab', | ||
tabName: 'Bøker', | ||
icon: <BookIcon />, | ||
}, | ||
{ | ||
tabId: 'videosTab', | ||
tabName: 'Filmer', | ||
icon: <VideoIcon />, | ||
}, | ||
{ | ||
tabId: 'tabWithVeryLongTabName', | ||
tabName: 'LoremIpsumLoremIpsumLoremIpsum', | ||
icon: <QuestionmarkDiamondIcon />, | ||
}, | ||
{ | ||
tabId: 'tabAsLink', | ||
tabName: 'Gå til Designsystemet', | ||
icon: <ExternalLinkIcon />, | ||
to: 'https://next.storybook.designsystemet.no', | ||
}, | ||
], | ||
}; |
157 changes: 157 additions & 0 deletions
157
frontend/libs/studio-components/src/components/StudioContentMenu/StudioContentMenu.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { userEvent } from '@testing-library/user-event'; | ||
import type { StudioContentMenuProps } from './StudioContentMenu'; | ||
import { StudioContentMenu } from './StudioContentMenu'; | ||
import type { StudioMenuTabType } from './types/StudioMenuTabType'; | ||
|
||
type StudioMenuTabName = 'tab1' | 'tab2' | 'tab3'; | ||
|
||
const onChangeTabMock = jest.fn(); | ||
|
||
const tab1Name = 'My tab'; | ||
const tab1Id: StudioMenuTabName = 'tab1'; | ||
const tab1: StudioMenuTabType<StudioMenuTabName> = { | ||
tabName: tab1Name, | ||
tabId: tab1Id, | ||
icon: <svg></svg>, | ||
}; | ||
const tab2Name = 'My second tab'; | ||
const tab2: StudioMenuTabType<StudioMenuTabName> = { | ||
tabName: tab2Name, | ||
tabId: 'tab2', | ||
icon: <svg></svg>, | ||
}; | ||
|
||
describe('StudioContentMenu', () => { | ||
afterEach(jest.clearAllMocks); | ||
|
||
it('renders an empty contentMenu when there is no provided tabs', () => { | ||
renderStudioContentMenu({ contentTabs: [] }); | ||
const emptyMenu = screen.getByRole('tablist'); | ||
expect(emptyMenu).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders the title and icon of a given menu tab', () => { | ||
const iconTitle = 'My icon'; | ||
renderStudioContentMenu({ | ||
contentTabs: [ | ||
{ | ||
...tab1, | ||
icon: <svg data-testid={iconTitle}></svg>, | ||
}, | ||
], | ||
}); | ||
const menuTab = screen.getByRole('tab', { name: tab1Name }); | ||
const menuIcon = screen.getByTestId(iconTitle); | ||
expect(menuTab).toBeInTheDocument(); | ||
expect(menuIcon).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders a tab with "to" prop as a link element', () => { | ||
const link = 'url-link'; | ||
renderStudioContentMenu({ | ||
contentTabs: [ | ||
{ | ||
...tab1, | ||
to: link, | ||
}, | ||
], | ||
}); | ||
const menuTab = screen.getByRole('tab', { name: tab1Name }); | ||
const linkTab = screen.getByRole('link', { name: tab1Name }); | ||
expect(menuTab).toBeInTheDocument(); | ||
expect(linkTab).toBeInTheDocument(); | ||
expect(linkTab).toHaveAttribute('href', link); | ||
}); | ||
|
||
it('allows changing focus to next tab using keyboard', async () => { | ||
const user = userEvent.setup(); | ||
renderStudioContentMenu({ | ||
contentTabs: [tab1, tab2], | ||
}); | ||
const tab1Element = screen.getByRole('tab', { name: tab1Name }); | ||
await user.click(tab1Element); | ||
const tab2Element = screen.getByRole('tab', { name: tab2Name }); | ||
expect(tab2Element).not.toHaveFocus(); | ||
await user.keyboard('{ArrowDown}'); | ||
expect(tab2Element).toHaveFocus(); | ||
}); | ||
|
||
it('keeps focus on current tab if pressing keyDown when focus is on last tab in menu', async () => { | ||
const user = userEvent.setup(); | ||
renderStudioContentMenu({ | ||
contentTabs: [tab1, tab2], | ||
}); | ||
const tab2Element = screen.getByRole('tab', { name: tab2Name }); | ||
await user.click(tab2Element); | ||
expect(tab2Element).toHaveFocus(); | ||
await user.keyboard('{ArrowDown}'); | ||
expect(tab2Element).toHaveFocus(); | ||
}); | ||
|
||
it('allows changing focus to previous tab using keyboard', async () => { | ||
const user = userEvent.setup(); | ||
renderStudioContentMenu({ | ||
contentTabs: [tab1, tab2], | ||
}); | ||
const tab2Element = screen.getByRole('tab', { name: tab2Name }); | ||
await user.click(tab2Element); | ||
const tab1Element = screen.getByRole('tab', { name: tab1Name }); | ||
expect(tab1Element).not.toHaveFocus(); | ||
await user.keyboard('{ArrowUp}'); | ||
expect(tab1Element).toHaveFocus(); | ||
}); | ||
|
||
it('keeps focus on current tab if pressing keyUp when focus is on first tab in menu', async () => { | ||
const user = userEvent.setup(); | ||
renderStudioContentMenu({ | ||
contentTabs: [tab1, tab2], | ||
}); | ||
const tab1Element = screen.getByRole('tab', { name: tab1Name }); | ||
await user.click(tab1Element); | ||
expect(tab1Element).toHaveFocus(); | ||
await user.keyboard('{ArrowUp}'); | ||
expect(tab1Element).toHaveFocus(); | ||
}); | ||
|
||
it('calls onChangeTab when clicking on a menu tab', async () => { | ||
const user = userEvent.setup(); | ||
renderStudioContentMenu({ | ||
contentTabs: [tab1], | ||
}); | ||
const menuTab = screen.getByRole('tab', { name: tab1Name }); | ||
await user.click(menuTab); | ||
expect(onChangeTabMock).toHaveBeenCalledTimes(1); | ||
expect(onChangeTabMock).toHaveBeenCalledWith(tab1Id); | ||
}); | ||
|
||
it('calls onChangeTab when clicking on a menu tab with link', async () => { | ||
const link = 'url-link'; | ||
const user = userEvent.setup(); | ||
renderStudioContentMenu({ | ||
contentTabs: [ | ||
{ | ||
...tab1, | ||
to: link, | ||
}, | ||
], | ||
}); | ||
const menuTab = screen.getByRole('tab', { name: tab1Name }); | ||
await user.click(menuTab); | ||
expect(onChangeTabMock).toHaveBeenCalledTimes(1); | ||
expect(onChangeTabMock).toHaveBeenCalledWith(tab1Id); | ||
}); | ||
}); | ||
|
||
const renderStudioContentMenu = ({ | ||
contentTabs, | ||
}: Partial<StudioContentMenuProps<StudioMenuTabName>> = {}) => { | ||
render( | ||
<StudioContentMenu | ||
contentTabs={contentTabs} | ||
selectedTabId={undefined} | ||
onChangeTab={onChangeTabMock} | ||
/>, | ||
); | ||
}; |
Oops, something went wrong.