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';
12 type TabbedListClasses = 'root' | 'tabs' | 'list' | 'listItem' | 'selected' | 'notFoundLabel';
14 const tabbedListStyles: CustomStyleRulesCallback<TabbedListClasses> = (theme: ArvadosTheme) => ({
19 backgroundColor: theme.palette.background.paper,
23 borderBottom: '1px solid lightgrey',
31 backgroundColor: theme.palette.grey[200],
35 backgroundColor: `${theme.palette.grey['300']} !important`
39 padding: theme.spacing(1),
40 color: theme.palette.grey[700],
45 type TabPanelProps = {
46 children: React.ReactNode;
51 type TabbedListProps<T> = {
52 tabbedListContents: Record<string, T[]>;
53 injectedStyles?: string;
54 selectedIndex?: number;
56 includeContentsLength: boolean;
57 handleSelect?: (selection: T) => React.MouseEventHandler<HTMLElement> | undefined;
58 renderListItem?: (item: T) => React.ReactNode;
59 handleTabChange?: (event: React.SyntheticEvent, newValue: number) => void;
62 export const TabbedList = withStyles(tabbedListStyles)(<T,>({ tabbedListContents, selectedIndex, selectedTab, injectedStyles, classes, handleSelect, renderListItem, handleTabChange, includeContentsLength }: TabbedListProps<T> & WithStyles<TabbedListClasses>) => {
63 const tabNr = selectedTab || 0;
64 const listRefs = useRef<HTMLDivElement[]>([]);
65 const tabLabels = Object.keys(tabbedListContents);
68 if (selectedIndex !== undefined && listRefs.current[selectedIndex]) {
69 listRefs.current[selectedIndex].scrollIntoView({ behavior: 'smooth', block: 'center' });
74 <div className={classNames(classes.root, injectedStyles)}>
75 <div className={classes.tabs}>
78 onChange={handleTabChange}
81 {tabLabels.map((label) => (
82 <Tab data-cy={`${label}-tab-label`} label={includeContentsLength ? `${label} (${tabbedListContents[label].length})` : label} />
90 <List className={classes.list}>
91 {tabbedListContents[tabLabels[tabNr]].length === 0 && <div className={classes.notFoundLabel}>no matching {tabLabels[tabNr]} found</div>}
92 {tabbedListContents[tabLabels[tabNr]].map((item, i) => (
93 <div ref={(el) => { if (!!el) listRefs.current[i] = el}}>
95 className={classNames(classes.listItem, { [classes.selected]: i === selectedIndex })}
96 selected={i === selectedIndex}
97 onClick={handleSelect && handleSelect(item)}
99 {renderListItem ? renderListItem(item) : JSON.stringify(item)}
109 const TabPanel = ({ children, value, index }: TabPanelProps) => {
110 return <div hidden={value !== index}>{value === index && children}</div>;