@oslokommune/punkt-react 20.1.7 → 20.2.0
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/CHANGELOG.md +8 -0
- package/dist/components/tabs/TabItem.js +63 -35
- package/dist/components/tabs/Tabs.d.ts +12 -0
- package/dist/components/tabs/Tabs.js +58 -60
- package/dist/punkt-react.cjs +1 -1
- package/dist/shared-utils/tabs/keyboard.d.ts +1 -0
- package/dist/shared-utils/tabs/keyboard.js +14 -0
- package/dist/shared-utils/tabs/panels.d.ts +2 -0
- package/dist/shared-utils/tabs/panels.js +7 -0
- package/package.json +3 -3
- package/src/components/tabs/TabItem.tsx +40 -6
- package/src/components/tabs/Tabs.test.tsx +425 -5
- package/src/components/tabs/Tabs.tsx +63 -22
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function tabIndexForKey(key: string, current: number, count: number, isDisabled: (index: number) => boolean): number | null;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// ../../shared-utils/tabs/keyboard.ts
|
|
2
|
+
function e(e, t, n, r) {
|
|
3
|
+
let i = Array.from({ length: n }, (e, t) => t).filter((e) => !r(e));
|
|
4
|
+
if (!i.length) return null;
|
|
5
|
+
let a = i[0], o = i[i.length - 1];
|
|
6
|
+
switch (e) {
|
|
7
|
+
case "Home": return a;
|
|
8
|
+
case "End": return o;
|
|
9
|
+
case "ArrowRight": return i.find((e) => e > t) ?? a;
|
|
10
|
+
case "ArrowLeft": return [...i].reverse().find((e) => e < t) ?? o;
|
|
11
|
+
default: return null;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export { e as tabIndexForKey };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// ../../shared-utils/tabs/panels.ts
|
|
2
|
+
var e = (e) => `${e}-tab`;
|
|
3
|
+
function t(e, t, n, r) {
|
|
4
|
+
let i = e.getRootNode().getElementById?.(t);
|
|
5
|
+
i && (i.hidden = !n, r && (i.hasAttribute("role") || i.setAttribute("role", "tabpanel"), i.hasAttribute("tabindex") || i.setAttribute("tabindex", "0"), e.id && !i.hasAttribute("aria-labelledby") && !i.hasAttribute("aria-label") && i.setAttribute("aria-labelledby", e.id)));
|
|
6
|
+
}
|
|
7
|
+
export { t as syncTabPanel, e as tabIdFor };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oslokommune/punkt-react",
|
|
3
|
-
"version": "20.
|
|
3
|
+
"version": "20.2.0",
|
|
4
4
|
"description": "React komponentbibliotek til Punkt, designsystem for Oslo kommune",
|
|
5
5
|
"homepage": "https://punkt.oslo.kommune.no",
|
|
6
6
|
"author": "Team Designsystem, Oslo kommune",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"@eslint/eslintrc": "^3.3.5",
|
|
43
43
|
"@eslint/js": "^10.0.1",
|
|
44
44
|
"@oslokommune/punkt-assets": "^20.0.5",
|
|
45
|
-
"@oslokommune/punkt-css": "^20.
|
|
45
|
+
"@oslokommune/punkt-css": "^20.2.0",
|
|
46
46
|
"@testing-library/jest-dom": "^7.0.1",
|
|
47
47
|
"@testing-library/react": "^16.3.3",
|
|
48
48
|
"@testing-library/user-event": "^14.6.8",
|
|
@@ -102,5 +102,5 @@
|
|
|
102
102
|
"url": "https://github.com/oslokommune/punkt/issues"
|
|
103
103
|
},
|
|
104
104
|
"license": "MIT",
|
|
105
|
-
"gitHead": "
|
|
105
|
+
"gitHead": "6e2e6b2c2801df3e024106db88fe4916f457b93c"
|
|
106
106
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
|
-
import { forwardRef, type KeyboardEvent, type MouseEvent, type ReactNode } from 'react'
|
|
3
|
+
import { forwardRef, type KeyboardEvent, type MouseEvent, type ReactNode, useEffect, useRef } from 'react'
|
|
4
4
|
|
|
5
5
|
import { PktIcon } from '../icon/Icon'
|
|
6
6
|
import type { TTagSkin } from 'shared-types'
|
|
7
|
+
import { syncTabPanel, tabIdFor } from 'shared-utils/tabs/panels'
|
|
7
8
|
|
|
8
9
|
import { PktTag } from '../tag/Tag'
|
|
9
10
|
import { useTabsContext } from './Tabs'
|
|
@@ -22,9 +23,37 @@ export interface IPktTabItem {
|
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
export const PktTabItem = forwardRef<HTMLAnchorElement | HTMLButtonElement, IPktTabItem>(
|
|
25
|
-
({ children, active, disabled = false, href, onClick, icon, controls, tag, tagSkin, index = 0 }, ref) => {
|
|
26
|
-
const {
|
|
27
|
-
|
|
26
|
+
({ children, active, disabled = false, href, onClick, icon, controls, tag, tagSkin = 'blue', index = 0 }, ref) => {
|
|
27
|
+
const {
|
|
28
|
+
arrowNav,
|
|
29
|
+
togglePanels,
|
|
30
|
+
selectedIndex,
|
|
31
|
+
setSelectedIndex,
|
|
32
|
+
fallbackFocusIndex,
|
|
33
|
+
reportTab,
|
|
34
|
+
registerTabRef,
|
|
35
|
+
handleKeyPress,
|
|
36
|
+
selectTab,
|
|
37
|
+
} = useTabsContext()
|
|
38
|
+
const isActive = !disabled && (togglePanels && selectedIndex !== null ? selectedIndex === index : !!active)
|
|
39
|
+
const isFocusable = isActive || fallbackFocusIndex === index
|
|
40
|
+
const tabId = togglePanels && controls ? tabIdFor(controls) : undefined
|
|
41
|
+
const ariaCurrent: 'page' | 'true' | undefined = !arrowNav && isActive ? (href ? 'page' : 'true') : undefined
|
|
42
|
+
const elementRef = useRef<HTMLAnchorElement | HTMLButtonElement | null>(null)
|
|
43
|
+
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
if (togglePanels && active && !disabled) setSelectedIndex(index)
|
|
46
|
+
}, [togglePanels, active, disabled, index, setSelectedIndex])
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
reportTab(index, { active: isActive, disabled })
|
|
50
|
+
return () => reportTab(index, null)
|
|
51
|
+
}, [index, isActive, disabled, reportTab])
|
|
52
|
+
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (!togglePanels || !controls || !elementRef.current) return
|
|
55
|
+
syncTabPanel(elementRef.current, controls, isActive, arrowNav)
|
|
56
|
+
}, [togglePanels, controls, isActive, arrowNav])
|
|
28
57
|
|
|
29
58
|
const handleClick = (event: MouseEvent<HTMLAnchorElement | HTMLButtonElement>) => {
|
|
30
59
|
if (disabled) {
|
|
@@ -47,13 +76,16 @@ export const PktTabItem = forwardRef<HTMLAnchorElement | HTMLButtonElement, IPkt
|
|
|
47
76
|
}
|
|
48
77
|
|
|
49
78
|
const commonProps = {
|
|
79
|
+
id: tabId,
|
|
50
80
|
'aria-selected': arrowNav ? isActive : undefined,
|
|
81
|
+
'aria-current': ariaCurrent,
|
|
51
82
|
'aria-controls': controls,
|
|
52
83
|
role: arrowNav ? 'tab' : undefined,
|
|
53
84
|
onKeyDown: handleKeyDown,
|
|
54
85
|
onClick: handleClick,
|
|
55
|
-
tabIndex: disabled ? -1 :
|
|
86
|
+
tabIndex: disabled ? -1 : isFocusable || !arrowNav ? undefined : -1,
|
|
56
87
|
ref: (el: HTMLAnchorElement | HTMLButtonElement | null) => {
|
|
88
|
+
elementRef.current = el
|
|
57
89
|
registerTabRef(index, el, disabled)
|
|
58
90
|
if (typeof ref === 'function') {
|
|
59
91
|
ref(el)
|
|
@@ -65,7 +97,9 @@ export const PktTabItem = forwardRef<HTMLAnchorElement | HTMLButtonElement, IPkt
|
|
|
65
97
|
|
|
66
98
|
const content = (
|
|
67
99
|
<>
|
|
68
|
-
{icon &&
|
|
100
|
+
{icon && (
|
|
101
|
+
<PktIcon name={icon} className={`pkt-icon--small ${icon === 'check' ? 'pkt-tabs__status-icon' : ''}`} />
|
|
102
|
+
)}
|
|
69
103
|
{children}
|
|
70
104
|
{tag && (
|
|
71
105
|
<PktTag skin={tagSkin} size="small">
|
|
@@ -4,7 +4,7 @@ import { fireEvent, render } from '@testing-library/react'
|
|
|
4
4
|
import { axe, toHaveNoViolations } from 'jest-axe'
|
|
5
5
|
import { vi } from 'vitest'
|
|
6
6
|
|
|
7
|
-
import { PktTabItem, PktTabs } from './Tabs'
|
|
7
|
+
import { type IPktTabs, PktTabItem, PktTabs } from './Tabs'
|
|
8
8
|
|
|
9
9
|
expect.extend(toHaveNoViolations)
|
|
10
10
|
|
|
@@ -269,7 +269,7 @@ describe('PktTabItem children format', () => {
|
|
|
269
269
|
expect(firstTab).toHaveFocus()
|
|
270
270
|
})
|
|
271
271
|
|
|
272
|
-
it('
|
|
272
|
+
it('wraps to the first tab with ArrowRight on the last tab', () => {
|
|
273
273
|
const { getByText } = render(
|
|
274
274
|
<PktTabs>
|
|
275
275
|
<PktTabItem index={0}>First Tab</PktTabItem>
|
|
@@ -285,10 +285,10 @@ describe('PktTabItem children format', () => {
|
|
|
285
285
|
thirdTab.focus()
|
|
286
286
|
fireEvent.keyDown(thirdTab, { key: 'ArrowRight' })
|
|
287
287
|
|
|
288
|
-
expect(
|
|
288
|
+
expect(getByText('First Tab')).toHaveFocus()
|
|
289
289
|
})
|
|
290
290
|
|
|
291
|
-
it('
|
|
291
|
+
it('wraps to the last tab with ArrowLeft on the first tab', () => {
|
|
292
292
|
const { getByText } = render(
|
|
293
293
|
<PktTabs>
|
|
294
294
|
<PktTabItem index={0} active>
|
|
@@ -304,7 +304,71 @@ describe('PktTabItem children format', () => {
|
|
|
304
304
|
firstTab.focus()
|
|
305
305
|
fireEvent.keyDown(firstTab, { key: 'ArrowLeft' })
|
|
306
306
|
|
|
307
|
-
expect(
|
|
307
|
+
expect(getByText('Third Tab')).toHaveFocus()
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
it('moves focus to the first and last tab with Home and End', () => {
|
|
311
|
+
const { getByText } = render(
|
|
312
|
+
<PktTabs>
|
|
313
|
+
<PktTabItem index={0}>First Tab</PktTabItem>
|
|
314
|
+
<PktTabItem index={1} active>
|
|
315
|
+
Second Tab
|
|
316
|
+
</PktTabItem>
|
|
317
|
+
<PktTabItem index={2}>Third Tab</PktTabItem>
|
|
318
|
+
</PktTabs>,
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
getByText('Second Tab').focus()
|
|
322
|
+
fireEvent.keyDown(getByText('Second Tab'), { key: 'End' })
|
|
323
|
+
expect(getByText('Third Tab')).toHaveFocus()
|
|
324
|
+
|
|
325
|
+
fireEvent.keyDown(getByText('Third Tab'), { key: 'Home' })
|
|
326
|
+
expect(getByText('First Tab')).toHaveFocus()
|
|
327
|
+
})
|
|
328
|
+
|
|
329
|
+
it('skips disabled tabs when wrapping and with Home and End', () => {
|
|
330
|
+
const { getByText } = render(
|
|
331
|
+
<PktTabs>
|
|
332
|
+
<PktTabItem index={0} disabled>
|
|
333
|
+
Tab 1
|
|
334
|
+
</PktTabItem>
|
|
335
|
+
<PktTabItem index={1} active>
|
|
336
|
+
Tab 2
|
|
337
|
+
</PktTabItem>
|
|
338
|
+
<PktTabItem index={2} disabled>
|
|
339
|
+
Tab 3
|
|
340
|
+
</PktTabItem>
|
|
341
|
+
<PktTabItem index={3}>Tab 4</PktTabItem>
|
|
342
|
+
</PktTabs>,
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
getByText('Tab 4').focus()
|
|
346
|
+
fireEvent.keyDown(getByText('Tab 4'), { key: 'ArrowRight' })
|
|
347
|
+
expect(getByText('Tab 2')).toHaveFocus()
|
|
348
|
+
|
|
349
|
+
fireEvent.keyDown(getByText('Tab 2'), { key: 'Home' })
|
|
350
|
+
expect(getByText('Tab 2')).toHaveFocus()
|
|
351
|
+
|
|
352
|
+
fireEvent.keyDown(getByText('Tab 2'), { key: 'End' })
|
|
353
|
+
expect(getByText('Tab 4')).toHaveFocus()
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
it('does not select a tab when moving focus without togglePanels', () => {
|
|
357
|
+
const onTabSelected = vi.fn()
|
|
358
|
+
const { getByText } = render(
|
|
359
|
+
<PktTabs onTabSelected={onTabSelected}>
|
|
360
|
+
<PktTabItem index={0} active>
|
|
361
|
+
First Tab
|
|
362
|
+
</PktTabItem>
|
|
363
|
+
<PktTabItem index={1}>Second Tab</PktTabItem>
|
|
364
|
+
</PktTabs>,
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
getByText('First Tab').focus()
|
|
368
|
+
fireEvent.keyDown(getByText('First Tab'), { key: 'ArrowRight' })
|
|
369
|
+
fireEvent.keyDown(getByText('Second Tab'), { key: 'End' })
|
|
370
|
+
|
|
371
|
+
expect(onTabSelected).not.toHaveBeenCalled()
|
|
308
372
|
})
|
|
309
373
|
|
|
310
374
|
it('selects tab when Space key is pressed', () => {
|
|
@@ -586,3 +650,359 @@ describe('PktTabItem children format', () => {
|
|
|
586
650
|
})
|
|
587
651
|
})
|
|
588
652
|
})
|
|
653
|
+
|
|
654
|
+
describe('togglePanels', () => {
|
|
655
|
+
const Panels = ({ first = {} }: { first?: Record<string, string> }) => (
|
|
656
|
+
<>
|
|
657
|
+
<div id="panel-0" {...first}>
|
|
658
|
+
Panel 1
|
|
659
|
+
</div>
|
|
660
|
+
<div id="panel-1">Panel 2</div>
|
|
661
|
+
<div id="panel-2">Panel 3</div>
|
|
662
|
+
</>
|
|
663
|
+
)
|
|
664
|
+
|
|
665
|
+
const renderWithPanels = (props: IPktTabs = { togglePanels: true }, activeIndex = 0, disabledIndex?: number) => {
|
|
666
|
+
const result = render(
|
|
667
|
+
<>
|
|
668
|
+
<PktTabs {...props}>
|
|
669
|
+
{[0, 1, 2].map((i) => (
|
|
670
|
+
<PktTabItem
|
|
671
|
+
key={i}
|
|
672
|
+
index={i}
|
|
673
|
+
controls={`panel-${i}`}
|
|
674
|
+
active={i === activeIndex}
|
|
675
|
+
disabled={i === disabledIndex}
|
|
676
|
+
>
|
|
677
|
+
{`Tab ${i + 1}`}
|
|
678
|
+
</PktTabItem>
|
|
679
|
+
))}
|
|
680
|
+
</PktTabs>
|
|
681
|
+
<Panels />
|
|
682
|
+
</>,
|
|
683
|
+
)
|
|
684
|
+
const buttons = [0, 1, 2].map((i) => result.getByText(`Tab ${i + 1}`))
|
|
685
|
+
const panels = [0, 1, 2].map((i) => document.getElementById(`panel-${i}`) as HTMLElement)
|
|
686
|
+
return { ...result, buttons, panels }
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
it('shows only the panel of the active tab', () => {
|
|
690
|
+
const { panels } = renderWithPanels()
|
|
691
|
+
|
|
692
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([false, true, true])
|
|
693
|
+
})
|
|
694
|
+
|
|
695
|
+
it('switches active tab and panel on click', () => {
|
|
696
|
+
const { buttons, panels } = renderWithPanels()
|
|
697
|
+
|
|
698
|
+
fireEvent.click(buttons[1])
|
|
699
|
+
|
|
700
|
+
expect(buttons[1]).toHaveClass('active')
|
|
701
|
+
expect(buttons[1]).toHaveAttribute('aria-selected', 'true')
|
|
702
|
+
expect(buttons[0]).toHaveAttribute('aria-selected', 'false')
|
|
703
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, false, true])
|
|
704
|
+
})
|
|
705
|
+
|
|
706
|
+
it('switches panel with Enter', () => {
|
|
707
|
+
const { buttons, panels } = renderWithPanels()
|
|
708
|
+
|
|
709
|
+
fireEvent.keyDown(buttons[2], { key: 'Enter' })
|
|
710
|
+
|
|
711
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, true, false])
|
|
712
|
+
})
|
|
713
|
+
|
|
714
|
+
it('still calls onTabSelected', () => {
|
|
715
|
+
const onTabSelected = vi.fn()
|
|
716
|
+
const { buttons } = renderWithPanels({ togglePanels: true, onTabSelected })
|
|
717
|
+
|
|
718
|
+
fireEvent.click(buttons[1])
|
|
719
|
+
|
|
720
|
+
expect(onTabSelected).toHaveBeenCalledWith(1)
|
|
721
|
+
})
|
|
722
|
+
|
|
723
|
+
it('follows active when the prop changes', () => {
|
|
724
|
+
const { buttons, panels, rerender } = renderWithPanels()
|
|
725
|
+
fireEvent.click(buttons[1])
|
|
726
|
+
|
|
727
|
+
rerender(
|
|
728
|
+
<>
|
|
729
|
+
<PktTabs togglePanels>
|
|
730
|
+
{[0, 1, 2].map((i) => (
|
|
731
|
+
<PktTabItem key={i} index={i} controls={`panel-${i}`} active={i === 2}>
|
|
732
|
+
{`Tab ${i + 1}`}
|
|
733
|
+
</PktTabItem>
|
|
734
|
+
))}
|
|
735
|
+
</PktTabs>
|
|
736
|
+
<Panels />
|
|
737
|
+
</>,
|
|
738
|
+
)
|
|
739
|
+
|
|
740
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, true, false])
|
|
741
|
+
})
|
|
742
|
+
|
|
743
|
+
it('keeps the selected tab when an unchanged active prop rerenders', () => {
|
|
744
|
+
const { buttons, panels, rerender } = renderWithPanels()
|
|
745
|
+
fireEvent.click(buttons[1])
|
|
746
|
+
|
|
747
|
+
rerender(
|
|
748
|
+
<>
|
|
749
|
+
<PktTabs togglePanels>
|
|
750
|
+
{[0, 1, 2].map((i) => (
|
|
751
|
+
<PktTabItem key={i} index={i} controls={`panel-${i}`} active={i === 0}>
|
|
752
|
+
{`Tab ${i + 1}`}
|
|
753
|
+
</PktTabItem>
|
|
754
|
+
))}
|
|
755
|
+
</PktTabs>
|
|
756
|
+
<Panels />
|
|
757
|
+
</>,
|
|
758
|
+
)
|
|
759
|
+
|
|
760
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, false, true])
|
|
761
|
+
})
|
|
762
|
+
|
|
763
|
+
it('does not select a disabled tab', () => {
|
|
764
|
+
const { buttons, panels } = renderWithPanels({ togglePanels: true }, 0, 1)
|
|
765
|
+
|
|
766
|
+
fireEvent.keyDown(buttons[1], { key: 'Enter' })
|
|
767
|
+
|
|
768
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([false, true, true])
|
|
769
|
+
})
|
|
770
|
+
|
|
771
|
+
it('works with items wrapped in other elements', () => {
|
|
772
|
+
const { getByText } = render(
|
|
773
|
+
<>
|
|
774
|
+
<PktTabs togglePanels>
|
|
775
|
+
{[0, 1, 2].map((i) => (
|
|
776
|
+
<div key={i}>
|
|
777
|
+
<PktTabItem index={i} controls={`panel-${i}`} active={i === 0}>
|
|
778
|
+
{`Tab ${i + 1}`}
|
|
779
|
+
</PktTabItem>
|
|
780
|
+
</div>
|
|
781
|
+
))}
|
|
782
|
+
</PktTabs>
|
|
783
|
+
<Panels />
|
|
784
|
+
</>,
|
|
785
|
+
)
|
|
786
|
+
|
|
787
|
+
fireEvent.click(getByText('Tab 3'))
|
|
788
|
+
|
|
789
|
+
expect([0, 1, 2].map((i) => document.getElementById(`panel-${i}`)?.hidden)).toEqual([true, true, false])
|
|
790
|
+
})
|
|
791
|
+
|
|
792
|
+
it('works with the tabs prop', () => {
|
|
793
|
+
const { getByText } = render(
|
|
794
|
+
<>
|
|
795
|
+
<PktTabs
|
|
796
|
+
togglePanels
|
|
797
|
+
tabs={[0, 1, 2].map((i) => ({ text: `Tab ${i + 1}`, controls: `panel-${i}`, active: i === 0 }))}
|
|
798
|
+
/>
|
|
799
|
+
<Panels />
|
|
800
|
+
</>,
|
|
801
|
+
)
|
|
802
|
+
|
|
803
|
+
fireEvent.click(getByText('Tab 2'))
|
|
804
|
+
|
|
805
|
+
expect([0, 1, 2].map((i) => document.getElementById(`panel-${i}`)?.hidden)).toEqual([true, false, true])
|
|
806
|
+
})
|
|
807
|
+
|
|
808
|
+
it('links tabs and panels', () => {
|
|
809
|
+
const { buttons, panels } = renderWithPanels()
|
|
810
|
+
|
|
811
|
+
expect(buttons[0]).toHaveAttribute('id', 'panel-0-tab')
|
|
812
|
+
expect(panels[0]).toHaveAttribute('role', 'tabpanel')
|
|
813
|
+
expect(panels[0]).toHaveAttribute('aria-labelledby', 'panel-0-tab')
|
|
814
|
+
expect(panels[0]).toHaveAttribute('tabindex', '0')
|
|
815
|
+
expect(panels[1]).toHaveAttribute('aria-labelledby', 'panel-1-tab')
|
|
816
|
+
})
|
|
817
|
+
|
|
818
|
+
it('keeps role, label and tabindex already set on the panel', () => {
|
|
819
|
+
render(
|
|
820
|
+
<>
|
|
821
|
+
<PktTabs togglePanels>
|
|
822
|
+
<PktTabItem index={0} controls="panel-0" active>
|
|
823
|
+
Tab 1
|
|
824
|
+
</PktTabItem>
|
|
825
|
+
</PktTabs>
|
|
826
|
+
<Panels first={{ role: 'region', 'aria-label': 'Egen', tabIndex: '-1' }} />
|
|
827
|
+
</>,
|
|
828
|
+
)
|
|
829
|
+
const panel = document.getElementById('panel-0')
|
|
830
|
+
|
|
831
|
+
expect(panel).toHaveAttribute('role', 'region')
|
|
832
|
+
expect(panel).toHaveAttribute('tabindex', '-1')
|
|
833
|
+
expect(panel).not.toHaveAttribute('aria-labelledby')
|
|
834
|
+
})
|
|
835
|
+
|
|
836
|
+
it('selects the tab that arrow keys, Home and End move to', () => {
|
|
837
|
+
const onTabSelected = vi.fn()
|
|
838
|
+
const { buttons, panels } = renderWithPanels({ togglePanels: true, onTabSelected })
|
|
839
|
+
|
|
840
|
+
buttons[0].focus()
|
|
841
|
+
fireEvent.keyDown(buttons[0], { key: 'ArrowRight' })
|
|
842
|
+
expect(buttons[1]).toHaveAttribute('aria-selected', 'true')
|
|
843
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, false, true])
|
|
844
|
+
|
|
845
|
+
fireEvent.keyDown(buttons[1], { key: 'End' })
|
|
846
|
+
expect(buttons[2]).toHaveFocus()
|
|
847
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, true, false])
|
|
848
|
+
|
|
849
|
+
fireEvent.keyDown(buttons[2], { key: 'End' })
|
|
850
|
+
expect(onTabSelected.mock.calls).toEqual([[1], [2]])
|
|
851
|
+
})
|
|
852
|
+
|
|
853
|
+
it('toggles panels without tab semantics when arrow navigation is disabled', () => {
|
|
854
|
+
const { buttons, panels } = renderWithPanels({ togglePanels: true, disableArrowNav: true })
|
|
855
|
+
|
|
856
|
+
fireEvent.click(buttons[1])
|
|
857
|
+
|
|
858
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, false, true])
|
|
859
|
+
expect(panels[1]).not.toHaveAttribute('role')
|
|
860
|
+
expect(panels[1]).not.toHaveAttribute('aria-labelledby')
|
|
861
|
+
expect(panels[1]).not.toHaveAttribute('tabindex')
|
|
862
|
+
})
|
|
863
|
+
|
|
864
|
+
it('leaves panels and markup alone without togglePanels', () => {
|
|
865
|
+
const { buttons, panels } = renderWithPanels({})
|
|
866
|
+
|
|
867
|
+
fireEvent.click(buttons[1])
|
|
868
|
+
|
|
869
|
+
expect(buttons[0]).toHaveClass('active')
|
|
870
|
+
expect(buttons[1]).not.toHaveClass('active')
|
|
871
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([false, false, false])
|
|
872
|
+
expect(panels[0]).not.toHaveAttribute('role')
|
|
873
|
+
expect(buttons[0]).not.toHaveAttribute('id')
|
|
874
|
+
})
|
|
875
|
+
|
|
876
|
+
it('should not have any accessibility violations', async () => {
|
|
877
|
+
const { container } = renderWithPanels()
|
|
878
|
+
|
|
879
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
880
|
+
})
|
|
881
|
+
})
|
|
882
|
+
|
|
883
|
+
describe('labels and current item', () => {
|
|
884
|
+
it('labels the tab list with aria-label', async () => {
|
|
885
|
+
const { container } = render(
|
|
886
|
+
<PktTabs ariaLabel="Faner">
|
|
887
|
+
<PktTabItem index={0} active>
|
|
888
|
+
Tab 1
|
|
889
|
+
</PktTabItem>
|
|
890
|
+
</PktTabs>,
|
|
891
|
+
)
|
|
892
|
+
|
|
893
|
+
expect(container.querySelector('.pkt-tabs__list')).toHaveAttribute('aria-label', 'Faner')
|
|
894
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
895
|
+
})
|
|
896
|
+
|
|
897
|
+
it('labels the tab list with aria-labelledby', () => {
|
|
898
|
+
const { container } = render(
|
|
899
|
+
<PktTabs ariaLabelledBy="overskrift">
|
|
900
|
+
<PktTabItem index={0} active>
|
|
901
|
+
Tab 1
|
|
902
|
+
</PktTabItem>
|
|
903
|
+
</PktTabs>,
|
|
904
|
+
)
|
|
905
|
+
|
|
906
|
+
expect(container.querySelector('.pkt-tabs__list')).toHaveAttribute('aria-labelledby', 'overskrift')
|
|
907
|
+
})
|
|
908
|
+
|
|
909
|
+
it('marks the active link as the current page in navigation mode', () => {
|
|
910
|
+
const { getByText } = render(
|
|
911
|
+
<PktTabs disableArrowNav>
|
|
912
|
+
<PktTabItem index={0} href="/a" active>
|
|
913
|
+
A
|
|
914
|
+
</PktTabItem>
|
|
915
|
+
<PktTabItem index={1} href="/b">
|
|
916
|
+
B
|
|
917
|
+
</PktTabItem>
|
|
918
|
+
</PktTabs>,
|
|
919
|
+
)
|
|
920
|
+
|
|
921
|
+
expect(getByText('A')).toHaveAttribute('aria-current', 'page')
|
|
922
|
+
expect(getByText('B')).not.toHaveAttribute('aria-current')
|
|
923
|
+
})
|
|
924
|
+
|
|
925
|
+
it('marks the active button as current in navigation mode', () => {
|
|
926
|
+
const { getByText } = render(
|
|
927
|
+
<PktTabs disableArrowNav>
|
|
928
|
+
<PktTabItem index={0} active>
|
|
929
|
+
A
|
|
930
|
+
</PktTabItem>
|
|
931
|
+
</PktTabs>,
|
|
932
|
+
)
|
|
933
|
+
|
|
934
|
+
expect(getByText('A')).toHaveAttribute('aria-current', 'true')
|
|
935
|
+
})
|
|
936
|
+
|
|
937
|
+
it('uses aria-selected, not aria-current, with tab semantics', () => {
|
|
938
|
+
const { getByText } = render(
|
|
939
|
+
<PktTabs>
|
|
940
|
+
<PktTabItem index={0} active>
|
|
941
|
+
A
|
|
942
|
+
</PktTabItem>
|
|
943
|
+
</PktTabs>,
|
|
944
|
+
)
|
|
945
|
+
|
|
946
|
+
expect(getByText('A')).not.toHaveAttribute('aria-current')
|
|
947
|
+
})
|
|
948
|
+
|
|
949
|
+
it('gives the tag the blue skin by default', () => {
|
|
950
|
+
const { getByText } = render(
|
|
951
|
+
<PktTabs>
|
|
952
|
+
<PktTabItem index={0} tag="Ny">
|
|
953
|
+
A
|
|
954
|
+
</PktTabItem>
|
|
955
|
+
</PktTabs>,
|
|
956
|
+
)
|
|
957
|
+
|
|
958
|
+
expect(getByText('Ny').closest('.pkt-tag')).toHaveClass('pkt-tag--blue')
|
|
959
|
+
})
|
|
960
|
+
})
|
|
961
|
+
|
|
962
|
+
describe('focus without an active tab', () => {
|
|
963
|
+
const renderTabs = (activeIndex?: number) => (
|
|
964
|
+
<PktTabs>
|
|
965
|
+
<PktTabItem index={0} disabled>
|
|
966
|
+
Tab 1
|
|
967
|
+
</PktTabItem>
|
|
968
|
+
<PktTabItem index={1} active={activeIndex === 1}>
|
|
969
|
+
Tab 2
|
|
970
|
+
</PktTabItem>
|
|
971
|
+
<PktTabItem index={2} active={activeIndex === 2}>
|
|
972
|
+
Tab 3
|
|
973
|
+
</PktTabItem>
|
|
974
|
+
</PktTabs>
|
|
975
|
+
)
|
|
976
|
+
|
|
977
|
+
it('makes the first enabled tab reachable with Tab', () => {
|
|
978
|
+
const { getByText } = render(renderTabs())
|
|
979
|
+
|
|
980
|
+
expect(getByText('Tab 1')).toHaveAttribute('tabindex', '-1')
|
|
981
|
+
expect(getByText('Tab 2')).not.toHaveAttribute('tabindex')
|
|
982
|
+
expect(getByText('Tab 3')).toHaveAttribute('tabindex', '-1')
|
|
983
|
+
})
|
|
984
|
+
|
|
985
|
+
it('drops the fallback once a tab is active', () => {
|
|
986
|
+
const { getByText, rerender } = render(renderTabs())
|
|
987
|
+
|
|
988
|
+
rerender(renderTabs(2))
|
|
989
|
+
|
|
990
|
+
expect(getByText('Tab 2')).toHaveAttribute('tabindex', '-1')
|
|
991
|
+
expect(getByText('Tab 3')).not.toHaveAttribute('tabindex')
|
|
992
|
+
})
|
|
993
|
+
|
|
994
|
+
it('works with items wrapped in other elements', () => {
|
|
995
|
+
const { getByText } = render(
|
|
996
|
+
<PktTabs>
|
|
997
|
+
{[0, 1].map((i) => (
|
|
998
|
+
<div key={i}>
|
|
999
|
+
<PktTabItem index={i}>{`Tab ${i + 1}`}</PktTabItem>
|
|
1000
|
+
</div>
|
|
1001
|
+
))}
|
|
1002
|
+
</PktTabs>,
|
|
1003
|
+
)
|
|
1004
|
+
|
|
1005
|
+
expect(getByText('Tab 1')).not.toHaveAttribute('tabindex')
|
|
1006
|
+
expect(getByText('Tab 2')).toHaveAttribute('tabindex', '-1')
|
|
1007
|
+
})
|
|
1008
|
+
})
|