Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 58c58be

Browse files
scharingerTigge
authored andcommitted
feat(tab): make tabs interactive with keyboard
Add `tabIndex` to tabs so they can be interacted with using the keyboard. Add a disabled tab to docs.
1 parent 5d732da commit 58c58be

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

packages/core/src/Tab/index.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export function TabBase<T>({
144144
onSelect,
145145
markerOffset = 'right',
146146
children,
147+
onKeyDown,
147148
...props
148149
}: TabBaseProps<T>): JSX.Element {
149150
const onClick = useCallback<React.MouseEventHandler<HTMLDivElement>>(() => {
@@ -154,12 +155,29 @@ export function TabBase<T>({
154155
onSelect(id)
155156
}, [selected, disabled, id, onSelect])
156157

158+
const handleKeyDown = useCallback<React.KeyboardEventHandler<BaseElement>>(
159+
event => {
160+
onKeyDown?.(event)
161+
162+
if (selected || disabled) {
163+
return
164+
}
165+
166+
if (event.key === 'Enter') {
167+
onSelect(id)
168+
}
169+
},
170+
[selected, disabled, id, onSelect]
171+
)
172+
157173
return (
158174
<TabBaseContainer
159175
disabled={disabled}
160176
onClick={onClick}
161177
selected={selected}
162178
markerOffset={markerOffset}
179+
tabIndex={disabled ? undefined : 0}
180+
onKeyDown={handleKeyDown}
163181
{...props}
164182
>
165183
<TabBaseMarker offset={markerOffset} selected={selected} />

packages/docs/src/mdx/coreComponents/Tab.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import {
1717
HomeIcon,
1818
SystemAccessoriesIcon,
19+
AdaptiveIcon,
1920
} from 'practical-react-components-icons'
2021

2122
# Tab
@@ -70,6 +71,12 @@ export const VTABS = [
7071
},
7172
{
7273
id: '2',
74+
label: 'About',
75+
icon: AdaptiveIcon,
76+
disabled: true,
77+
},
78+
{
79+
id: '3',
7380
label: 'Contact',
7481
icon: SystemAccessoriesIcon,
7582
},
@@ -132,6 +139,7 @@ export const DemoComponent = ({}) => {
132139
icon={tab.icon}
133140
selected={tab.id === selectedVTab}
134141
onSelect={setSelectedVTab}
142+
disabled={tab.disabled}
135143
/>
136144
))}
137145
</VerticalTabContainer>

0 commit comments

Comments
 (0)