mirror of https://github.com/portainer/portainer
bring across widget tabs from EE
parent
659402c3ec
commit
a7b6ccf91f
|
@ -0,0 +1,66 @@
|
||||||
|
import { RawParams } from '@uirouter/react';
|
||||||
|
import clsx from 'clsx';
|
||||||
|
import { ReactNode } from 'react';
|
||||||
|
|
||||||
|
import { Icon } from '@@/Icon';
|
||||||
|
import { Link } from '@@/Link';
|
||||||
|
|
||||||
|
export interface Tab {
|
||||||
|
name: string;
|
||||||
|
icon: ReactNode;
|
||||||
|
widget: ReactNode;
|
||||||
|
selectedTabParam: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
currentTabIndex: number;
|
||||||
|
tabs: Tab[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function WidgetTabs({ currentTabIndex, tabs }: Props) {
|
||||||
|
// ensure that the selectedTab param is always valid
|
||||||
|
const invalidQueryParamValue = tabs.every(
|
||||||
|
(tab) => encodeURIComponent(tab.selectedTabParam) !== tab.selectedTabParam
|
||||||
|
);
|
||||||
|
|
||||||
|
if (invalidQueryParamValue) {
|
||||||
|
throw new Error('Invalid query param value for tab');
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="row">
|
||||||
|
<div className="col-sm-12 !mb-0">
|
||||||
|
<div className="pl-2">
|
||||||
|
{tabs.map(({ name, icon }, index) => (
|
||||||
|
<Link
|
||||||
|
to="."
|
||||||
|
params={{ 'selected-tab': tabs[index].selectedTabParam }}
|
||||||
|
key={index}
|
||||||
|
className={clsx(
|
||||||
|
'inline-flex items-center gap-2 border-0 border-b-2 border-solid bg-transparent px-4 py-2',
|
||||||
|
currentTabIndex === index
|
||||||
|
? 'border-blue-8 text-blue-8 th-highcontrast:border-blue-6 th-highcontrast:text-blue-6 th-dark:border-blue-6 th-dark:text-blue-6'
|
||||||
|
: 'border-transparent'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<Icon icon={icon} />
|
||||||
|
{name}
|
||||||
|
</Link>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// findSelectedTabIndex returns the index of the selected-tab, or 0 if none is selected
|
||||||
|
export function findSelectedTabIndex(
|
||||||
|
{ params }: { params: RawParams },
|
||||||
|
tabs: Tab[]
|
||||||
|
) {
|
||||||
|
const selectedTabParam = params['selected-tab'] || tabs[0].selectedTabParam;
|
||||||
|
const currentTabIndex = tabs.findIndex(
|
||||||
|
(tab) => tab.selectedTabParam === selectedTabParam
|
||||||
|
);
|
||||||
|
return currentTabIndex || 0;
|
||||||
|
}
|
|
@ -4,11 +4,13 @@ import { WidgetFooter } from './WidgetFooter';
|
||||||
import { WidgetTitle } from './WidgetTitle';
|
import { WidgetTitle } from './WidgetTitle';
|
||||||
import { WidgetTaskbar } from './WidgetTaskbar';
|
import { WidgetTaskbar } from './WidgetTaskbar';
|
||||||
import { Loading } from './Loading';
|
import { Loading } from './Loading';
|
||||||
|
import { WidgetTabs } from './WidgetTabs';
|
||||||
|
|
||||||
interface WithSubcomponents {
|
interface WithSubcomponents {
|
||||||
Body: typeof WidgetBody;
|
Body: typeof WidgetBody;
|
||||||
Footer: typeof WidgetFooter;
|
Footer: typeof WidgetFooter;
|
||||||
Title: typeof WidgetTitle;
|
Title: typeof WidgetTitle;
|
||||||
|
Tabs: typeof WidgetTabs;
|
||||||
Taskbar: typeof WidgetTaskbar;
|
Taskbar: typeof WidgetTaskbar;
|
||||||
Loading: typeof Loading;
|
Loading: typeof Loading;
|
||||||
}
|
}
|
||||||
|
@ -18,6 +20,7 @@ const Widget = MainComponent as typeof MainComponent & WithSubcomponents;
|
||||||
Widget.Body = WidgetBody;
|
Widget.Body = WidgetBody;
|
||||||
Widget.Footer = WidgetFooter;
|
Widget.Footer = WidgetFooter;
|
||||||
Widget.Title = WidgetTitle;
|
Widget.Title = WidgetTitle;
|
||||||
|
Widget.Tabs = WidgetTabs;
|
||||||
Widget.Taskbar = WidgetTaskbar;
|
Widget.Taskbar = WidgetTaskbar;
|
||||||
Widget.Loading = Loading;
|
Widget.Loading = Loading;
|
||||||
|
|
||||||
|
@ -26,6 +29,7 @@ export {
|
||||||
WidgetBody,
|
WidgetBody,
|
||||||
WidgetFooter,
|
WidgetFooter,
|
||||||
WidgetTitle,
|
WidgetTitle,
|
||||||
|
WidgetTabs,
|
||||||
WidgetTaskbar,
|
WidgetTaskbar,
|
||||||
Loading,
|
Loading,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue