remix-project mirror
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
remix-project/libs/remix-ui/tabs/src/lib/remix-ui-tabs.tsx

109 lines
3.9 KiB

3 years ago
import React, { useState, useRef, useEffect, useReducer } from 'react' // eslint-disable-line
3 years ago
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs'
3 years ago
import './remix-ui-tabs.css'
/* eslint-disable-next-line */
export interface TabsUIProps {
tabs: Array<any>
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({})
3 years ago
const tabsElement = useRef(null)
3 years ago
3 years ago
const tabs = useRef(props.tabs)
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' })
}
}, [selectedIndex])
3 years ago
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' : '')
3 years ago
return (
<div ref={el => { tabsRef.current[index] = el }} className={classNameTab} data-id={index === currentIndexRef.current ? 'tab-active' : ''} title={tab.tooltip}>
3 years ago
{tab.icon ? (<img className="my-1 mr-1 iconImage" src={tab.icon} />) : (<i className={classNameImg}></i>)}
<span className="title-tabs">{tab.title}</span>
3 years ago
<span className="close-tabs" onClick={(event) => { props.onClose(index); event.stopPropagation() }}>
3 years ago
<i className="text-dark fas fa-times"></i>
</span>
</div>
)
}
const active = () => {
3 years ago
if (currentIndexRef.current < 0) return ''
3 years ago
return tabs.current[currentIndexRef.current].name
3 years ago
}
const activateTab = (name: string) => {
3 years ago
const index = tabs.current.findIndex((tab) => tab.name === name)
3 years ago
currentIndexRef.current = index
3 years ago
setSelectedIndex(index)
3 years ago
}
3 years ago
3 years ago
const transformScroll = (event) => {
3 years ago
if (!event.deltaY) {
3 years ago
return
}
3 years ago
event.currentTarget.scrollLeft += event.deltaY + event.deltaX
event.preventDefault()
}
3 years ago
useEffect(() => {
props.onReady({
activateTab,
active
})
3 years ago
return () => { tabsElement.current.removeEventListener('wheel', transformScroll) }
3 years ago
}, [])
return (
<div className="remix-ui-tabs d-flex justify-content-between border-0 header nav-tabs" data-id="tabs-component">
3 years ago
<div className="d-flex flex-row" style={ { maxWidth: 'fit-content', width: '97%' } }>
<div className="d-flex flex-row justify-content-center align-items-center m-1 mt-2">
<span data-id="tabProxyZoomOut" className="btn btn-sm px-2 fas fa-search-minus text-dark" title="Zoom out" onClick={() => props.onZoomOut()}></span>
<span data-id="tabProxyZoomIn" className="btn btn-sm px-2 fas fa-search-plus text-dark" title="Zoom in" onClick={() => props.onZoomIn()}></span>
</div>
<Tabs
className="tab-scroll"
selectedIndex={selectedIndex}
3 years ago
domRef={(domEl) => {
if (tabsElement.current) return
tabsElement.current = domEl
tabsElement.current.addEventListener('wheel', transformScroll)
}}
onSelect={(index) => {
props.onSelect(index)
currentIndexRef.current = index
setSelectedIndex(index)
}}
>
<TabList className="d-flex flex-row align-items-center">
{props.tabs.map((tab, i) => <Tab className="py-1" key={tab.name}>{renderTab(tab, i)}</Tab>)}
</TabList>
{props.tabs.map((tab) => <TabPanel key={tab.name} ></TabPanel>)}
</Tabs>
3 years ago
</div>
<i className="mt-2 mr-2 fas fa-arrows-alt-h" title="Scroll to see all tabs"></i>
3 years ago
</div>
)
}
export default TabsUI