import { css, cx } from '@emotion/css'; import { Dictionary } from 'lodash'; import { GrafanaTheme2 } from '@grafana/data'; import { Checkbox, Icon, RadioButtonDot, ScrollContainer, useStyles2 } from '@grafana/ui'; import { t } from 'app/core/internationalization'; import { ScopesTree } from './ScopesTree'; import { Node, NodeReason, OnNodeSelectToggle, OnNodeUpdate, TreeScope } from './types'; export interface ScopesTreeItemProps { anyChildExpanded: boolean; groupedNodes: Dictionary; isLastExpandedNode: boolean; loadingNodeName: string | undefined; node: Node; nodePath: string[]; nodeReason: NodeReason; scopeNames: string[]; scopes: TreeScope[]; type: 'persisted' | 'result'; onNodeUpdate: OnNodeUpdate; onNodeSelectToggle: OnNodeSelectToggle; } export function ScopesTreeItem({ anyChildExpanded, groupedNodes, isLastExpandedNode, loadingNodeName, node, nodePath, nodeReason, scopeNames, scopes, type, onNodeSelectToggle, onNodeUpdate, }: ScopesTreeItemProps) { const styles = useStyles2(getStyles); const nodes = groupedNodes[nodeReason] || []; if (nodes.length === 0) { return null; } const children = (
{nodes.map((childNode) => { const isSelected = childNode.isSelectable && scopeNames.includes(childNode.linkId!); if (anyChildExpanded && !childNode.isExpanded) { return null; } const childNodePath = [...nodePath, childNode.name]; const radioName = childNodePath.join('.'); return (
{childNode.isSelectable && !childNode.isExpanded ? ( node.disableMultiSelect ? ( { onNodeSelectToggle(childNodePath); }} /> ) : ( { onNodeSelectToggle(childNodePath); }} /> ) ) : null} {childNode.isExpandable ? ( ) : ( {childNode.title} )}
{childNode.isExpanded && ( )}
); })}
); if (isLastExpandedNode) { return ( {children} ); } return children; } const getStyles = (theme: GrafanaTheme2) => { return { expandedContainer: css({ display: 'flex', flexDirection: 'column', maxHeight: '100%', }), title: css({ alignItems: 'center', display: 'flex', gap: theme.spacing(1), fontSize: theme.typography.pxToRem(14), lineHeight: theme.typography.pxToRem(22), padding: theme.spacing(0.5, 0), '& > label': css({ gap: 0, }), }), titlePadding: css({ // Fix for checkboxes and radios outline overflow due to scrollbars paddingLeft: theme.spacing(0.5), }), expand: css({ alignItems: 'center', background: 'none', border: 0, display: 'flex', gap: theme.spacing(1), margin: 0, padding: 0, }), children: css({ display: 'flex', flexDirection: 'column', overflowY: 'hidden', maxHeight: '100%', paddingLeft: theme.spacing(4), }), }; };