2022-03-31 06:24:35 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import Link from 'next/link'
|
2022-05-26 21:41:24 +00:00
|
|
|
import { ReactNode, useState } from 'react'
|
2022-05-05 22:30:30 +00:00
|
|
|
import { Row } from './row'
|
2022-03-31 06:24:35 +00:00
|
|
|
|
|
|
|
type Tab = {
|
|
|
|
title: string
|
2022-05-26 21:41:24 +00:00
|
|
|
tabIcon?: ReactNode
|
|
|
|
content: ReactNode
|
2022-03-31 06:24:35 +00:00
|
|
|
// If set, change the url to this href when the tab is selected
|
|
|
|
href?: string
|
|
|
|
}
|
|
|
|
|
2022-05-05 22:30:30 +00:00
|
|
|
export function Tabs(props: {
|
|
|
|
tabs: Tab[]
|
|
|
|
defaultIndex?: number
|
2022-07-01 22:37:30 +00:00
|
|
|
labelClassName?: string
|
2022-06-06 19:53:02 +00:00
|
|
|
onClick?: (tabTitle: string, index: number) => void
|
2022-05-05 22:30:30 +00:00
|
|
|
}) {
|
2022-07-01 22:37:30 +00:00
|
|
|
const { tabs, defaultIndex, labelClassName, onClick } = props
|
2022-03-31 06:24:35 +00:00
|
|
|
const [activeIndex, setActiveIndex] = useState(defaultIndex ?? 0)
|
2022-06-16 16:29:04 +00:00
|
|
|
const activeTab = tabs[activeIndex] as Tab | undefined // can be undefined in weird case
|
2022-03-31 06:24:35 +00:00
|
|
|
|
|
|
|
return (
|
2022-07-01 22:37:30 +00:00
|
|
|
<>
|
2022-04-08 21:13:10 +00:00
|
|
|
<div className="border-b border-gray-200">
|
2022-07-01 22:37:30 +00:00
|
|
|
<nav className="-mb-px mb-4 flex space-x-8" aria-label="Tabs">
|
2022-04-08 21:13:10 +00:00
|
|
|
{tabs.map((tab, i) => (
|
|
|
|
<Link href={tab.href ?? '#'} key={tab.title} shallow={!!tab.href}>
|
|
|
|
<a
|
2022-06-23 08:07:52 +00:00
|
|
|
id={`tab-${i}`}
|
2022-04-08 21:13:10 +00:00
|
|
|
key={tab.title}
|
|
|
|
onClick={(e) => {
|
|
|
|
if (!tab.href) {
|
|
|
|
e.preventDefault()
|
|
|
|
}
|
|
|
|
setActiveIndex(i)
|
2022-06-06 19:53:02 +00:00
|
|
|
onClick?.(tab.title, i)
|
2022-04-08 21:13:10 +00:00
|
|
|
}}
|
|
|
|
className={clsx(
|
|
|
|
activeIndex === i
|
|
|
|
? 'border-indigo-500 text-indigo-600'
|
|
|
|
: 'border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700',
|
2022-05-05 22:30:30 +00:00
|
|
|
'cursor-pointer whitespace-nowrap border-b-2 py-3 px-1 text-sm font-medium',
|
2022-07-01 22:37:30 +00:00
|
|
|
labelClassName
|
2022-04-08 21:13:10 +00:00
|
|
|
)}
|
|
|
|
aria-current={activeIndex === i ? 'page' : undefined}
|
|
|
|
>
|
2022-05-05 22:30:30 +00:00
|
|
|
<Row className={'items-center justify-center gap-1'}>
|
|
|
|
{tab.tabIcon && <span> {tab.tabIcon}</span>}
|
|
|
|
{tab.title}
|
|
|
|
</Row>
|
2022-04-08 21:13:10 +00:00
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
))}
|
|
|
|
</nav>
|
|
|
|
</div>
|
2022-03-31 06:24:35 +00:00
|
|
|
|
2022-07-01 22:37:30 +00:00
|
|
|
{activeTab?.content}
|
|
|
|
</>
|
2022-03-31 06:24:35 +00:00
|
|
|
)
|
|
|
|
}
|