import { cx } from '@emotion/css'; import React, { useCallback, useEffect } from 'react'; import { Checkbox, IconButton, useStyles2, useTheme2 } from '@grafana/ui'; import { Space } from '../Space'; import { EntryIcon } from './EntryIcon'; import getStyles from './styles'; import { ResourceRow } from './types'; interface NestedEntryProps { level: number; entry: ResourceRow; isSelected: boolean; isSelectable: boolean; isOpen: boolean; isDisabled: boolean; scrollIntoView?: boolean; onToggleCollapse: (row: ResourceRow) => void; onSelectedChange: (row: ResourceRow, selected: boolean) => void; } export const NestedEntry: React.FC = ({ entry, isSelected, isDisabled, isOpen, isSelectable, level, scrollIntoView, onToggleCollapse, onSelectedChange, }) => { const theme = useTheme2(); const styles = useStyles2(getStyles); const hasChildren = !!entry.children; const handleToggleCollapse = useCallback(() => { onToggleCollapse(entry); }, [onToggleCollapse, entry]); const handleSelectedChanged = useCallback( (ev: React.ChangeEvent) => { const isSelected = ev.target.checked; onSelectedChange(entry, isSelected); }, [entry, onSelectedChange] ); const checkboxId = `${scrollIntoView ? 'table' : 'summary'}_checkbox_${entry.uri}`; // Scroll to the selected element if it's not in the view // Only do it once, when the component is mounted useEffect(() => { if (isSelected && scrollIntoView) { document.getElementById(checkboxId)?.scrollIntoView({ behavior: 'smooth', block: 'center', }); } }, []); // eslint-disable-line react-hooks/exhaustive-deps return (
{hasChildren ? ( ) : ( )} {isSelectable && ( <> )}
); };