import { createRef, Fragment } from 'react'; import { Tooltip, TooltipProps } from '@patternfly/react-core/dist/esm/components/Tooltip'; import { Checkbox } from '@patternfly/react-core/dist/esm/components/Checkbox'; import { Radio } from '@patternfly/react-core/dist/esm/components/Radio'; export enum RowSelectVariant { radio = 'radio', checkbox = 'checkbox' } export interface SelectColumnProps { children?: React.ReactNode; className?: string; onSelect?: (event: React.FormEvent) => void; selectVariant?: RowSelectVariant; /** text to display on the tooltip */ tooltip?: React.ReactNode; /** other props to pass to the tooltip */ tooltipProps?: Omit; /** id for the input element - required by Checkbox and Radio components */ id?: string; /** name for the input element - required by Radio component */ name?: string; /** Whether the checkbox should be in an indeterminate state */ isIndeterminate?: boolean; } export const SelectColumn: React.FunctionComponent = ({ children = null as React.ReactNode, // eslint-disable-next-line @typescript-eslint/no-unused-vars className, onSelect = null as (event: React.FormEvent) => void, selectVariant, tooltip, tooltipProps, id, name, isIndeterminate, ...props }: SelectColumnProps) => { const inputRef = createRef(); const handleChange = (event: React.FormEvent, _checked: boolean) => { onSelect && onSelect(event); }; const commonProps = { ...props, id, ref: inputRef, onChange: handleChange }; const checkboxProps = { ...commonProps, ...(isIndeterminate && { isChecked: null }) }; const content = ( {selectVariant === RowSelectVariant.checkbox ? ( ) : ( )} {children} ); return tooltip ? ( {content} ) : ( content ); }; SelectColumn.displayName = 'SelectColumn';