-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
137 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
import {createContext} from 'react'; | ||
|
||
const TabsRootContext = createContext(null); | ||
|
||
export default TabsRootContext; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
'use client'; | ||
import React, {useState} from 'react'; | ||
import {customClassSwitcher} from '~/core'; | ||
|
||
import TabsRootContext from '../context/TabsRootContext'; | ||
|
||
import {TabRootProps} from '../types'; | ||
|
||
const COMPONENT_NAME = 'Tabs'; | ||
|
||
|
||
const TabRoot = ({children, defaultTab='', customRootClass, tabs=[], className, color, ...props}: TabRootProps) => { | ||
const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME); | ||
|
||
const [activeTab, setActiveTab] = useState(defaultTab || tabs[0].value || ''); | ||
|
||
const nextTab = ()=>{ | ||
const currentIndex = tabs.findIndex(tab => tab.value === activeTab); | ||
const nextIndex = currentIndex + 1; | ||
if (nextIndex < tabs.length) { | ||
setActiveTab(tabs[nextIndex].value); | ||
} | ||
} | ||
|
||
const previousTab = ()=>{ | ||
const currentIndex = tabs.findIndex(tab => tab.value === activeTab); | ||
const previousIndex = currentIndex - 1; | ||
if (previousIndex >= 0) { | ||
setActiveTab(tabs[previousIndex].value); | ||
} | ||
} | ||
|
||
|
||
return ( | ||
<TabsRootContext.Provider value={{ | ||
activeTab, | ||
setActiveTab, | ||
nextTab, | ||
previousTab, | ||
tabs | ||
}}> | ||
<div className={`${rootClass} ${className}`} data-accent-color={color} {...props} > | ||
{children} | ||
</div> | ||
</TabsRootContext.Provider> | ||
); | ||
}; | ||
|
||
TabRoot.displayName = COMPONENT_NAME; | ||
|
||
export default TabRoot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use client'; | ||
import React, {useContext} from 'react'; | ||
import {customClassSwitcher} from '~/core'; | ||
import {TabProps} from '../types'; | ||
|
||
import TabsRootContext from '../context/TabsRootContext'; | ||
|
||
|
||
const COMPONENT_NAME = 'TabTrigger'; | ||
|
||
export type TabTriggerProps = { | ||
tab: TabProps; | ||
setActiveTab: React.Dispatch<Tab>; | ||
activeTab: TabProps; | ||
className?: string; | ||
customRootClass?: string; | ||
index: number; | ||
props?: Record<string, any>[] | ||
} | ||
|
||
const TabTrigger = ({tab, setActiveTab, activeTab, className, customRootClass, index, ...props}: TabTriggerProps) => { | ||
|
||
// use context | ||
const {tabs, previousTab, nextTab} = useContext(TabsRootContext); | ||
console.log(tabs); | ||
|
||
|
||
const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME); | ||
|
||
const isActive = activeTab === tab.value; | ||
|
||
const handleClick = (tab: Tab) => { | ||
setActiveTab(tab.value); | ||
}; | ||
|
||
const handleKeyDownEvent = (e: React.KeyboardEvent) => { | ||
console.log(e.key); | ||
if(e.key=="ArrowLeft"){ | ||
previousTab(); | ||
} | ||
if(e.key=="ArrowRight"){ | ||
nextTab(); | ||
} | ||
} | ||
|
||
return ( | ||
<button | ||
role="tab" key={index} className={`${rootClass} ${isActive?'active':''} ${className}`} {...props} onKeyDown={handleKeyDownEvent} | ||
onClick={() => handleClick(tab)}> | ||
<span className={`${rootClass}-inner`}> | ||
{tab.label} | ||
</span> | ||
</button> | ||
); | ||
}; | ||
|
||
TabTrigger.displayName = 'TabTrigger'; | ||
|
||
export default TabTrigger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters