Debian Codegraph @

node/@patternfly/react-table/src/components/Table/EditableSelectInputCell.tsx

raw
1 import { Fragment } from 'react';
2 import { css } from '@patternfly/react-styles';
3 import { Select, SelectList } from '@patternfly/react-core/dist/esm/components/Select';
4 import { MenuToggle } from '@patternfly/react-core/dist/esm/components/MenuToggle';
5 import inlineStyles from '@patternfly/react-styles/css/components/InlineEdit/inline-edit';
6 import formStyles from '@patternfly/react-styles/css/components/Form/form';
7 import { EditableSelectInputProps } from './base/types';
8
9 export interface IEditableSelectInputCell extends Omit<
10 React.HTMLProps<HTMLElement | HTMLDivElement>,
11 'onSelect' | 'onToggle'
12 > {
13 /** Row index of this select input cell */
14 rowIndex: number;
15 /** Cell index of this select input cell */
16 cellIndex: number;
17 /** Props to build the select component */
18 props: EditableSelectInputProps;
19 /** Event handler which fires when user selects an option in this cell */
20 onSelect: (
21 event: React.MouseEvent | React.ChangeEvent,
22 newValue: any | any[],
23 rowIndex: number,
24 cellIndex: number,
25 isPlaceholder?: boolean
26 ) => void;
27 /** Options to display in the expandable select menu */
28 options?: React.ReactElement<any>[];
29 /** Flag indicating the select input is disabled */
30 isDisabled?: boolean;
31 /** Flag indicating the toggle gets placeholder styles **/
32 isPlaceholder?: boolean;
33 /** Current selected options to display as the read only value of the table cell */
34 selections?: any | any[];
35 /** Flag indicating the select menu is open */
36 isOpen?: boolean;
37 /** Event handler which fires when the select toggle is toggled */
38 onToggle?: (event: React.MouseEvent | undefined) => void;
39 /** Event handler which fires when the user clears the selections */
40 clearSelection?: (event: React.MouseEvent, rowIndex: number, cellIndex: number) => void;
41 }
42
43 export const EditableSelectInputCell: React.FunctionComponent<IEditableSelectInputCell> = ({
44 value,
45 rowIndex,
46 cellIndex,
47 onSelect = () => {},
48 clearSelection,
49 isOpen = false,
50 isPlaceholder = false,
51 onToggle = () => {},
52 selections = [''],
53 options = [] as React.ReactElement<any>[],
54 props
55 }: IEditableSelectInputCell) => {
56 const onSelectHandler = (event: React.MouseEvent | React.ChangeEvent, newValue: any | any[]) => {
57 onSelect(event, newValue, rowIndex, cellIndex, isPlaceholder);
58 };
59
60 const onClear = (event: React.MouseEvent) => {
61 clearSelection(event, rowIndex, cellIndex);
62 };
63
64 const select = (
65 <Select
66 onSelect={onSelectHandler}
67 {...(clearSelection && { onClear })}
68 isOpen={isOpen}
69 selected={selections}
70 toggle={(toggleRef: any) => (
71 <MenuToggle ref={toggleRef} onClick={onToggle} isExpanded={isOpen} isPlaceholder={isPlaceholder}>
72 {isOpen ? 'Expanded' : 'Collapsed'}
73 </MenuToggle>
74 )}
75 {...props.editableSelectProps}
76 >
77 <SelectList>{options}</SelectList>
78 </Select>
79 );
80
81 return (
82 <Fragment>
83 <div className={inlineStyles.inlineEditValue}>{Array.isArray(value) ? value.join(', ') : value}</div>
84 <div className={inlineStyles.inlineEditInput}>
85 {select}
86 <div className={css(formStyles.formHelperText)} aria-live="polite">
87 {props.errorText}
88 </div>
89 </div>
90 </Fragment>
91 );
92 };
93 EditableSelectInputCell.displayName = 'EditableSelectInputCell';