ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

40 lines
901 B

  1. import React, { useState } from 'react'
  2. import { cn } from '@/lib/utils'
  3. type TabProps = { name: string
  4. init?: boolean
  5. children: React.ReactNode }
  6. type Props = { children: React.ReactNode }
  7. export const Tab = ({ children }: TabProps) => <>{children}</>
  8. export default ({ children }: Props) => {
  9. const tabs = React.Children.toArray (children) as React.ReactElement<TabProps>[]
  10. const [current, setCurrent] = useState<number> (() => {
  11. const i = tabs.findIndex (tab => tab.props.init)
  12. return i >= 0 ? i : 0
  13. })
  14. return (
  15. <div className="mt-4">
  16. <div className="flex gap-4">
  17. {tabs.map ((tab, i) => (
  18. <a key={i}
  19. href="#"
  20. className={cn (i === current && 'font-bold')}
  21. onClick={ev => {
  22. ev.preventDefault ()
  23. setCurrent (i)
  24. }}>
  25. {tab.props.name}
  26. </a>))}
  27. </div>
  28. <div className="mt-2">
  29. {tabs[current]}
  30. </div>
  31. </div>)
  32. }