Skip to content

Commit e9f847f

Browse files
committed
Refactor tab navigation event handling logic and focus
1 parent 87ed313 commit e9f847f

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/components/ui/Tabs/fragments/TabTrigger.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export type TabTriggerProps = {
1818
const TabTrigger = ({ tab, className = '', ...props }: TabTriggerProps) => {
1919
// use context
2020
const { tabs, previousTab, nextTab, activeTab, setActiveTab, rootClass } = useContext(TabsRootContext);
21-
const ref = useRef(null);
21+
const ref = useRef<HTMLButtonElement>(null);
2222

2323
const isActive = activeTab === tab.value;
2424

@@ -28,20 +28,20 @@ const TabTrigger = ({ tab, className = '', ...props }: TabTriggerProps) => {
2828

2929
const handleKeyDownEvent = (e: React.KeyboardEvent) => {
3030
if (e.key == 'ArrowLeft') {
31-
const tab = previousTab();
32-
console.log(tab);
31+
previousTab();
3332
}
3433
if (e.key == 'ArrowRight') {
35-
const tab = nextTab();
36-
console.log(tab);
34+
nextTab();
3735
}
3836
};
3937

4038
const handleFocus = (tab: TabProps) => {
41-
ref.current.focus();
39+
if (ref.current) {
40+
ref.current?.focus();
41+
}
4242
setActiveTab(tab.value);
4343

44-
// This is a really cool idea, when a focus event is triggered, we can set the active tab to the tab that is being focused on
44+
// This is a good way to manage flow, when a focus event is triggered, we can set the active tab to the tab that is being focused on
4545
// This way, we dont need to keep track of the active tab in the parent component
4646
// This should be the defacto pattern we should follow for all components
4747
};

0 commit comments

Comments
 (0)