import React, { useState, useRef, useEffect, useReducer } from 'react' // eslint-disable-line import { Tab, Tabs, TabList, TabPanel } from 'react-tabs' import './remix-ui-tabs.css' /* eslint-disable-next-line */ export interface TabsUIProps { tabs: Array onSelect: (index: number) => void onClose: (index: number) => void onZoomOut: () => void onZoomIn: () => void onReady: (api: any) => void } export interface TabsUIApi { activateTab: (namee: string) => void active: () => string } export const TabsUI = (props: TabsUIProps) => { const [selectedIndex, setSelectedIndex] = useState(-1) const currentIndexRef = useRef(-1) const tabsRef = useRef({}) const tabsElement = useRef(null) const tabs = useRef(props.tabs) const tabsScrollable = useRef(null) tabs.current = props.tabs // we do this to pass the tabs list to the onReady callbacks useEffect(() => { if (props.tabs[selectedIndex]) { tabsRef.current[selectedIndex].scrollIntoView({ behavior: 'smooth', block: 'center' }) console.log("usesc") } }, [selectedIndex]) const renderTab = (tab, index) => { const classNameImg = 'my-1 mr-1 text-dark ' + tab.iconClass const classNameTab = 'nav-item nav-link d-flex justify-content-center align-items-center px-2 py-1 tab' + (index === currentIndexRef.current ? ' active' : '') return (
{ props.onSelect(index); currentIndexRef.current = index; setSelectedIndex(index) }} ref={el => { tabsRef.current[index] = el }} className={classNameTab} title={tab.tooltip}> {tab.icon ? () : ()} {tab.title} { props.onClose(index); event.stopPropagation() }}>
) } const active = () => { if (currentIndexRef.current < 0) return '' return tabs.current[currentIndexRef.current].name } const activateTab = (name: string) => { const index = tabs.current.findIndex((tab) => tab.name === name) currentIndexRef.current = index setSelectedIndex(index) } function transformScroll(event) { if (!event.deltaY) { return; } event.currentTarget.scrollLeft += event.deltaY + event.deltaX; event.preventDefault(); } useEffect(() => { props.onReady({ activateTab, active }) return () => { tabsElement.current.removeEventListener('wheel', transformScroll) } }, []) return (
props.onZoomOut()}> props.onZoomIn()}>
{ if (tabsElement.current) return tabsElement.current = domEl tabsElement.current.addEventListener('wheel', transformScroll) }} > {props.tabs.map((tab, i) => {renderTab(tab, i)})} {props.tabs.map((tab) => )}
) } export default TabsUI