forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditableSelectInputCell.tsx
More file actions
91 lines (86 loc) · 3.26 KB
/
EditableSelectInputCell.tsx
File metadata and controls
91 lines (86 loc) · 3.26 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { Fragment } from 'react';
import { css } from '@patternfly/react-styles';
import { Select, SelectList } from '@patternfly/react-core/dist/esm/components/Select';
import { MenuToggle } from '@patternfly/react-core/dist/esm/components/MenuToggle';
import inlineStyles from '@patternfly/react-styles/css/components/InlineEdit/inline-edit';
import formStyles from '@patternfly/react-styles/css/components/Form/form';
import { EditableSelectInputProps } from './base/types';
export interface IEditableSelectInputCell
extends Omit<React.HTMLProps<HTMLElement | HTMLDivElement>, 'onSelect' | 'onToggle'> {
/** Row index of this select input cell */
rowIndex: number;
/** Cell index of this select input cell */
cellIndex: number;
/** Props to build the select component */
props: EditableSelectInputProps;
/** Event handler which fires when user selects an option in this cell */
onSelect: (
event: React.MouseEvent | React.ChangeEvent,
newValue: any | any[],
rowIndex: number,
cellIndex: number,
isPlaceholder?: boolean
) => void;
/** Options to display in the expandable select menu */
options?: React.ReactElement<any>[];
/** Flag indicating the select input is disabled */
isDisabled?: boolean;
/** Flag indicating the toggle gets placeholder styles **/
isPlaceholder?: boolean;
/** Current selected options to display as the read only value of the table cell */
selections?: any | any[];
/** Flag indicating the select menu is open */
isOpen?: boolean;
/** Event handler which fires when the select toggle is toggled */
onToggle?: (event: React.MouseEvent | undefined) => void;
/** Event handler which fires when the user clears the selections */
clearSelection?: (event: React.MouseEvent, rowIndex: number, cellIndex: number) => void;
}
export const EditableSelectInputCell: React.FunctionComponent<IEditableSelectInputCell> = ({
value,
rowIndex,
cellIndex,
onSelect = () => {},
clearSelection,
isOpen = false,
isPlaceholder = false,
onToggle = () => {},
selections = [''],
options = [] as React.ReactElement<any>[],
props
}: IEditableSelectInputCell) => {
const onSelectHandler = (event: React.MouseEvent | React.ChangeEvent, newValue: any | any[]) => {
onSelect(event, newValue, rowIndex, cellIndex, isPlaceholder);
};
const onClear = (event: React.MouseEvent) => {
clearSelection(event, rowIndex, cellIndex);
};
const select = (
<Select
onSelect={onSelectHandler}
{...(clearSelection && { onClear })}
isOpen={isOpen}
selected={selections}
toggle={(toggleRef: any) => (
<MenuToggle ref={toggleRef} onClick={onToggle} isExpanded={isOpen} isPlaceholder={isPlaceholder}>
{isOpen ? 'Expanded' : 'Collapsed'}
</MenuToggle>
)}
{...props.editableSelectProps}
>
<SelectList>{options}</SelectList>
</Select>
);
return (
<Fragment>
<div className={inlineStyles.inlineEditValue}>{Array.isArray(value) ? value.join(', ') : value}</div>
<div className={inlineStyles.inlineEditInput}>
{select}
<div className={css(formStyles.formHelperText)} aria-live="polite">
{props.errorText}
</div>
</div>
</Fragment>
);
};
EditableSelectInputCell.displayName = 'EditableSelectInputCell';