import React from "react"; /** * Product tabs — pill-list style from --tab-* tokens. * WAI-ARIA tabs pattern: role="tablist" with roving tabindex, ArrowLeft/Right * (or Up/Down when vertical), Home/End. Wire `panelId` per tab when the panel * lives outside this component. * R03: 선택 상태와 키보드 진입점을 분리. 선택 항목이 disabled면 첫 사용 가능 항목이 진입점. */ export function Tabs({ tabs = [], value, defaultValue, onChange, ariaLabel = "탭", orientation = "horizontal", style }) { const norm = tabs.map((t) => (typeof t === "string" ? { value: t, label: t } : t)); const [internal, setInternal] = React.useState(defaultValue ?? norm[0]?.value); const active = value !== undefined ? value : internal; const refs = React.useRef([]); const pickable = norm.map((t, i) => (t.disabled ? -1 : i)).filter((i) => i >= 0); const activeIndex = norm.findIndex((t) => t.value === active); const entryIndex = pickable.includes(activeIndex) ? activeIndex : pickable[0] ?? -1; const pick = (v) => { if (value === undefined) setInternal(v); if (onChange) onChange(v); }; const move = (from, dir) => { if (!pickable.length) return; const at = pickable.indexOf(from); const next = at === -1 ? pickable[0] : pickable[(at + dir + pickable.length) % pickable.length]; pick(norm[next].value); refs.current[next]?.focus(); }; const nextKeys = orientation === "vertical" ? ["ArrowDown"] : ["ArrowRight"]; const prevKeys = orientation === "vertical" ? ["ArrowUp"] : ["ArrowLeft"]; const onKeyDown = (e, i) => { if (!pickable.length) return; if (nextKeys.includes(e.key)) { e.preventDefault(); move(i, 1); } else if (prevKeys.includes(e.key)) { e.preventDefault(); move(i, -1); } else if (e.key === "Home") { e.preventDefault(); pick(norm[pickable[0]].value); refs.current[pickable[0]]?.focus(); } else if (e.key === "End") { e.preventDefault(); const l = pickable[pickable.length - 1]; pick(norm[l].value); refs.current[l]?.focus(); } }; return (
{norm.map((tab, i) => { const isActive = tab.value === active; return ( ); })}
); }