forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectColumn.tsx
More file actions
70 lines (63 loc) · 1.91 KB
/
SelectColumn.tsx
File metadata and controls
70 lines (63 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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<HTMLInputElement>) => void;
selectVariant?: RowSelectVariant;
/** text to display on the tooltip */
tooltip?: React.ReactNode;
/** other props to pass to the tooltip */
tooltipProps?: Omit<TooltipProps, 'content'>;
/** id for the input element - required by Checkbox and Radio components */
id?: string;
/** name for the input element - required by Radio component */
name?: string;
}
export const SelectColumn: React.FunctionComponent<SelectColumnProps> = ({
children = null as React.ReactNode,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
className,
onSelect = null as (event: React.FormEvent<HTMLInputElement>) => void,
selectVariant,
tooltip,
tooltipProps,
id,
name,
...props
}: SelectColumnProps) => {
const inputRef = createRef<any>();
const handleChange = (event: React.FormEvent<HTMLInputElement>, _checked: boolean) => {
onSelect && onSelect(event);
};
const commonProps = {
...props,
id,
ref: inputRef,
onChange: handleChange
};
const content = (
<Fragment>
{selectVariant === RowSelectVariant.checkbox ? (
<Checkbox {...commonProps} />
) : (
<Radio {...commonProps} name={name} />
)}
{children}
</Fragment>
);
return tooltip ? (
<Tooltip triggerRef={inputRef} content={tooltip} {...tooltipProps}>
{content}
</Tooltip>
) : (
content
);
};
SelectColumn.displayName = 'SelectColumn';