@oslokommune/punkt-react 20.1.7 → 20.2.1
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 +24 -0
- package/dist/components/interfaces.d.ts +2 -1
- package/dist/components/tabs/TabItem.d.ts +2 -10
- package/dist/components/tabs/TabItem.js +66 -36
- package/dist/components/tabs/Tabs.d.ts +13 -20
- package/dist/components/tabs/Tabs.js +55 -62
- package/dist/punkt-react.cjs +1 -1
- package/dist/shared-types/index.d.ts +2 -0
- package/dist/shared-types/index.js +7 -6
- package/dist/shared-types/tabs.d.ts +66 -0
- package/dist/shared-types/tabs.js +12 -0
- 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/interfaces.ts +2 -1
- package/src/components/tabs/TabItem.tsx +57 -17
- package/src/components/tabs/Tabs.test.tsx +447 -5
- package/src/components/tabs/Tabs.tsx +69 -61
|
@@ -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,32 @@ 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('reaches every tab when a conditional tab is hidden', () => {
|
|
292
|
+
const renderWith = (showMiddle: boolean) => (
|
|
293
|
+
<PktTabs>
|
|
294
|
+
<PktTabItem index={0} active>
|
|
295
|
+
First Tab
|
|
296
|
+
</PktTabItem>
|
|
297
|
+
{showMiddle && <PktTabItem index={1}>Second Tab</PktTabItem>}
|
|
298
|
+
<PktTabItem index={2}>Third Tab</PktTabItem>
|
|
299
|
+
</PktTabs>
|
|
300
|
+
)
|
|
301
|
+
const { getByText, rerender } = render(renderWith(true))
|
|
302
|
+
rerender(renderWith(false))
|
|
303
|
+
|
|
304
|
+
const firstTab = getByText('First Tab')
|
|
305
|
+
firstTab.focus()
|
|
306
|
+
fireEvent.keyDown(firstTab, { key: 'ArrowRight' })
|
|
307
|
+
expect(getByText('Third Tab')).toHaveFocus()
|
|
308
|
+
|
|
309
|
+
fireEvent.keyDown(getByText('Third Tab'), { key: 'ArrowRight' })
|
|
310
|
+
expect(firstTab).toHaveFocus()
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
it('wraps to the last tab with ArrowLeft on the first tab', () => {
|
|
292
314
|
const { getByText } = render(
|
|
293
315
|
<PktTabs>
|
|
294
316
|
<PktTabItem index={0} active>
|
|
@@ -304,7 +326,71 @@ describe('PktTabItem children format', () => {
|
|
|
304
326
|
firstTab.focus()
|
|
305
327
|
fireEvent.keyDown(firstTab, { key: 'ArrowLeft' })
|
|
306
328
|
|
|
307
|
-
expect(
|
|
329
|
+
expect(getByText('Third Tab')).toHaveFocus()
|
|
330
|
+
})
|
|
331
|
+
|
|
332
|
+
it('moves focus to the first and last tab with Home and End', () => {
|
|
333
|
+
const { getByText } = render(
|
|
334
|
+
<PktTabs>
|
|
335
|
+
<PktTabItem index={0}>First Tab</PktTabItem>
|
|
336
|
+
<PktTabItem index={1} active>
|
|
337
|
+
Second Tab
|
|
338
|
+
</PktTabItem>
|
|
339
|
+
<PktTabItem index={2}>Third Tab</PktTabItem>
|
|
340
|
+
</PktTabs>,
|
|
341
|
+
)
|
|
342
|
+
|
|
343
|
+
getByText('Second Tab').focus()
|
|
344
|
+
fireEvent.keyDown(getByText('Second Tab'), { key: 'End' })
|
|
345
|
+
expect(getByText('Third Tab')).toHaveFocus()
|
|
346
|
+
|
|
347
|
+
fireEvent.keyDown(getByText('Third Tab'), { key: 'Home' })
|
|
348
|
+
expect(getByText('First Tab')).toHaveFocus()
|
|
349
|
+
})
|
|
350
|
+
|
|
351
|
+
it('skips disabled tabs when wrapping and with Home and End', () => {
|
|
352
|
+
const { getByText } = render(
|
|
353
|
+
<PktTabs>
|
|
354
|
+
<PktTabItem index={0} disabled>
|
|
355
|
+
Tab 1
|
|
356
|
+
</PktTabItem>
|
|
357
|
+
<PktTabItem index={1} active>
|
|
358
|
+
Tab 2
|
|
359
|
+
</PktTabItem>
|
|
360
|
+
<PktTabItem index={2} disabled>
|
|
361
|
+
Tab 3
|
|
362
|
+
</PktTabItem>
|
|
363
|
+
<PktTabItem index={3}>Tab 4</PktTabItem>
|
|
364
|
+
</PktTabs>,
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
getByText('Tab 4').focus()
|
|
368
|
+
fireEvent.keyDown(getByText('Tab 4'), { key: 'ArrowRight' })
|
|
369
|
+
expect(getByText('Tab 2')).toHaveFocus()
|
|
370
|
+
|
|
371
|
+
fireEvent.keyDown(getByText('Tab 2'), { key: 'Home' })
|
|
372
|
+
expect(getByText('Tab 2')).toHaveFocus()
|
|
373
|
+
|
|
374
|
+
fireEvent.keyDown(getByText('Tab 2'), { key: 'End' })
|
|
375
|
+
expect(getByText('Tab 4')).toHaveFocus()
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
it('does not select a tab when moving focus without togglePanels', () => {
|
|
379
|
+
const onTabSelected = vi.fn()
|
|
380
|
+
const { getByText } = render(
|
|
381
|
+
<PktTabs onTabSelected={onTabSelected}>
|
|
382
|
+
<PktTabItem index={0} active>
|
|
383
|
+
First Tab
|
|
384
|
+
</PktTabItem>
|
|
385
|
+
<PktTabItem index={1}>Second Tab</PktTabItem>
|
|
386
|
+
</PktTabs>,
|
|
387
|
+
)
|
|
388
|
+
|
|
389
|
+
getByText('First Tab').focus()
|
|
390
|
+
fireEvent.keyDown(getByText('First Tab'), { key: 'ArrowRight' })
|
|
391
|
+
fireEvent.keyDown(getByText('Second Tab'), { key: 'End' })
|
|
392
|
+
|
|
393
|
+
expect(onTabSelected).not.toHaveBeenCalled()
|
|
308
394
|
})
|
|
309
395
|
|
|
310
396
|
it('selects tab when Space key is pressed', () => {
|
|
@@ -586,3 +672,359 @@ describe('PktTabItem children format', () => {
|
|
|
586
672
|
})
|
|
587
673
|
})
|
|
588
674
|
})
|
|
675
|
+
|
|
676
|
+
describe('togglePanels', () => {
|
|
677
|
+
const Panels = ({ first = {} }: { first?: Record<string, string> }) => (
|
|
678
|
+
<>
|
|
679
|
+
<div id="panel-0" {...first}>
|
|
680
|
+
Panel 1
|
|
681
|
+
</div>
|
|
682
|
+
<div id="panel-1">Panel 2</div>
|
|
683
|
+
<div id="panel-2">Panel 3</div>
|
|
684
|
+
</>
|
|
685
|
+
)
|
|
686
|
+
|
|
687
|
+
const renderWithPanels = (props: IPktTabs = { togglePanels: true }, activeIndex = 0, disabledIndex?: number) => {
|
|
688
|
+
const result = render(
|
|
689
|
+
<>
|
|
690
|
+
<PktTabs {...props}>
|
|
691
|
+
{[0, 1, 2].map((i) => (
|
|
692
|
+
<PktTabItem
|
|
693
|
+
key={i}
|
|
694
|
+
index={i}
|
|
695
|
+
controls={`panel-${i}`}
|
|
696
|
+
active={i === activeIndex}
|
|
697
|
+
disabled={i === disabledIndex}
|
|
698
|
+
>
|
|
699
|
+
{`Tab ${i + 1}`}
|
|
700
|
+
</PktTabItem>
|
|
701
|
+
))}
|
|
702
|
+
</PktTabs>
|
|
703
|
+
<Panels />
|
|
704
|
+
</>,
|
|
705
|
+
)
|
|
706
|
+
const buttons = [0, 1, 2].map((i) => result.getByText(`Tab ${i + 1}`))
|
|
707
|
+
const panels = [0, 1, 2].map((i) => document.getElementById(`panel-${i}`) as HTMLElement)
|
|
708
|
+
return { ...result, buttons, panels }
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
it('shows only the panel of the active tab', () => {
|
|
712
|
+
const { panels } = renderWithPanels()
|
|
713
|
+
|
|
714
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([false, true, true])
|
|
715
|
+
})
|
|
716
|
+
|
|
717
|
+
it('switches active tab and panel on click', () => {
|
|
718
|
+
const { buttons, panels } = renderWithPanels()
|
|
719
|
+
|
|
720
|
+
fireEvent.click(buttons[1])
|
|
721
|
+
|
|
722
|
+
expect(buttons[1]).toHaveClass('active')
|
|
723
|
+
expect(buttons[1]).toHaveAttribute('aria-selected', 'true')
|
|
724
|
+
expect(buttons[0]).toHaveAttribute('aria-selected', 'false')
|
|
725
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, false, true])
|
|
726
|
+
})
|
|
727
|
+
|
|
728
|
+
it('switches panel with Enter', () => {
|
|
729
|
+
const { buttons, panels } = renderWithPanels()
|
|
730
|
+
|
|
731
|
+
fireEvent.keyDown(buttons[2], { key: 'Enter' })
|
|
732
|
+
|
|
733
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, true, false])
|
|
734
|
+
})
|
|
735
|
+
|
|
736
|
+
it('still calls onTabSelected', () => {
|
|
737
|
+
const onTabSelected = vi.fn()
|
|
738
|
+
const { buttons } = renderWithPanels({ togglePanels: true, onTabSelected })
|
|
739
|
+
|
|
740
|
+
fireEvent.click(buttons[1])
|
|
741
|
+
|
|
742
|
+
expect(onTabSelected).toHaveBeenCalledWith(1)
|
|
743
|
+
})
|
|
744
|
+
|
|
745
|
+
it('follows active when the prop changes', () => {
|
|
746
|
+
const { buttons, panels, rerender } = renderWithPanels()
|
|
747
|
+
fireEvent.click(buttons[1])
|
|
748
|
+
|
|
749
|
+
rerender(
|
|
750
|
+
<>
|
|
751
|
+
<PktTabs togglePanels>
|
|
752
|
+
{[0, 1, 2].map((i) => (
|
|
753
|
+
<PktTabItem key={i} index={i} controls={`panel-${i}`} active={i === 2}>
|
|
754
|
+
{`Tab ${i + 1}`}
|
|
755
|
+
</PktTabItem>
|
|
756
|
+
))}
|
|
757
|
+
</PktTabs>
|
|
758
|
+
<Panels />
|
|
759
|
+
</>,
|
|
760
|
+
)
|
|
761
|
+
|
|
762
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, true, false])
|
|
763
|
+
})
|
|
764
|
+
|
|
765
|
+
it('keeps the selected tab when an unchanged active prop rerenders', () => {
|
|
766
|
+
const { buttons, panels, rerender } = renderWithPanels()
|
|
767
|
+
fireEvent.click(buttons[1])
|
|
768
|
+
|
|
769
|
+
rerender(
|
|
770
|
+
<>
|
|
771
|
+
<PktTabs togglePanels>
|
|
772
|
+
{[0, 1, 2].map((i) => (
|
|
773
|
+
<PktTabItem key={i} index={i} controls={`panel-${i}`} active={i === 0}>
|
|
774
|
+
{`Tab ${i + 1}`}
|
|
775
|
+
</PktTabItem>
|
|
776
|
+
))}
|
|
777
|
+
</PktTabs>
|
|
778
|
+
<Panels />
|
|
779
|
+
</>,
|
|
780
|
+
)
|
|
781
|
+
|
|
782
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, false, true])
|
|
783
|
+
})
|
|
784
|
+
|
|
785
|
+
it('does not select a disabled tab', () => {
|
|
786
|
+
const { buttons, panels } = renderWithPanels({ togglePanels: true }, 0, 1)
|
|
787
|
+
|
|
788
|
+
fireEvent.keyDown(buttons[1], { key: 'Enter' })
|
|
789
|
+
|
|
790
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([false, true, true])
|
|
791
|
+
})
|
|
792
|
+
|
|
793
|
+
it('works with items wrapped in other elements', () => {
|
|
794
|
+
const { getByText } = render(
|
|
795
|
+
<>
|
|
796
|
+
<PktTabs togglePanels>
|
|
797
|
+
{[0, 1, 2].map((i) => (
|
|
798
|
+
<div key={i}>
|
|
799
|
+
<PktTabItem index={i} controls={`panel-${i}`} active={i === 0}>
|
|
800
|
+
{`Tab ${i + 1}`}
|
|
801
|
+
</PktTabItem>
|
|
802
|
+
</div>
|
|
803
|
+
))}
|
|
804
|
+
</PktTabs>
|
|
805
|
+
<Panels />
|
|
806
|
+
</>,
|
|
807
|
+
)
|
|
808
|
+
|
|
809
|
+
fireEvent.click(getByText('Tab 3'))
|
|
810
|
+
|
|
811
|
+
expect([0, 1, 2].map((i) => document.getElementById(`panel-${i}`)?.hidden)).toEqual([true, true, false])
|
|
812
|
+
})
|
|
813
|
+
|
|
814
|
+
it('works with the tabs prop', () => {
|
|
815
|
+
const { getByText } = render(
|
|
816
|
+
<>
|
|
817
|
+
<PktTabs
|
|
818
|
+
togglePanels
|
|
819
|
+
tabs={[0, 1, 2].map((i) => ({ text: `Tab ${i + 1}`, controls: `panel-${i}`, active: i === 0 }))}
|
|
820
|
+
/>
|
|
821
|
+
<Panels />
|
|
822
|
+
</>,
|
|
823
|
+
)
|
|
824
|
+
|
|
825
|
+
fireEvent.click(getByText('Tab 2'))
|
|
826
|
+
|
|
827
|
+
expect([0, 1, 2].map((i) => document.getElementById(`panel-${i}`)?.hidden)).toEqual([true, false, true])
|
|
828
|
+
})
|
|
829
|
+
|
|
830
|
+
it('links tabs and panels', () => {
|
|
831
|
+
const { buttons, panels } = renderWithPanels()
|
|
832
|
+
|
|
833
|
+
expect(buttons[0]).toHaveAttribute('id', 'panel-0-tab')
|
|
834
|
+
expect(panels[0]).toHaveAttribute('role', 'tabpanel')
|
|
835
|
+
expect(panels[0]).toHaveAttribute('aria-labelledby', 'panel-0-tab')
|
|
836
|
+
expect(panels[0]).toHaveAttribute('tabindex', '0')
|
|
837
|
+
expect(panels[1]).toHaveAttribute('aria-labelledby', 'panel-1-tab')
|
|
838
|
+
})
|
|
839
|
+
|
|
840
|
+
it('keeps role, label and tabindex already set on the panel', () => {
|
|
841
|
+
render(
|
|
842
|
+
<>
|
|
843
|
+
<PktTabs togglePanels>
|
|
844
|
+
<PktTabItem index={0} controls="panel-0" active>
|
|
845
|
+
Tab 1
|
|
846
|
+
</PktTabItem>
|
|
847
|
+
</PktTabs>
|
|
848
|
+
<Panels first={{ role: 'region', 'aria-label': 'Egen', tabIndex: '-1' }} />
|
|
849
|
+
</>,
|
|
850
|
+
)
|
|
851
|
+
const panel = document.getElementById('panel-0')
|
|
852
|
+
|
|
853
|
+
expect(panel).toHaveAttribute('role', 'region')
|
|
854
|
+
expect(panel).toHaveAttribute('tabindex', '-1')
|
|
855
|
+
expect(panel).not.toHaveAttribute('aria-labelledby')
|
|
856
|
+
})
|
|
857
|
+
|
|
858
|
+
it('selects the tab that arrow keys, Home and End move to', () => {
|
|
859
|
+
const onTabSelected = vi.fn()
|
|
860
|
+
const { buttons, panels } = renderWithPanels({ togglePanels: true, onTabSelected })
|
|
861
|
+
|
|
862
|
+
buttons[0].focus()
|
|
863
|
+
fireEvent.keyDown(buttons[0], { key: 'ArrowRight' })
|
|
864
|
+
expect(buttons[1]).toHaveAttribute('aria-selected', 'true')
|
|
865
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, false, true])
|
|
866
|
+
|
|
867
|
+
fireEvent.keyDown(buttons[1], { key: 'End' })
|
|
868
|
+
expect(buttons[2]).toHaveFocus()
|
|
869
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, true, false])
|
|
870
|
+
|
|
871
|
+
fireEvent.keyDown(buttons[2], { key: 'End' })
|
|
872
|
+
expect(onTabSelected.mock.calls).toEqual([[1], [2]])
|
|
873
|
+
})
|
|
874
|
+
|
|
875
|
+
it('toggles panels without tab semantics when arrow navigation is disabled', () => {
|
|
876
|
+
const { buttons, panels } = renderWithPanels({ togglePanels: true, disableArrowNav: true })
|
|
877
|
+
|
|
878
|
+
fireEvent.click(buttons[1])
|
|
879
|
+
|
|
880
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([true, false, true])
|
|
881
|
+
expect(panels[1]).not.toHaveAttribute('role')
|
|
882
|
+
expect(panels[1]).not.toHaveAttribute('aria-labelledby')
|
|
883
|
+
expect(panels[1]).not.toHaveAttribute('tabindex')
|
|
884
|
+
})
|
|
885
|
+
|
|
886
|
+
it('leaves panels and markup alone without togglePanels', () => {
|
|
887
|
+
const { buttons, panels } = renderWithPanels({})
|
|
888
|
+
|
|
889
|
+
fireEvent.click(buttons[1])
|
|
890
|
+
|
|
891
|
+
expect(buttons[0]).toHaveClass('active')
|
|
892
|
+
expect(buttons[1]).not.toHaveClass('active')
|
|
893
|
+
expect(panels.map((panel) => panel.hidden)).toEqual([false, false, false])
|
|
894
|
+
expect(panels[0]).not.toHaveAttribute('role')
|
|
895
|
+
expect(buttons[0]).not.toHaveAttribute('id')
|
|
896
|
+
})
|
|
897
|
+
|
|
898
|
+
it('should not have any accessibility violations', async () => {
|
|
899
|
+
const { container } = renderWithPanels()
|
|
900
|
+
|
|
901
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
902
|
+
})
|
|
903
|
+
})
|
|
904
|
+
|
|
905
|
+
describe('labels and current item', () => {
|
|
906
|
+
it('labels the tab list with aria-label', async () => {
|
|
907
|
+
const { container } = render(
|
|
908
|
+
<PktTabs ariaLabel="Faner">
|
|
909
|
+
<PktTabItem index={0} active>
|
|
910
|
+
Tab 1
|
|
911
|
+
</PktTabItem>
|
|
912
|
+
</PktTabs>,
|
|
913
|
+
)
|
|
914
|
+
|
|
915
|
+
expect(container.querySelector('.pkt-tabs__list')).toHaveAttribute('aria-label', 'Faner')
|
|
916
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
917
|
+
})
|
|
918
|
+
|
|
919
|
+
it('labels the tab list with aria-labelledby', () => {
|
|
920
|
+
const { container } = render(
|
|
921
|
+
<PktTabs ariaLabelledBy="overskrift">
|
|
922
|
+
<PktTabItem index={0} active>
|
|
923
|
+
Tab 1
|
|
924
|
+
</PktTabItem>
|
|
925
|
+
</PktTabs>,
|
|
926
|
+
)
|
|
927
|
+
|
|
928
|
+
expect(container.querySelector('.pkt-tabs__list')).toHaveAttribute('aria-labelledby', 'overskrift')
|
|
929
|
+
})
|
|
930
|
+
|
|
931
|
+
it('marks the active link as the current page in navigation mode', () => {
|
|
932
|
+
const { getByText } = render(
|
|
933
|
+
<PktTabs disableArrowNav>
|
|
934
|
+
<PktTabItem index={0} href="/a" active>
|
|
935
|
+
A
|
|
936
|
+
</PktTabItem>
|
|
937
|
+
<PktTabItem index={1} href="/b">
|
|
938
|
+
B
|
|
939
|
+
</PktTabItem>
|
|
940
|
+
</PktTabs>,
|
|
941
|
+
)
|
|
942
|
+
|
|
943
|
+
expect(getByText('A')).toHaveAttribute('aria-current', 'page')
|
|
944
|
+
expect(getByText('B')).not.toHaveAttribute('aria-current')
|
|
945
|
+
})
|
|
946
|
+
|
|
947
|
+
it('marks the active button as current in navigation mode', () => {
|
|
948
|
+
const { getByText } = render(
|
|
949
|
+
<PktTabs disableArrowNav>
|
|
950
|
+
<PktTabItem index={0} active>
|
|
951
|
+
A
|
|
952
|
+
</PktTabItem>
|
|
953
|
+
</PktTabs>,
|
|
954
|
+
)
|
|
955
|
+
|
|
956
|
+
expect(getByText('A')).toHaveAttribute('aria-current', 'true')
|
|
957
|
+
})
|
|
958
|
+
|
|
959
|
+
it('uses aria-selected, not aria-current, with tab semantics', () => {
|
|
960
|
+
const { getByText } = render(
|
|
961
|
+
<PktTabs>
|
|
962
|
+
<PktTabItem index={0} active>
|
|
963
|
+
A
|
|
964
|
+
</PktTabItem>
|
|
965
|
+
</PktTabs>,
|
|
966
|
+
)
|
|
967
|
+
|
|
968
|
+
expect(getByText('A')).not.toHaveAttribute('aria-current')
|
|
969
|
+
})
|
|
970
|
+
|
|
971
|
+
it('gives the tag the blue skin by default', () => {
|
|
972
|
+
const { getByText } = render(
|
|
973
|
+
<PktTabs>
|
|
974
|
+
<PktTabItem index={0} tag="Ny">
|
|
975
|
+
A
|
|
976
|
+
</PktTabItem>
|
|
977
|
+
</PktTabs>,
|
|
978
|
+
)
|
|
979
|
+
|
|
980
|
+
expect(getByText('Ny').closest('.pkt-tag')).toHaveClass('pkt-tag--blue')
|
|
981
|
+
})
|
|
982
|
+
})
|
|
983
|
+
|
|
984
|
+
describe('focus without an active tab', () => {
|
|
985
|
+
const renderTabs = (activeIndex?: number) => (
|
|
986
|
+
<PktTabs>
|
|
987
|
+
<PktTabItem index={0} disabled>
|
|
988
|
+
Tab 1
|
|
989
|
+
</PktTabItem>
|
|
990
|
+
<PktTabItem index={1} active={activeIndex === 1}>
|
|
991
|
+
Tab 2
|
|
992
|
+
</PktTabItem>
|
|
993
|
+
<PktTabItem index={2} active={activeIndex === 2}>
|
|
994
|
+
Tab 3
|
|
995
|
+
</PktTabItem>
|
|
996
|
+
</PktTabs>
|
|
997
|
+
)
|
|
998
|
+
|
|
999
|
+
it('makes the first enabled tab reachable with Tab', () => {
|
|
1000
|
+
const { getByText } = render(renderTabs())
|
|
1001
|
+
|
|
1002
|
+
expect(getByText('Tab 1')).toHaveAttribute('tabindex', '-1')
|
|
1003
|
+
expect(getByText('Tab 2')).not.toHaveAttribute('tabindex')
|
|
1004
|
+
expect(getByText('Tab 3')).toHaveAttribute('tabindex', '-1')
|
|
1005
|
+
})
|
|
1006
|
+
|
|
1007
|
+
it('drops the fallback once a tab is active', () => {
|
|
1008
|
+
const { getByText, rerender } = render(renderTabs())
|
|
1009
|
+
|
|
1010
|
+
rerender(renderTabs(2))
|
|
1011
|
+
|
|
1012
|
+
expect(getByText('Tab 2')).toHaveAttribute('tabindex', '-1')
|
|
1013
|
+
expect(getByText('Tab 3')).not.toHaveAttribute('tabindex')
|
|
1014
|
+
})
|
|
1015
|
+
|
|
1016
|
+
it('works with items wrapped in other elements', () => {
|
|
1017
|
+
const { getByText } = render(
|
|
1018
|
+
<PktTabs>
|
|
1019
|
+
{[0, 1].map((i) => (
|
|
1020
|
+
<div key={i}>
|
|
1021
|
+
<PktTabItem index={i}>{`Tab ${i + 1}`}</PktTabItem>
|
|
1022
|
+
</div>
|
|
1023
|
+
))}
|
|
1024
|
+
</PktTabs>,
|
|
1025
|
+
)
|
|
1026
|
+
|
|
1027
|
+
expect(getByText('Tab 1')).not.toHaveAttribute('tabindex')
|
|
1028
|
+
expect(getByText('Tab 2')).toHaveAttribute('tabindex', '-1')
|
|
1029
|
+
})
|
|
1030
|
+
})
|
|
@@ -8,21 +8,30 @@ import {
|
|
|
8
8
|
KeyboardEvent,
|
|
9
9
|
ReactNode,
|
|
10
10
|
Ref,
|
|
11
|
+
useCallback,
|
|
11
12
|
useContext,
|
|
12
|
-
useEffect,
|
|
13
13
|
useRef,
|
|
14
|
+
useState,
|
|
14
15
|
} from 'react'
|
|
15
16
|
|
|
16
|
-
import type
|
|
17
|
+
import { TABS_DEFAULTS, type IPktTab, type IPktTabsBase } from 'shared-types'
|
|
18
|
+
import { tabIndexForKey } from 'shared-utils/tabs/keyboard'
|
|
17
19
|
|
|
18
20
|
import { PktIcon } from '../icon/Icon'
|
|
19
21
|
import { PktTabItem } from './TabItem'
|
|
20
22
|
import { withStableKeys } from '../../utils/stableKeys'
|
|
21
23
|
|
|
24
|
+
type TTabState = { active: boolean; disabled: boolean }
|
|
25
|
+
|
|
22
26
|
// Context for passing tab navigation logic to children
|
|
23
27
|
interface ITabsContext {
|
|
24
28
|
arrowNav: boolean
|
|
25
|
-
|
|
29
|
+
togglePanels: boolean
|
|
30
|
+
selectedIndex: number | null
|
|
31
|
+
setSelectedIndex: (index: number) => void
|
|
32
|
+
fallbackFocusIndex: number | null
|
|
33
|
+
reportTab: (index: number, state: TTabState | null) => void
|
|
34
|
+
registerTabRef: (index: number, el: HTMLAnchorElement | HTMLButtonElement | null) => void
|
|
26
35
|
handleKeyPress: (index: number, event: KeyboardEvent<HTMLAnchorElement | HTMLButtonElement>) => void
|
|
27
36
|
selectTab: (index: number) => void
|
|
28
37
|
}
|
|
@@ -37,25 +46,9 @@ export const useTabsContext = () => {
|
|
|
37
46
|
return context
|
|
38
47
|
}
|
|
39
48
|
|
|
40
|
-
export
|
|
41
|
-
text: string
|
|
42
|
-
href?: string
|
|
43
|
-
action?: (index: number) => void
|
|
44
|
-
disabled?: boolean
|
|
45
|
-
icon?: string
|
|
46
|
-
controls?: string
|
|
47
|
-
tag?: {
|
|
48
|
-
text: string
|
|
49
|
-
skin: TTagSkin | undefined
|
|
50
|
-
}
|
|
51
|
-
active?: boolean
|
|
52
|
-
}
|
|
49
|
+
export type { IPktTab }
|
|
53
50
|
|
|
54
|
-
export interface IPktTabs {
|
|
55
|
-
arrowNav?: boolean
|
|
56
|
-
disableArrowNav?: boolean
|
|
57
|
-
separatorIconName?: string
|
|
58
|
-
separatorIconPath?: string
|
|
51
|
+
export interface IPktTabs extends IPktTabsBase {
|
|
59
52
|
tabs?: IPktTab[]
|
|
60
53
|
onTabSelected?: (index: number) => void
|
|
61
54
|
children?: ReactNode
|
|
@@ -64,10 +57,13 @@ export interface IPktTabs {
|
|
|
64
57
|
export const PktTabs = forwardRef(
|
|
65
58
|
(
|
|
66
59
|
{
|
|
67
|
-
arrowNav =
|
|
68
|
-
disableArrowNav =
|
|
60
|
+
arrowNav = TABS_DEFAULTS.arrowNav,
|
|
61
|
+
disableArrowNav = TABS_DEFAULTS.disableArrowNav,
|
|
69
62
|
separatorIconName,
|
|
70
63
|
separatorIconPath,
|
|
64
|
+
togglePanels = TABS_DEFAULTS.togglePanels,
|
|
65
|
+
ariaLabel,
|
|
66
|
+
ariaLabelledBy,
|
|
71
67
|
tabs,
|
|
72
68
|
onTabSelected,
|
|
73
69
|
children,
|
|
@@ -75,7 +71,28 @@ export const PktTabs = forwardRef(
|
|
|
75
71
|
ref: Ref<HTMLDivElement>,
|
|
76
72
|
): JSX.Element => {
|
|
77
73
|
const tabElementsRef = useRef<Array<HTMLAnchorElement | HTMLButtonElement | null>>([])
|
|
78
|
-
const
|
|
74
|
+
const [selectedIndex, setSelectedIndex] = useState<number | null>(null)
|
|
75
|
+
const [tabStates, setTabStates] = useState<Record<number, TTabState> | null>(null)
|
|
76
|
+
|
|
77
|
+
const reportTab = useCallback((index: number, state: TTabState | null) => {
|
|
78
|
+
setTabStates((previous) => {
|
|
79
|
+
const current = previous?.[index]
|
|
80
|
+
const unchanged = state ? current?.active === state.active && current?.disabled === state.disabled : !current
|
|
81
|
+
if (unchanged) return previous
|
|
82
|
+
const next = { ...previous }
|
|
83
|
+
if (state) next[index] = state
|
|
84
|
+
else delete next[index]
|
|
85
|
+
return next
|
|
86
|
+
})
|
|
87
|
+
}, [])
|
|
88
|
+
|
|
89
|
+
const fallbackFocusIndex =
|
|
90
|
+
tabStates && !Object.values(tabStates).some((tab) => tab.active)
|
|
91
|
+
? (Object.keys(tabStates)
|
|
92
|
+
.map(Number)
|
|
93
|
+
.filter((index) => !tabStates[index].disabled)
|
|
94
|
+
.sort((a, b) => a - b)[0] ?? null)
|
|
95
|
+
: null
|
|
79
96
|
|
|
80
97
|
const useArrowNav = arrowNav && !disableArrowNav
|
|
81
98
|
|
|
@@ -83,21 +100,10 @@ export const PktTabs = forwardRef(
|
|
|
83
100
|
// eslint-disable-next-line @eslint-react/no-children-to-array
|
|
84
101
|
const childItems = Children.toArray(children)
|
|
85
102
|
const hasChildren = childItems.length > 0
|
|
86
|
-
const tabCount =
|
|
103
|
+
const tabCount = tabStates ? Math.max(0, ...Object.keys(tabStates).map((key) => Number(key) + 1)) : 0
|
|
87
104
|
const hasSeparator = !!(separatorIconName || separatorIconPath)
|
|
88
105
|
|
|
89
|
-
|
|
90
|
-
tabElementsRef.current = tabElementsRef.current.slice(0, tabCount)
|
|
91
|
-
Object.keys(disabledMapRef.current).forEach((key) => {
|
|
92
|
-
const index = Number(key)
|
|
93
|
-
if (index >= tabCount) delete disabledMapRef.current[index]
|
|
94
|
-
})
|
|
95
|
-
}, [tabCount])
|
|
96
|
-
|
|
97
|
-
const isTabDisabled = (index: number): boolean => {
|
|
98
|
-
if (tabs) return !!tabs[index]?.disabled
|
|
99
|
-
return !!disabledMapRef.current[index]
|
|
100
|
-
}
|
|
106
|
+
const isTabDisabled = (index: number): boolean => !tabStates?.[index] || tabStates[index].disabled
|
|
101
107
|
|
|
102
108
|
const selectTab = (index: number): void => {
|
|
103
109
|
if (isTabDisabled(index)) return
|
|
@@ -106,32 +112,18 @@ export const PktTabs = forwardRef(
|
|
|
106
112
|
tab.action(index)
|
|
107
113
|
}
|
|
108
114
|
if (onTabSelected) onTabSelected(index)
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const findEnabledIndex = (startIndex: number, direction: -1 | 1): number | null => {
|
|
112
|
-
let current = startIndex + direction
|
|
113
|
-
|
|
114
|
-
while (current >= 0 && current < tabCount) {
|
|
115
|
-
if (!isTabDisabled(current)) return current
|
|
116
|
-
current += direction
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return null
|
|
115
|
+
if (togglePanels) setSelectedIndex(index)
|
|
120
116
|
}
|
|
121
117
|
|
|
122
118
|
const handleKeyPress = (index: number, event: KeyboardEvent<HTMLAnchorElement | HTMLButtonElement>) => {
|
|
123
119
|
if (!useArrowNav) return
|
|
124
120
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const previousEnabled = findEnabledIndex(index, -1)
|
|
128
|
-
if (previousEnabled !== null) tabElementsRef.current[previousEnabled]?.focus()
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (event.key === 'ArrowRight') {
|
|
121
|
+
const target = tabIndexForKey(event.key, index, tabCount, isTabDisabled)
|
|
122
|
+
if (target !== null) {
|
|
132
123
|
event.preventDefault()
|
|
133
|
-
|
|
134
|
-
if (
|
|
124
|
+
tabElementsRef.current[target]?.focus()
|
|
125
|
+
if (togglePanels && target !== index) selectTab(target)
|
|
126
|
+
return
|
|
135
127
|
}
|
|
136
128
|
|
|
137
129
|
if (event.key === 'Enter' || event.key === ' ' || event.key === 'Spacebar' || event.key === 'ArrowDown') {
|
|
@@ -142,9 +134,8 @@ export const PktTabs = forwardRef(
|
|
|
142
134
|
}
|
|
143
135
|
}
|
|
144
136
|
|
|
145
|
-
const registerTabRef = (index: number, el: HTMLAnchorElement | HTMLButtonElement | null
|
|
137
|
+
const registerTabRef = (index: number, el: HTMLAnchorElement | HTMLButtonElement | null) => {
|
|
146
138
|
tabElementsRef.current[index] = el
|
|
147
|
-
disabledMapRef.current[index] = disabled
|
|
148
139
|
}
|
|
149
140
|
|
|
150
141
|
const renderSeparator = (key: string) => (
|
|
@@ -186,9 +177,26 @@ export const PktTabs = forwardRef(
|
|
|
186
177
|
const renderedTabs = withSeparators((hasChildren ? childItems : tabItems || []) as ReactNode[])
|
|
187
178
|
|
|
188
179
|
return (
|
|
189
|
-
<TabsContext.Provider
|
|
180
|
+
<TabsContext.Provider
|
|
181
|
+
value={{
|
|
182
|
+
arrowNav: useArrowNav,
|
|
183
|
+
togglePanels,
|
|
184
|
+
selectedIndex,
|
|
185
|
+
setSelectedIndex,
|
|
186
|
+
fallbackFocusIndex,
|
|
187
|
+
reportTab,
|
|
188
|
+
registerTabRef,
|
|
189
|
+
handleKeyPress,
|
|
190
|
+
selectTab,
|
|
191
|
+
}}
|
|
192
|
+
>
|
|
190
193
|
<div className={tabsClasses} ref={ref}>
|
|
191
|
-
<div
|
|
194
|
+
<div
|
|
195
|
+
className="pkt-tabs__list"
|
|
196
|
+
role={useArrowNav ? 'tablist' : 'navigation'}
|
|
197
|
+
aria-label={ariaLabel || undefined}
|
|
198
|
+
aria-labelledby={ariaLabelledBy || undefined}
|
|
199
|
+
>
|
|
192
200
|
{renderedTabs}
|
|
193
201
|
</div>
|
|
194
202
|
</div>
|