/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ import React, { useEffect, useState, KeyboardEvent } from "react"; import TabVertical from "./TabVertical"; interface Props { children: React.ReactNode; defaultActive: string; ariaLabelledById: string; } function TabsVertical(props: Props) { const [activeTab, setActiveTab] = useState(props.defaultActive); const tabsMap = new Map(); useEffect(() => { setActiveTab(props.defaultActive); }, [props.defaultActive]); function getActiveTabIndex(): number { for (let [key, value] of Array.from(tabsMap.entries())) { if (value === activeTab) { return key; } } return 0; } const onKeyDown = (e: KeyboardEvent) => { if (e.key === "ArrowDown") { const index = getActiveTabIndex(); if (index < tabsMap.size - 1) { setActiveTab(tabsMap.get(index + 1) || props.defaultActive); } } else if (e.key === "ArrowUp") { const index = getActiveTabIndex(); if (index > 0) { setActiveTab(tabsMap.get(index - 1) || props.defaultActive); } } }; const onClickTabItem = (tab: string, currentTab: HTMLElement) => { setActiveTab(tab); }; const onEnterTabItem = (tab: string, currentTab: HTMLElement) => { setActiveTab(tab); }; return (
    {React.Children.map(props.children, (child: any, index) => { tabsMap.set(index, child.props.id); return ( ); })}
{React.Children.map(props.children, (child) => { const childId = (child as any).props.id; if (childId !== activeTab) { return
; } return (
{(child as any).props.children}
); })}
); } export default TabsVertical;