/* * Copyright (C) 2020 Red Hat, Inc. * SPDX-License-Identifier: LGPL-2.1-or-later */ import React from 'react'; import { Tab, TabTitleText, Tabs } from "@patternfly/react-core/dist/esm/components/Tabs/index.js"; import './cockpit-components-listing-panel.scss'; /* tabRenderers optional: list of tab renderers for inline expansion, array of objects with * - name tab name (has to be unique in the entry, used as react key) * - renderer react component * - data render data passed to the tab renderer */ interface TabRenderer { name: string; // eslint-disable-next-line @typescript-eslint/no-explicit-any renderer: React.ComponentType; data?: Record; } interface ListingPanelProps { tabRenderers?: TabRenderer[]; listingDetail?: React.ReactNode; initiallyActiveTab?: number; } interface ListingPanelState { activeTab: string | number; } export class ListingPanel extends React.Component { static defaultProps = { tabRenderers: [], }; constructor(props: ListingPanelProps) { super(props); this.state = { activeTab: props.initiallyActiveTab ? props.initiallyActiveTab : 0, // currently active tab in expanded mode, defaults to first tab }; this.handleTabClick = this.handleTabClick.bind(this); } handleTabClick(event: React.MouseEvent, tabIndex: number | string) { event.preventDefault(); if (this.state.activeTab !== tabIndex) { this.setState({ activeTab: tabIndex }); } } render() { let listingDetail; if ('listingDetail' in this.props) { listingDetail = ( {this.props.listingDetail} ); } return (
{listingDetail &&
{listingDetail}
} {this.props.tabRenderers?.length && {this.props.tabRenderers?.map((itm, tabIdx) => { const Renderer = itm.renderer; const rendererData = itm.data; return ( {itm.name}}>
); })}
}
); } }