'use client' import { forwardRef, type KeyboardEvent, type MouseEvent, type ReactNode } from 'react' import { PktIcon } from '../icon/Icon' import { type IPktTag, PktTag } from '../tag/Tag' import { useTabsContext } from './Tabs' export type TSkin = IPktTag['skin'] export interface IPktTabItem { children: ReactNode active?: boolean disabled?: boolean href?: string onClick?: (event: MouseEvent) => void icon?: string controls?: string tag?: string tagSkin?: TSkin index?: number } export const PktTabItem = forwardRef( ({ children, active, disabled = false, href, onClick, icon, controls, tag, tagSkin, index = 0 }, ref) => { const { arrowNav, registerTabRef, handleKeyPress, selectTab } = useTabsContext() const isActive = !!active && !disabled const handleClick = (event: MouseEvent) => { if (disabled) { event.preventDefault() event.stopPropagation() return } selectTab(index) onClick?.(event) } const handleKeyDown = (event: KeyboardEvent) => { if (disabled && (event.key === 'Enter' || event.key === ' ' || event.key === 'Spacebar')) { event.preventDefault() event.stopPropagation() return } handleKeyPress(index, event) } const commonProps = { 'aria-selected': arrowNav ? isActive : undefined, 'aria-controls': controls, role: arrowNav ? 'tab' : undefined, onKeyDown: handleKeyDown, onClick: handleClick, tabIndex: disabled ? -1 : isActive || !arrowNav ? undefined : -1, ref: (el: HTMLAnchorElement | HTMLButtonElement | null) => { registerTabRef(index, el, disabled) if (typeof ref === 'function') { ref(el) } else if (ref) { ref.current = el } }, } const content = ( <> {icon && } {children} {tag && ( {tag} )} ) if (href) { return ( {content} ) } return ( ) }, ) PktTabItem.displayName = 'PktTabItem'