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, ListItemButton } from '@mui/material';
7 import { CustomStyleRulesCallback } from 'common/custom-theme';
8 import { WithStyles, withStyles } from '@mui/styles';
9 import classNames from 'classnames';
10 import { ArvadosTheme } from 'common/custom-theme';
11 import { InlinePulser } from 'components/loading/inline-pulser';
13 type TabbedListClasses = 'root' | 'tabs' | 'tabPanel' | 'listItem' | 'selected' | 'spinner' | 'notFoundLabel';
15 const tabbedListStyles: CustomStyleRulesCallback<TabbedListClasses> = (theme: ArvadosTheme) => ({
18 flexDirection: 'column',
23 backgroundColor: theme.palette.background.paper,
27 borderBottom: '1px solid lightgrey',
35 backgroundColor: theme.palette.grey[200],
39 backgroundColor: `${theme.palette.grey['300']} !important`
43 justifyContent: 'center',
49 padding: theme.spacing(1),
50 color: theme.palette.grey[700],
55 type TabPanelProps = {
56 children: React.ReactNode;
61 type TabbedListProps<T> = {
62 tabbedListContents: Record<string, T[]>;
63 injectedStyles?: string;
64 selectedIndex?: number;
66 includeContentsLength: boolean;
68 handleSelect?: (selection: T) => React.MouseEventHandler<HTMLElement> | undefined;
69 renderListItem?: (item: T) => React.ReactNode;
70 handleTabChange?: (event: React.SyntheticEvent, newValue: number) => void;
73 export const TabbedList = withStyles(tabbedListStyles)(<T,>({ tabbedListContents, selectedIndex, selectedTab, isWorking, injectedStyles, classes, handleSelect, renderListItem, handleTabChange, includeContentsLength }: TabbedListProps<T> & WithStyles<TabbedListClasses>) => {
74 const tabNr = selectedTab || 0;
75 const listRefs = useRef<HTMLDivElement[]>([]);
76 const tabLabels = Object.keys(tabbedListContents);
79 if (selectedIndex !== undefined && listRefs.current[selectedIndex]) {
80 listRefs.current[selectedIndex].scrollIntoView({ behavior: 'smooth', block: 'center' });
84 const TabPanel = ({ children, value, index }: TabPanelProps) => {
85 return <div className={classes.tabPanel} hidden={value !== index}>{value === index && children}</div>;
89 <div className={classNames(classes.root, injectedStyles)}>
90 <div className={classes.tabs}>
93 onChange={handleTabChange}
96 {tabLabels.map((label) => (
97 <Tab data-cy={`${label}-tab-label`} label={includeContentsLength ? `${label} (${tabbedListContents[label].length})` : label} />
105 {isWorking ? <div className={classes.spinner}><InlinePulser /></div> :
107 {tabbedListContents[tabLabels[tabNr]].length === 0 && <div className={classes.notFoundLabel}>no matching {tabLabels[tabNr]} found</div>}
108 {tabbedListContents[tabLabels[tabNr]].map((item, i) => (
109 <div ref={(el) => { if (!!el) listRefs.current[i] = el}}>
111 className={classNames(classes.listItem, { [classes.selected]: i === selectedIndex })}
112 selected={i === selectedIndex}
113 onClick={handleSelect && handleSelect(item)}
115 {renderListItem ? renderListItem(item) : JSON.stringify(item)}