1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React, { useEffect, useRef } from 'react';
6 import { Tabs, Tab, List, ListItem, StyleRulesCallback, withStyles } from '@material-ui/core';
7 import { WithStyles } from '@material-ui/core';
8 import classNames from 'classnames';
9 import { ArvadosTheme } from 'common/custom-theme';
11 type TabbedListClasses = 'root' | 'tabs' | 'list' | 'listItem';
13 const tabbedListStyles: StyleRulesCallback<TabbedListClasses> = (theme: ArvadosTheme) => ({
18 backgroundColor: theme.palette.background.paper,
22 borderBottom: '1px solid lightgrey',
32 type TabPanelProps = {
33 children: React.ReactNode;
38 type TabbedListProps<T> = {
39 tabbedListContents: Record<string, T[]>;
40 injectedStyles?: string;
41 selectedIndex?: number;
43 renderListItem?: (item: T) => React.ReactNode;
44 handleTabChange?: (event: React.SyntheticEvent, newValue: number) => void;
47 export const TabbedList = withStyles(tabbedListStyles)(<T, _>({ tabbedListContents, selectedIndex, selectedTab, injectedStyles, classes, renderListItem, handleTabChange }: TabbedListProps<T> & WithStyles<TabbedListClasses>) => {
48 const tabNr = selectedTab || 0;
49 const listRefs = useRef<HTMLDivElement[]>([]);
50 const tabLabels = Object.keys(tabbedListContents);
53 if (selectedIndex !== undefined && listRefs.current[selectedIndex]) {
54 listRefs.current[selectedIndex].scrollIntoView({ behavior: 'smooth', block: 'center' });
59 <div className={classNames(classes.root, injectedStyles)}>
60 <div className={classes.tabs}>
63 onChange={handleTabChange}
66 {tabLabels.map((label) => (
75 <List className={classes.list}>
76 {tabbedListContents[tabLabels[tabNr]].map((item, i) => (
77 <div ref={(el) => { if (!!el) listRefs.current[i] = el}}>
79 className={classes.listItem}
80 selected={i === selectedIndex}
82 {renderListItem ? renderListItem(item) : JSON.stringify(item)}
92 const TabPanel = ({ children, value, index }: TabPanelProps) => {
93 return <div hidden={value !== index}>{value === index && children}</div>;