import React from "react"; import classNames from "classnames"; import Color from "color"; import { Divider, Icon, IconButton, OverflowText, Section, SectionHeader, Spacing, StickyTarget, StickyTargetProps, Toolbar, ToolbarSection, Tooltip, } from "../../components"; import { TestableComponent } from "../../components/interfaces"; import { CLASSPREFIX as eccgui } from "../../configuration/constants"; export interface ContentGroupProps extends Omit, "title">, TestableComponent { /** * Title of the content group. */ title?: string; /** * Level of the content group. */ level?: number; /** * Context information to display in the header. */ contextInfo?: React.ReactElement | React.ReactElement[]; /** * Annotation to display in the content. */ annotation?: React.ReactElement | React.ReactElement[]; /** * Action options to display in the header. */ actionOptions?: React.ReactElement | React.ReactElement[]; /** * Flag to collapse the content group. */ isCollapsed?: boolean; /** * Text to display when the callapse button is hovered. * If not set then it uses "Show more" or "Show less". */ textToggleCollapse?: string; /** * Event handler to toggle the collapse state. */ handlerToggleCollapse?: () => void; /** * Use a border on the left side to visually connect the whole content content group. */ borderMainConnection?: boolean; /** * Use a border on the left side to visually emphase the content group. * If it is set to an array of color codes then the border is multi colored. */ borderSubConnection?: boolean | string[]; /** * Whitespace size between header and the content. */ whitespaceSize?: "tiny" | "small" | "medium" | "large" | "xlarge"; /** * Title minimum headline level. */ minimumHeadlineLevel?: 1 | 2 | 3 | 4 | 5 | 6; /** * Props to pass to `StickyTarget`. */ stickyHeaderProps?: Omit; /** * Description of the content group. * Added as tooltip to an info icon placed in the content group header. */ description?: string; /** * Flag to hide the group divider. */ hideGroupDivider?: boolean; /** * Additional props to pass to the content container. */ contentProps?: Omit, "children">; } /** * Manage display of a grouped content section. * Add info, actions and context annotations by using its properties. * Can be nested into each other. */ export const ContentGroup = ({ children, className = "", title, contextInfo, annotation, actionOptions, isCollapsed = false, textToggleCollapse, handlerToggleCollapse, borderMainConnection = false, borderSubConnection = false, level = 1, minimumHeadlineLevel = 3, whitespaceSize = "small", style, stickyHeaderProps, description, hideGroupDivider, contentProps, ...otherContentWrapperProps }: ContentGroupProps) => { const displayHeader = title || handlerToggleCollapse; let borderGradient: string[] | undefined = undefined; if (typeof borderSubConnection === "object") { const borderColors: string[] = Array.isArray(borderSubConnection) ? borderSubConnection : [borderSubConnection]; borderGradient = borderColors.reduce((acc: string[], borderColor: string, index: number): string[] => { try { const color = Color(borderColor); acc.push( `${color.rgb().toString()} ` + `${(index / borderColors.length) * 100}% ` + `${((index + 1) / borderColors.length) * 100}%`, ); } catch { // eslint-disable-next-line no-console console.warn("Received invalid background color for tag: " + borderColor); } return acc; }, []); } const contextInfoElements = Array.isArray(contextInfo) ? contextInfo : [contextInfo]; const { className: contentClassName, ...otherContentProps } = contentProps ?? {}; const headerContent = displayHeader ? ( <> {handlerToggleCollapse && ( )} {title && ( {React.createElement( "h" + Math.min( Math.max(minimumHeadlineLevel, level + minimumHeadlineLevel), 6, ).toString(), { children: {title}, className: `${eccgui}-contentgroup__header__title`, }, )} {description && ( <> )} )} {contextInfoElements && contextInfoElements[0]?.props && Object.values(contextInfoElements[0].props).every((v) => v !== undefined) && (
{contextInfoElements}
)} {(!isCollapsed || !handlerToggleCollapse) && actionOptions && ( {actionOptions} )}
{(!isCollapsed || !handlerToggleCollapse) && ( <> {!hideGroupDivider && } )} ) : ( <> ); return (
{headerContent && stickyHeaderProps ? ( {headerContent} ) : ( headerContent )} {(!isCollapsed || !handlerToggleCollapse) && ( <>
{children}
{contextInfo && !displayHeader && (
{contextInfoElements}
)} {annotation &&
{annotation}
} {actionOptions && !displayHeader && (
{actionOptions}
)}
)}
); };