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' | 'listItem' | 'selected' | 'spinner' | 'notFoundLabel';
15 const tabbedListStyles: CustomStyleRulesCallback<TabbedListClasses> = (theme: ArvadosTheme) => ({
18 flexDirection: 'column',
22 backgroundColor: theme.palette.background.paper,
26 borderBottom: '1px solid lightgrey',
32 backgroundColor: theme.palette.grey[200],
36 backgroundColor: `${theme.palette.grey['300']} !important`
40 justifyContent: 'center',
46 padding: theme.spacing(1),
47 color: theme.palette.grey[700],
52 type TabPanelProps = {
53 children: React.ReactNode;
58 type TabbedListProps<T> = {
59 tabbedListContents: Record<string, T[]>;
60 injectedStyles?: string;
61 selectedIndex?: number;
63 includeContentsLength: boolean;
65 handleSelect?: (selection: T) => React.MouseEventHandler<HTMLElement> | undefined;
66 renderListItem?: (item: T) => React.ReactNode;
67 handleTabChange?: (event: React.SyntheticEvent, newValue: number) => void;
70 export const TabbedList = withStyles(tabbedListStyles)(<T,>({ tabbedListContents, selectedIndex = 0, selectedTab = 0, isWorking, injectedStyles, classes, handleSelect, renderListItem, handleTabChange, includeContentsLength }: TabbedListProps<T> & WithStyles<TabbedListClasses>) => {
71 const tabLabels = Object.keys(tabbedListContents);
72 const selectedTabLabel = tabLabels[selectedTab];
73 const listContents = tabbedListContents[selectedTabLabel] || [];
75 const TabPanel = ({ children, value, index }: TabPanelProps) => {
76 return <div hidden={value !== index}>{value === index && children}</div>;
80 <div className={classNames(classes.root, injectedStyles)}>
82 className={classes.tabs}
84 onChange={handleTabChange}
87 {tabLabels.map((label) => (
88 <Tab key={label} data-cy={`${label}-tab-label`} label={includeContentsLength ? `${label} (${tabbedListContents[label].length})` : label} />
95 {isWorking ? <div className={classes.spinner}><InlinePulser /></div> :
97 {listContents.length === 0 && <div className={classes.notFoundLabel}>no matching {tabLabels[selectedTab]} found</div>}
98 {listContents.map((item, i) => (
99 <div key={`${selectedTabLabel}-${i}`}>
101 className={classNames(classes.listItem, { [classes.selected]: i === selectedIndex })}
102 selected={i === selectedIndex}
103 onClick={handleSelect && handleSelect(item)}
105 {renderListItem ? renderListItem(item) : JSON.stringify(item)}