@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
|
@@ -8,20 +8,30 @@ import {
|
|
|
8
8
|
KeyboardEvent,
|
|
9
9
|
ReactNode,
|
|
10
10
|
Ref,
|
|
11
|
+
useCallback,
|
|
11
12
|
useContext,
|
|
12
13
|
useEffect,
|
|
13
14
|
useRef,
|
|
15
|
+
useState,
|
|
14
16
|
} from 'react'
|
|
15
17
|
|
|
16
18
|
import type { TTagSkin } from 'shared-types'
|
|
19
|
+
import { tabIndexForKey } from 'shared-utils/tabs/keyboard'
|
|
17
20
|
|
|
18
21
|
import { PktIcon } from '../icon/Icon'
|
|
19
22
|
import { PktTabItem } from './TabItem'
|
|
20
23
|
import { withStableKeys } from '../../utils/stableKeys'
|
|
21
24
|
|
|
25
|
+
type TTabState = { active: boolean; disabled: boolean }
|
|
26
|
+
|
|
22
27
|
// Context for passing tab navigation logic to children
|
|
23
28
|
interface ITabsContext {
|
|
24
29
|
arrowNav: boolean
|
|
30
|
+
togglePanels: boolean
|
|
31
|
+
selectedIndex: number | null
|
|
32
|
+
setSelectedIndex: (index: number) => void
|
|
33
|
+
fallbackFocusIndex: number | null
|
|
34
|
+
reportTab: (index: number, state: TTabState | null) => void
|
|
25
35
|
registerTabRef: (index: number, el: HTMLAnchorElement | HTMLButtonElement | null, disabled?: boolean) => void
|
|
26
36
|
handleKeyPress: (index: number, event: KeyboardEvent<HTMLAnchorElement | HTMLButtonElement>) => void
|
|
27
37
|
selectTab: (index: number) => void
|
|
@@ -56,6 +66,9 @@ export interface IPktTabs {
|
|
|
56
66
|
disableArrowNav?: boolean
|
|
57
67
|
separatorIconName?: string
|
|
58
68
|
separatorIconPath?: string
|
|
69
|
+
togglePanels?: boolean
|
|
70
|
+
ariaLabel?: string
|
|
71
|
+
ariaLabelledBy?: string
|
|
59
72
|
tabs?: IPktTab[]
|
|
60
73
|
onTabSelected?: (index: number) => void
|
|
61
74
|
children?: ReactNode
|
|
@@ -68,6 +81,9 @@ export const PktTabs = forwardRef(
|
|
|
68
81
|
disableArrowNav = false,
|
|
69
82
|
separatorIconName,
|
|
70
83
|
separatorIconPath,
|
|
84
|
+
togglePanels = false,
|
|
85
|
+
ariaLabel,
|
|
86
|
+
ariaLabelledBy,
|
|
71
87
|
tabs,
|
|
72
88
|
onTabSelected,
|
|
73
89
|
children,
|
|
@@ -76,6 +92,28 @@ export const PktTabs = forwardRef(
|
|
|
76
92
|
): JSX.Element => {
|
|
77
93
|
const tabElementsRef = useRef<Array<HTMLAnchorElement | HTMLButtonElement | null>>([])
|
|
78
94
|
const disabledMapRef = useRef<Record<number, boolean>>({})
|
|
95
|
+
const [selectedIndex, setSelectedIndex] = useState<number | null>(null)
|
|
96
|
+
const [tabStates, setTabStates] = useState<Record<number, TTabState> | null>(null)
|
|
97
|
+
|
|
98
|
+
const reportTab = useCallback((index: number, state: TTabState | null) => {
|
|
99
|
+
setTabStates((previous) => {
|
|
100
|
+
const current = previous?.[index]
|
|
101
|
+
const unchanged = state ? current?.active === state.active && current?.disabled === state.disabled : !current
|
|
102
|
+
if (unchanged) return previous
|
|
103
|
+
const next = { ...previous }
|
|
104
|
+
if (state) next[index] = state
|
|
105
|
+
else delete next[index]
|
|
106
|
+
return next
|
|
107
|
+
})
|
|
108
|
+
}, [])
|
|
109
|
+
|
|
110
|
+
const fallbackFocusIndex =
|
|
111
|
+
tabStates && !Object.values(tabStates).some((tab) => tab.active)
|
|
112
|
+
? (Object.keys(tabStates)
|
|
113
|
+
.map(Number)
|
|
114
|
+
.filter((index) => !tabStates[index].disabled)
|
|
115
|
+
.sort((a, b) => a - b)[0] ?? null)
|
|
116
|
+
: null
|
|
79
117
|
|
|
80
118
|
const useArrowNav = arrowNav && !disableArrowNav
|
|
81
119
|
|
|
@@ -106,32 +144,18 @@ export const PktTabs = forwardRef(
|
|
|
106
144
|
tab.action(index)
|
|
107
145
|
}
|
|
108
146
|
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
|
|
147
|
+
if (togglePanels) setSelectedIndex(index)
|
|
120
148
|
}
|
|
121
149
|
|
|
122
150
|
const handleKeyPress = (index: number, event: KeyboardEvent<HTMLAnchorElement | HTMLButtonElement>) => {
|
|
123
151
|
if (!useArrowNav) return
|
|
124
152
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const previousEnabled = findEnabledIndex(index, -1)
|
|
128
|
-
if (previousEnabled !== null) tabElementsRef.current[previousEnabled]?.focus()
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (event.key === 'ArrowRight') {
|
|
153
|
+
const target = tabIndexForKey(event.key, index, tabCount, isTabDisabled)
|
|
154
|
+
if (target !== null) {
|
|
132
155
|
event.preventDefault()
|
|
133
|
-
|
|
134
|
-
if (
|
|
156
|
+
tabElementsRef.current[target]?.focus()
|
|
157
|
+
if (togglePanels && target !== index) selectTab(target)
|
|
158
|
+
return
|
|
135
159
|
}
|
|
136
160
|
|
|
137
161
|
if (event.key === 'Enter' || event.key === ' ' || event.key === 'Spacebar' || event.key === 'ArrowDown') {
|
|
@@ -186,9 +210,26 @@ export const PktTabs = forwardRef(
|
|
|
186
210
|
const renderedTabs = withSeparators((hasChildren ? childItems : tabItems || []) as ReactNode[])
|
|
187
211
|
|
|
188
212
|
return (
|
|
189
|
-
<TabsContext.Provider
|
|
213
|
+
<TabsContext.Provider
|
|
214
|
+
value={{
|
|
215
|
+
arrowNav: useArrowNav,
|
|
216
|
+
togglePanels,
|
|
217
|
+
selectedIndex,
|
|
218
|
+
setSelectedIndex,
|
|
219
|
+
fallbackFocusIndex,
|
|
220
|
+
reportTab,
|
|
221
|
+
registerTabRef,
|
|
222
|
+
handleKeyPress,
|
|
223
|
+
selectTab,
|
|
224
|
+
}}
|
|
225
|
+
>
|
|
190
226
|
<div className={tabsClasses} ref={ref}>
|
|
191
|
-
<div
|
|
227
|
+
<div
|
|
228
|
+
className="pkt-tabs__list"
|
|
229
|
+
role={useArrowNav ? 'tablist' : 'navigation'}
|
|
230
|
+
aria-label={ariaLabel || undefined}
|
|
231
|
+
aria-labelledby={ariaLabelledBy || undefined}
|
|
232
|
+
>
|
|
192
233
|
{renderedTabs}
|
|
193
234
|
</div>
|
|
194
235
|
</div>
|