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
|
|
|
|
className?: string
|
|
|
|
onClick?: (tabName: string) => void
|
|
|
|
}) {
|
|
|
|
const { tabs, defaultIndex, className, onClick } = props
|
2022-03-31 06:24:35 +00:00
|
|
|
const [activeIndex, setActiveIndex] = useState(defaultIndex ?? 0)
|
|
|
|
const activeTab = tabs[activeIndex]
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
2022-04-08 21:13:10 +00:00
|
|
|
<div className="border-b border-gray-200">
|
|
|
|
<nav className="-mb-px flex space-x-8" aria-label="Tabs">
|
|
|
|
{tabs.map((tab, i) => (
|
|
|
|
<Link href={tab.href ?? '#'} key={tab.title} shallow={!!tab.href}>
|
|
|
|
<a
|
|
|
|
key={tab.title}
|
|
|
|
onClick={(e) => {
|
|
|
|
if (!tab.href) {
|
|
|
|
e.preventDefault()
|
|
|
|
}
|
|
|
|
setActiveIndex(i)
|
2022-05-05 22:30:30 +00:00
|
|
|
onClick?.(tab.title)
|
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',
|
|
|
|
className
|
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
|
|
|
|
|
|
|
<div className="mt-4">{activeTab.content}</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|