({
children,
className = {},
controlledActive,
onActiveChange,
renderTab,
shouldFocusTabOnChange = false,
shouldMountInactive = false,
showBorder = true,
showHeader = true,
style,
shallow = false,
swipeable = false,
tabListProps = {},
tabTag,
extraHeaderContent,
}: TabContainerProps<T>)
| 68 | } |
| 69 | |
| 70 | export function TabContainer<T extends string = string>({ |
| 71 | children, |
| 72 | className = {}, |
| 73 | controlledActive, |
| 74 | onActiveChange, |
| 75 | renderTab, |
| 76 | shouldFocusTabOnChange = false, |
| 77 | shouldMountInactive = false, |
| 78 | showBorder = true, |
| 79 | showHeader = true, |
| 80 | style, |
| 81 | shallow = false, |
| 82 | swipeable = false, |
| 83 | tabListProps = {}, |
| 84 | tabTag, |
| 85 | extraHeaderContent, |
| 86 | }: TabContainerProps<T>): ReactElement { |
| 87 | const router = useRouter(); |
| 88 | const containerRef = useRef<HTMLDivElement>(null); |
| 89 | const tabs = useMemo(() => (children ?? []).filter(Boolean), [children]); |
| 90 | const currentPath = getRouterPathname(router.asPath || router.pathname); |
| 91 | |
| 92 | const [active, setActive] = useState(() => { |
| 93 | if (!tabs.length) { |
| 94 | return '' as T; |
| 95 | } |
| 96 | |
| 97 | const defaultLabel = tabs[0].props.label; |
| 98 | |
| 99 | if (tabs[0].props.url) { |
| 100 | const matchingChild = tabs.find((c) => c.props.url === currentPath); |
| 101 | return matchingChild ? matchingChild.props.label : defaultLabel; |
| 102 | } |
| 103 | |
| 104 | return defaultLabel; |
| 105 | }); |
| 106 | |
| 107 | const currentActive = controlledActive ?? active; |
| 108 | |
| 109 | const navigateToUrl = useCallback( |
| 110 | (url: string) => { |
| 111 | router.push(url, undefined, { shallow }); |
| 112 | }, |
| 113 | [router, shallow], |
| 114 | ); |
| 115 | |
| 116 | const onClick: TabListProps['onClick'] = (label, event) => { |
| 117 | const child = tabs.find((c) => c.props.label === label); |
| 118 | setActive(label as T); |
| 119 | const shouldChange = onActiveChange?.(label as T, event); |
| 120 | |
| 121 | // evaluate !== false due to backwards compatibility with implementations that return undefined |
| 122 | if (shouldChange !== false) { |
| 123 | setTimeout(() => { |
| 124 | if (shouldFocusTabOnChange && containerRef?.current) { |
| 125 | const [firstChild] = containerRef.current.children; |
| 126 | if (firstChild instanceof HTMLElement) { |
| 127 | firstChild.focus(); |
nothing calls this directly
no test coverage detected