// Copyright (C) The Arvados Authors. All rights reserved. // // SPDX-License-Identifier: AGPL-3.0 import React, { useEffect, useRef } from 'react'; import { Tabs, Tab, List, ListItem, StyleRulesCallback, withStyles } from '@material-ui/core'; import { WithStyles } from '@material-ui/core'; import classNames from 'classnames'; import { ArvadosTheme } from 'common/custom-theme'; type TabbedListClasses = 'root' | 'tabs' | 'list' | 'listItem' | 'notFoundLabel'; const tabbedListStyles: StyleRulesCallback = (theme: ArvadosTheme) => ({ root: { overflowY: 'auto', }, tabs: { backgroundColor: theme.palette.background.paper, position: 'sticky', top: 0, zIndex: 1, borderBottom: '1px solid lightgrey', }, list: { overflowY: 'scroll', }, listItem: { cursor: 'pointer', '&:hover': { backgroundColor: theme.palette.grey[200], } }, notFoundLabel: { cursor: 'default', padding: theme.spacing.unit, color: theme.palette.grey[700], textAlign: 'center', }, }); type TabPanelProps = { children: React.ReactNode; value: number; index: number; }; type TabbedListProps = { tabbedListContents: Record; injectedStyles?: string; selectedIndex?: number; selectedTab?: number; handleSelect?: (selection: T) => React.MouseEventHandler | undefined; renderListItem?: (item: T) => React.ReactNode; handleTabChange?: (event: React.SyntheticEvent, newValue: number) => void; }; export const TabbedList = withStyles(tabbedListStyles)(({ tabbedListContents, selectedIndex, selectedTab, injectedStyles, classes, handleSelect, renderListItem, handleTabChange }: TabbedListProps & WithStyles) => { const tabNr = selectedTab || 0; const listRefs = useRef([]); const tabLabels = Object.keys(tabbedListContents); useEffect(() => { if (selectedIndex !== undefined && listRefs.current[selectedIndex]) { listRefs.current[selectedIndex].scrollIntoView({ behavior: 'smooth', block: 'center' }); } }, [selectedIndex]); return (
{tabLabels.map((label) => ( ))}
{tabbedListContents[tabLabels[tabNr]].length === 0 &&
no matching {tabLabels[tabNr]} found
} {tabbedListContents[tabLabels[tabNr]].map((item, i) => (
{ if (!!el) listRefs.current[i] = el}}> {renderListItem ? renderListItem(item) : JSON.stringify(item)}
))}
); }); const TabPanel = ({ children, value, index }: TabPanelProps) => { return ; };