@registrucentras/rc-ses-react-components 2.0.0-rc.3 → 2.0.0-rc.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/icons/ArrowRightIcon.d.ts +1 -1
- package/dist/components/common/CardShell/index.d.ts +1 -1
- package/dist/components/common/CardShell/types.d.ts +3 -1
- package/dist/components/common/IconWithSquareBackground/index.d.ts +12 -0
- package/dist/components/common/ServiceLinks/ServiceLinkItem/index.d.ts +15 -0
- package/dist/components/common/ServiceLinks/index.d.ts +3 -0
- package/dist/components/common/ServiceLinks/types.d.ts +34 -0
- package/dist/components/common/SideNav/SideNav.types.d.ts +20 -0
- package/dist/components/common/SideNav/SideNavLayout.d.ts +4 -0
- package/dist/components/common/SideNav/components/SideNavPill.d.ts +9 -0
- package/dist/components/common/SideNav/components/SideNavPillList.d.ts +9 -0
- package/dist/components/common/SideNav/components/SideNavRow.d.ts +9 -0
- package/dist/components/common/SideNav/hooks/useSideNavScrollSpy.d.ts +12 -0
- package/dist/components/common/SideNav/index.d.ts +4 -0
- package/dist/components/common/TitleBlock/index.d.ts +3 -0
- package/dist/components/common/TitleBlock/types.d.ts +73 -0
- package/dist/components/utils/normalizeSx.d.ts +4 -0
- package/dist/library/index.d.ts +8 -2
- package/dist/library/types.d.ts +6 -2
- package/dist/registrucentras-rc-ses-react-components.cjs +1 -1
- package/dist/registrucentras-rc-ses-react-components.js +3412 -2728
- package/dist/stories/components/display/CardShell.stories.d.ts +1 -1
- package/dist/stories/components/display/SelectableCardList.stories.d.ts +1 -0
- package/dist/stories/components/display/ServiceLinks.stories.d.ts +28 -0
- package/dist/stories/components/display/TitleBlock.stories.d.ts +20 -0
- package/dist/stories/components/form/FormControlWrapper.stories.d.ts +1 -0
- package/dist/stories/components/icons/IconWithSquareBackground.stories.d.ts +7 -0
- package/dist/stories/components/navigation/SideNav.stories.d.ts +7 -0
- package/package.json +1 -1
- package/src/assets/icons/ArrowRightIcon.tsx +2 -1
- package/src/components/common/AdvancedList/components/AdvancedListItem/index.tsx +8 -6
- package/src/components/common/Card/index.tsx +1 -23
- package/src/components/common/CardShell/index.tsx +4 -1
- package/src/components/common/CardShell/types.ts +3 -1
- package/src/components/common/IconWithSquareBackground/index.test.tsx +55 -0
- package/src/components/common/IconWithSquareBackground/index.tsx +83 -0
- package/src/components/common/ServiceLinks/ServiceLinkItem/index.tsx +177 -0
- package/src/components/common/ServiceLinks/index.test.tsx +166 -0
- package/src/components/common/ServiceLinks/index.tsx +66 -0
- package/src/components/common/ServiceLinks/types.ts +37 -0
- package/src/components/common/SideNav/SideNav.types.ts +24 -0
- package/src/components/common/SideNav/SideNavLayout.test.tsx +113 -0
- package/src/components/common/SideNav/SideNavLayout.tsx +67 -0
- package/src/components/common/SideNav/components/SideNavPill.tsx +62 -0
- package/src/components/common/SideNav/components/SideNavPillList.tsx +190 -0
- package/src/components/common/SideNav/components/SideNavRow.tsx +72 -0
- package/src/components/common/SideNav/hooks/useSideNavScrollSpy.test.ts +457 -0
- package/src/components/common/SideNav/hooks/useSideNavScrollSpy.ts +154 -0
- package/src/components/common/SideNav/index.test.tsx +99 -0
- package/src/components/common/SideNav/index.tsx +83 -0
- package/src/components/common/TitleBlock/index.test.tsx +366 -0
- package/src/components/common/TitleBlock/index.tsx +144 -0
- package/src/components/common/TitleBlock/types.ts +82 -0
- package/src/components/utils/normalizeSx.ts +24 -0
- package/src/i18n/namespaces/common/en.json +4 -0
- package/src/i18n/namespaces/common/lt.json +4 -0
- package/src/library/index.ts +8 -2
- package/src/library/types.ts +20 -8
- package/src/stories/components/display/CardShell.stories.tsx +14 -12
- package/src/stories/components/display/SelectableCardList.stories.tsx +33 -1
- package/src/stories/components/display/ServiceLinks.stories.tsx +154 -0
- package/src/stories/components/display/TitleBlock.stories.tsx +506 -0
- package/src/stories/components/form/FormControlWrapper.stories.tsx +23 -0
- package/src/stories/components/icons/IconWithSquareBackground.stories.tsx +60 -0
- package/src/stories/components/navigation/SideNav.stories.tsx +110 -0
- package/src/theme/cards.ts +1 -1
- package/src/theme/light/MuiButton.tsx +6 -0
- package/dist/components/common/CardShell/CardHeader/index.d.ts +0 -3
- package/dist/components/common/CardShell/CardHeader/types.d.ts +0 -33
- package/src/components/common/CardShell/CardHeader/index.test.tsx +0 -207
- package/src/components/common/CardShell/CardHeader/index.tsx +0 -104
- package/src/components/common/CardShell/CardHeader/types.ts +0 -36
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { ThemeProvider } from '@mui/material/styles'
|
|
2
|
+
import '@testing-library/jest-dom'
|
|
3
|
+
import { fireEvent, render, screen } from '@testing-library/react'
|
|
4
|
+
import { type ReactElement } from 'react'
|
|
5
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
6
|
+
|
|
7
|
+
import theme from '@/theme/light'
|
|
8
|
+
|
|
9
|
+
import RcSesServiceLinks from '.'
|
|
10
|
+
|
|
11
|
+
const renderServiceLinks = (ui: ReactElement) =>
|
|
12
|
+
render(<ThemeProvider theme={theme}>{ui}</ThemeProvider>)
|
|
13
|
+
|
|
14
|
+
const baseItems = [
|
|
15
|
+
{ label: 'First service', href: '#first' },
|
|
16
|
+
{ label: 'Second service', href: '#second' },
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
describe('RcSesServiceLinks', () => {
|
|
20
|
+
test('renders all provided items with their labels', () => {
|
|
21
|
+
renderServiceLinks(<RcSesServiceLinks items={baseItems} />)
|
|
22
|
+
|
|
23
|
+
expect(screen.getByText('First service')).toBeInTheDocument()
|
|
24
|
+
expect(screen.getByText('Second service')).toBeInTheDocument()
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('renders an item with href as a link pointing to the href', () => {
|
|
28
|
+
renderServiceLinks(<RcSesServiceLinks items={[{ label: 'Go', href: '/target' }]} />)
|
|
29
|
+
|
|
30
|
+
const link = screen.getByRole('link', { name: 'Go' })
|
|
31
|
+
expect(link).toHaveAttribute('href', '/target')
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('adds rel="noopener noreferrer" for links opening in a new tab', () => {
|
|
35
|
+
renderServiceLinks(
|
|
36
|
+
<RcSesServiceLinks items={[{ label: 'Ext', href: '/x', target: '_blank' }]} />,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
const link = screen.getByRole('link', { name: 'Ext' })
|
|
40
|
+
expect(link).toHaveAttribute('target', '_blank')
|
|
41
|
+
expect(link).toHaveAttribute('rel', 'noopener noreferrer')
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('does not set rel on same-tab links', () => {
|
|
45
|
+
renderServiceLinks(<RcSesServiceLinks items={[{ label: 'Same', href: '/s' }]} />)
|
|
46
|
+
|
|
47
|
+
const link = screen.getByRole('link', { name: 'Same' })
|
|
48
|
+
expect(link).not.toHaveAttribute('rel')
|
|
49
|
+
expect(link).not.toHaveAttribute('target')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('fires onClick on a link while keeping it a link', () => {
|
|
53
|
+
const onClick = vi.fn()
|
|
54
|
+
renderServiceLinks(
|
|
55
|
+
<RcSesServiceLinks items={[{ label: 'Both', href: '/b', onClick }]} />,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
const link = screen.getByRole('link', { name: 'Both' })
|
|
59
|
+
fireEvent.click(link)
|
|
60
|
+
expect(onClick).toHaveBeenCalledTimes(1)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('does not render disabled item as a link', () => {
|
|
64
|
+
renderServiceLinks(
|
|
65
|
+
<RcSesServiceLinks items={[{ label: 'Off', href: '/off', disabled: true }]} />,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
expect(screen.queryByRole('link', { name: 'Off' })).not.toBeInTheDocument()
|
|
69
|
+
expect(screen.getByText('Off')).toBeInTheDocument()
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('removes disabled item from the keyboard tab order', () => {
|
|
73
|
+
renderServiceLinks(
|
|
74
|
+
<RcSesServiceLinks
|
|
75
|
+
items={[{ label: 'Off', href: '/off', disabled: true }]}
|
|
76
|
+
testIds={{ item: 'row' }}
|
|
77
|
+
/>,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
expect(screen.getByTestId('row')).toHaveAttribute('tabindex', '-1')
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('renders navigable rows through a custom linkComponent', () => {
|
|
84
|
+
const RouterLink = ({ href, children, ...rest }: React.ComponentProps<'a'>) => (
|
|
85
|
+
<a href={href} data-router-link {...rest}>
|
|
86
|
+
{children}
|
|
87
|
+
</a>
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
renderServiceLinks(<RcSesServiceLinks items={baseItems} linkComponent={RouterLink} />)
|
|
91
|
+
|
|
92
|
+
const links = screen.getAllByRole('link')
|
|
93
|
+
expect(links).toHaveLength(2)
|
|
94
|
+
links.forEach((link) => expect(link).toHaveAttribute('data-router-link'))
|
|
95
|
+
expect(links[0]).toHaveAttribute('href', '#first')
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('linkComponent receives href, target, rel and onClick', () => {
|
|
99
|
+
const onClick = vi.fn()
|
|
100
|
+
const linkComponent = vi.fn(
|
|
101
|
+
({ href, children, ...rest }: React.ComponentProps<'a'>) => (
|
|
102
|
+
<a href={href} {...rest}>
|
|
103
|
+
{children}
|
|
104
|
+
</a>
|
|
105
|
+
),
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
renderServiceLinks(
|
|
109
|
+
<RcSesServiceLinks
|
|
110
|
+
items={[{ label: 'Go', href: '/target', target: '_blank', onClick }]}
|
|
111
|
+
linkComponent={linkComponent}
|
|
112
|
+
/>,
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
expect(linkComponent).toHaveBeenCalled()
|
|
116
|
+
expect(linkComponent.mock.calls[0][0]).toEqual(
|
|
117
|
+
expect.objectContaining({
|
|
118
|
+
href: '/target',
|
|
119
|
+
target: '_blank',
|
|
120
|
+
rel: 'noopener noreferrer',
|
|
121
|
+
onClick,
|
|
122
|
+
}),
|
|
123
|
+
)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
test('linkComponent does not receive rel for same-tab links', () => {
|
|
127
|
+
const linkComponent = vi.fn(
|
|
128
|
+
({ href, children, ...rest }: React.ComponentProps<'a'>) => (
|
|
129
|
+
<a href={href} {...rest}>
|
|
130
|
+
{children}
|
|
131
|
+
</a>
|
|
132
|
+
),
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
renderServiceLinks(
|
|
136
|
+
<RcSesServiceLinks
|
|
137
|
+
items={[{ label: 'Go', href: '/target' }]}
|
|
138
|
+
linkComponent={linkComponent}
|
|
139
|
+
/>,
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
const props = linkComponent.mock.calls[0][0]
|
|
143
|
+
expect(props.rel).toBeUndefined()
|
|
144
|
+
expect(props.target).toBeUndefined()
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('linkComponent is not used for disabled items', () => {
|
|
148
|
+
const linkComponent = vi.fn(
|
|
149
|
+
({ href, children, ...rest }: React.ComponentProps<'a'>) => (
|
|
150
|
+
<a href={href} {...rest}>
|
|
151
|
+
{children}
|
|
152
|
+
</a>
|
|
153
|
+
),
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
renderServiceLinks(
|
|
157
|
+
<RcSesServiceLinks
|
|
158
|
+
items={[{ label: 'Off', href: '/off', disabled: true }]}
|
|
159
|
+
linkComponent={linkComponent}
|
|
160
|
+
/>,
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
expect(linkComponent).not.toHaveBeenCalled()
|
|
164
|
+
expect(screen.queryByRole('link', { name: 'Off' })).not.toBeInTheDocument()
|
|
165
|
+
})
|
|
166
|
+
})
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Box, Divider } from '@mui/material'
|
|
2
|
+
import { Fragment } from 'react'
|
|
3
|
+
|
|
4
|
+
import palette from '@/theme/palette'
|
|
5
|
+
|
|
6
|
+
import ServiceLinkItem from './ServiceLinkItem'
|
|
7
|
+
import { RcSesServiceLinksProps } from './types'
|
|
8
|
+
|
|
9
|
+
function RcSesServiceLinks({
|
|
10
|
+
items,
|
|
11
|
+
dividers = true,
|
|
12
|
+
isLoading = false,
|
|
13
|
+
skeletonCount = 4,
|
|
14
|
+
linkComponent,
|
|
15
|
+
className,
|
|
16
|
+
testIds,
|
|
17
|
+
}: RcSesServiceLinksProps) {
|
|
18
|
+
const rowCount = isLoading ? skeletonCount : items.length
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<Box
|
|
22
|
+
className={className}
|
|
23
|
+
data-testid={testIds?.root}
|
|
24
|
+
sx={{
|
|
25
|
+
display: 'flex',
|
|
26
|
+
flexDirection: 'column',
|
|
27
|
+
width: '100%',
|
|
28
|
+
}}
|
|
29
|
+
>
|
|
30
|
+
{Array.from({ length: rowCount }).map((_, index) => {
|
|
31
|
+
if (isLoading) {
|
|
32
|
+
return (
|
|
33
|
+
// Skeleton rows are indistinguishable and the list length is fixed
|
|
34
|
+
// by `skeletonCount`, so index is a stable identity here.
|
|
35
|
+
// eslint-disable-next-line react/no-array-index-key
|
|
36
|
+
<Fragment key={`skeleton-${index}`}>
|
|
37
|
+
{dividers && index > 0 ? (
|
|
38
|
+
<Divider sx={{ borderColor: palette.grey[200] }} />
|
|
39
|
+
) : null}
|
|
40
|
+
<ServiceLinkItem label='' isLoading testIds={testIds} />
|
|
41
|
+
</Fragment>
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const item = items[index]
|
|
46
|
+
const { key: itemKey, ...itemProps } = item
|
|
47
|
+
const key = itemKey || String(index)
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<Fragment key={key}>
|
|
51
|
+
{dividers && index > 0 ? (
|
|
52
|
+
<Divider sx={{ borderColor: palette.grey[200] }} />
|
|
53
|
+
) : null}
|
|
54
|
+
<ServiceLinkItem
|
|
55
|
+
{...itemProps}
|
|
56
|
+
linkComponent={linkComponent}
|
|
57
|
+
testIds={testIds}
|
|
58
|
+
/>
|
|
59
|
+
</Fragment>
|
|
60
|
+
)
|
|
61
|
+
})}
|
|
62
|
+
</Box>
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export default RcSesServiceLinks
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ElementType, ReactNode } from 'react'
|
|
2
|
+
|
|
3
|
+
export type RcSesServiceLinksTestIds = {
|
|
4
|
+
root?: string
|
|
5
|
+
item?: string
|
|
6
|
+
itemLabel?: string
|
|
7
|
+
itemIcon?: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type RcSesServiceLinkItem = {
|
|
11
|
+
label: ReactNode
|
|
12
|
+
href?: string
|
|
13
|
+
target?: string
|
|
14
|
+
onClick?: (event: React.MouseEvent<HTMLElement>) => void
|
|
15
|
+
disabled?: boolean
|
|
16
|
+
/**
|
|
17
|
+
* Optional stable identity used as the React key when rendering the list.
|
|
18
|
+
* Stripped out before spreading onto the row, so it never reaches the DOM.
|
|
19
|
+
*/
|
|
20
|
+
key?: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type RcSesServiceLinksProps = {
|
|
24
|
+
items: RcSesServiceLinkItem[]
|
|
25
|
+
dividers?: boolean
|
|
26
|
+
isLoading?: boolean
|
|
27
|
+
skeletonCount?: number
|
|
28
|
+
/**
|
|
29
|
+
* Custom element used to render navigable rows. Receives `href` (and other
|
|
30
|
+
* anchor props). Falls back to a native `<a>` when omitted. For router
|
|
31
|
+
* links pass a small adapter, e.g.
|
|
32
|
+
* `({ href, ...rest }) => <RouterLink to={href} {...rest} />`.
|
|
33
|
+
*/
|
|
34
|
+
linkComponent?: ElementType
|
|
35
|
+
className?: string
|
|
36
|
+
testIds?: RcSesServiceLinksTestIds
|
|
37
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { SxProps, Theme } from '@mui/material'
|
|
2
|
+
import { ReactNode } from 'react'
|
|
3
|
+
|
|
4
|
+
export type RcSesSideNavItem = {
|
|
5
|
+
id: string
|
|
6
|
+
label: string
|
|
7
|
+
count?: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type RcSesSideNavProps = {
|
|
11
|
+
// In document order - scroll-spy compares section positions in list order.
|
|
12
|
+
items: RcSesSideNavItem[]
|
|
13
|
+
activeItemId?: string
|
|
14
|
+
onItemClick?: (id: string) => void
|
|
15
|
+
title?: string
|
|
16
|
+
sx?: SxProps<Theme>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type RcSesSideNavLayoutProps = Omit<RcSesSideNavProps, 'sx'> & {
|
|
20
|
+
children: ReactNode
|
|
21
|
+
sidebarWidth?: string
|
|
22
|
+
offset?: number
|
|
23
|
+
sx?: SxProps<Theme>
|
|
24
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { ThemeProvider } from '@mui/material/styles'
|
|
2
|
+
import { act, render, screen } from '@testing-library/react'
|
|
3
|
+
import { ReactElement } from 'react'
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
5
|
+
|
|
6
|
+
import i18n from '@/i18n/i18n'
|
|
7
|
+
import theme from '@/theme/light'
|
|
8
|
+
|
|
9
|
+
import { RcSesSideNavItem } from './SideNav.types'
|
|
10
|
+
import RcSesSideNavLayout from './SideNavLayout'
|
|
11
|
+
|
|
12
|
+
function ResizeObserverMock() {
|
|
13
|
+
return { observe: () => {}, unobserve: () => {}, disconnect: () => {} }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
Element.prototype.scrollIntoView = vi.fn()
|
|
17
|
+
|
|
18
|
+
const items: RcSesSideNavItem[] = [
|
|
19
|
+
{ id: 'family', label: 'Aš ir mano šeima', count: 4 },
|
|
20
|
+
{ id: 'documents', label: 'Dokumentai apie gyventoją', count: 2 },
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
const renderLayout = (ui: ReactElement) =>
|
|
24
|
+
render(<ThemeProvider theme={theme}>{ui}</ThemeProvider>)
|
|
25
|
+
|
|
26
|
+
describe('RcSesSideNavLayout', () => {
|
|
27
|
+
beforeEach(async () => {
|
|
28
|
+
vi.stubGlobal('ResizeObserver', ResizeObserverMock)
|
|
29
|
+
await i18n.changeLanguage('lt')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
vi.unstubAllGlobals()
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('renders the nav alongside its content children', () => {
|
|
37
|
+
renderLayout(
|
|
38
|
+
<RcSesSideNavLayout items={items} title='Temos'>
|
|
39
|
+
<div id='family'>Family content</div>
|
|
40
|
+
<div id='documents'>Documents content</div>
|
|
41
|
+
</RcSesSideNavLayout>,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
expect(screen.getByRole('navigation', { name: 'Temos' })).toBeInTheDocument()
|
|
45
|
+
expect(screen.getByText('Family content')).toBeInTheDocument()
|
|
46
|
+
expect(screen.getByText('Documents content')).toBeInTheDocument()
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('forwards activeItemId/onItemClick through to the nav for controlled mode', () => {
|
|
50
|
+
const handleClick = vi.fn()
|
|
51
|
+
renderLayout(
|
|
52
|
+
<RcSesSideNavLayout items={items} activeItemId='family' onItemClick={handleClick}>
|
|
53
|
+
<div id='family' />
|
|
54
|
+
<div id='documents' />
|
|
55
|
+
</RcSesSideNavLayout>,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
screen.getAllByRole('button', { name: 'Dokumentai apie gyventoją, 2' })[0].click()
|
|
59
|
+
|
|
60
|
+
expect(handleClick).toHaveBeenCalledWith('documents')
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('is interactive by default and scrolls to the clicked item when uncontrolled', () => {
|
|
64
|
+
const scrollToMock = vi.fn()
|
|
65
|
+
vi.stubGlobal('scrollTo', scrollToMock)
|
|
66
|
+
vi.stubGlobal(
|
|
67
|
+
'matchMedia',
|
|
68
|
+
vi.fn(() => ({ matches: false })),
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
renderLayout(
|
|
72
|
+
<RcSesSideNavLayout items={items} title='Temos'>
|
|
73
|
+
<div id='family' />
|
|
74
|
+
<div id='documents' />
|
|
75
|
+
</RcSesSideNavLayout>,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
act(() => {
|
|
79
|
+
screen.getAllByRole('button', { name: 'Dokumentai apie gyventoją, 2' })[0].click()
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
expect(scrollToMock).toHaveBeenCalledWith(
|
|
83
|
+
expect.objectContaining({ behavior: 'smooth' }),
|
|
84
|
+
)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('does not throw when clicked with no matching section in the content', () => {
|
|
88
|
+
renderLayout(
|
|
89
|
+
<RcSesSideNavLayout items={items} title='Temos'>
|
|
90
|
+
<div />
|
|
91
|
+
</RcSesSideNavLayout>,
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
expect(() =>
|
|
95
|
+
screen.getAllByRole('button', { name: 'Dokumentai apie gyventoją, 2' })[0].click(),
|
|
96
|
+
).not.toThrow()
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('does not drop custom sx values passed as an array', () => {
|
|
100
|
+
const { container } = renderLayout(
|
|
101
|
+
<RcSesSideNavLayout
|
|
102
|
+
items={items}
|
|
103
|
+
title='Temos'
|
|
104
|
+
sx={[{ backgroundColor: 'rgb(1, 2, 3)' }]}
|
|
105
|
+
>
|
|
106
|
+
<div id='family' />
|
|
107
|
+
<div id='documents' />
|
|
108
|
+
</RcSesSideNavLayout>,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
expect(container.firstChild).toHaveStyle({ backgroundColor: 'rgb(1, 2, 3)' })
|
|
112
|
+
})
|
|
113
|
+
})
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Box } from '@mui/material'
|
|
2
|
+
import { useRef } from 'react'
|
|
3
|
+
|
|
4
|
+
import normalizeSx from '@/components/utils/normalizeSx'
|
|
5
|
+
|
|
6
|
+
import RcSesSideNav from '.'
|
|
7
|
+
import { RcSesSideNavLayoutProps } from './SideNav.types'
|
|
8
|
+
import useSideNavScrollSpy from './hooks/useSideNavScrollSpy'
|
|
9
|
+
|
|
10
|
+
function RcSesSideNavLayout({
|
|
11
|
+
children,
|
|
12
|
+
items,
|
|
13
|
+
activeItemId: controlledActiveItemId,
|
|
14
|
+
onItemClick,
|
|
15
|
+
title,
|
|
16
|
+
offset,
|
|
17
|
+
sidebarWidth = '17rem',
|
|
18
|
+
sx,
|
|
19
|
+
}: RcSesSideNavLayoutProps) {
|
|
20
|
+
const contentRef = useRef<HTMLDivElement>(null)
|
|
21
|
+
|
|
22
|
+
const isControlled = controlledActiveItemId !== undefined
|
|
23
|
+
const itemIds = items.map((item) => item.id)
|
|
24
|
+
const scrollSpy = useSideNavScrollSpy({
|
|
25
|
+
itemIds: isControlled ? [] : itemIds,
|
|
26
|
+
offset,
|
|
27
|
+
scope: contentRef,
|
|
28
|
+
})
|
|
29
|
+
const activeItemId = isControlled ? controlledActiveItemId : scrollSpy.activeItemId
|
|
30
|
+
|
|
31
|
+
const isInteractive = !isControlled || !!onItemClick
|
|
32
|
+
const handleItemClick = isInteractive
|
|
33
|
+
? (id: string) => {
|
|
34
|
+
if (!isControlled) scrollSpy.scrollToItem(id)
|
|
35
|
+
onItemClick?.(id)
|
|
36
|
+
}
|
|
37
|
+
: undefined
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<Box
|
|
41
|
+
sx={[
|
|
42
|
+
{ display: 'flex', flexDirection: { xs: 'column', md: 'row' }, gap: '2rem' },
|
|
43
|
+
...normalizeSx(sx),
|
|
44
|
+
]}
|
|
45
|
+
>
|
|
46
|
+
<RcSesSideNav
|
|
47
|
+
items={items}
|
|
48
|
+
activeItemId={activeItemId}
|
|
49
|
+
onItemClick={handleItemClick}
|
|
50
|
+
title={title}
|
|
51
|
+
sx={{
|
|
52
|
+
position: 'sticky',
|
|
53
|
+
top: `${offset ?? 0}px`,
|
|
54
|
+
alignSelf: { xs: 'stretch', md: 'flex-start' },
|
|
55
|
+
width: { md: sidebarWidth },
|
|
56
|
+
flexShrink: 0,
|
|
57
|
+
}}
|
|
58
|
+
/>
|
|
59
|
+
<Box ref={contentRef} sx={{ flex: 1, minWidth: 0 }}>
|
|
60
|
+
{children}
|
|
61
|
+
</Box>
|
|
62
|
+
</Box>
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export default RcSesSideNavLayout
|
|
67
|
+
export type { RcSesSideNavLayoutProps } from './SideNav.types'
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Box, Typography } from '@mui/material'
|
|
2
|
+
|
|
3
|
+
import palette, { common } from '@/theme/palette'
|
|
4
|
+
|
|
5
|
+
type Props = {
|
|
6
|
+
label: string
|
|
7
|
+
count?: number
|
|
8
|
+
ariaLabel?: string
|
|
9
|
+
active: boolean
|
|
10
|
+
onClick?: () => void
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function SideNavPill({ label, count, ariaLabel, active, onClick }: Props) {
|
|
14
|
+
return (
|
|
15
|
+
<Box
|
|
16
|
+
component={onClick ? 'button' : 'div'}
|
|
17
|
+
type={onClick ? 'button' : undefined}
|
|
18
|
+
onClick={onClick}
|
|
19
|
+
aria-current={active ? 'page' : undefined}
|
|
20
|
+
aria-label={ariaLabel}
|
|
21
|
+
sx={{
|
|
22
|
+
display: 'flex',
|
|
23
|
+
alignItems: 'center',
|
|
24
|
+
gap: '0.5rem',
|
|
25
|
+
flexShrink: 0,
|
|
26
|
+
whiteSpace: 'nowrap',
|
|
27
|
+
margin: 0,
|
|
28
|
+
borderRadius: '9999px',
|
|
29
|
+
border: `0.0625rem solid ${active ? palette.primary.main : palette.grey[200]}`,
|
|
30
|
+
backgroundColor: active ? palette.primary[50] : common.white,
|
|
31
|
+
padding: '0.5rem 1rem',
|
|
32
|
+
cursor: onClick ? 'pointer' : 'default',
|
|
33
|
+
WebkitTapHighlightColor: 'transparent',
|
|
34
|
+
'&:focus-visible': {
|
|
35
|
+
outline: `0.125rem solid ${palette.primary.main}`,
|
|
36
|
+
outlineOffset: '0.125rem',
|
|
37
|
+
},
|
|
38
|
+
}}
|
|
39
|
+
>
|
|
40
|
+
<Typography
|
|
41
|
+
component='span'
|
|
42
|
+
sx={{
|
|
43
|
+
fontSize: '0.875rem',
|
|
44
|
+
lineHeight: '1.25rem',
|
|
45
|
+
color: active ? palette.grey[900] : palette.grey[600],
|
|
46
|
+
}}
|
|
47
|
+
>
|
|
48
|
+
{label}
|
|
49
|
+
</Typography>
|
|
50
|
+
{count !== undefined && (
|
|
51
|
+
<Typography
|
|
52
|
+
component='span'
|
|
53
|
+
sx={{ fontSize: '0.8125rem', lineHeight: '1.125rem', color: palette.grey[500] }}
|
|
54
|
+
>
|
|
55
|
+
{count}
|
|
56
|
+
</Typography>
|
|
57
|
+
)}
|
|
58
|
+
</Box>
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default SideNavPill
|