| 1 |
import { Fragment } from 'react'; |
| 2 |
import { TextInput } from '@patternfly/react-core/dist/esm/components/TextInput'; |
| 3 |
import inlineStyles from '@patternfly/react-styles/css/components/InlineEdit/inline-edit'; |
| 4 |
import formStyles from '@patternfly/react-styles/css/components/Form/form'; |
| 5 |
import { css } from '@patternfly/react-styles'; |
| 6 |
import { EditableTextCellProps } from './base/types'; |
| 7 |
|
| 8 |
export interface IEditableTextCell extends React.HTMLProps<HTMLDivElement> { |
| 9 |
/** The current value of the text input */ |
| 10 |
value: string; |
| 11 |
/** Row index of this text cell */ |
| 12 |
rowIndex: number; |
| 13 |
/** Cell index of this text cell */ |
| 14 |
cellIndex: number; |
| 15 |
/** Props to build the input */ |
| 16 |
props: EditableTextCellProps; |
| 17 |
/** Event handler which fires when user changes the text in this cell */ |
| 18 |
handleTextInputChange: ( |
| 19 |
newValue: string, |
| 20 |
event: React.FormEvent<HTMLInputElement>, |
| 21 |
rowIndex: number, |
| 22 |
cellIndex: number |
| 23 |
) => void; |
| 24 |
/** accessible label of the text input */ |
| 25 |
inputAriaLabel: string; |
| 26 |
/** flag indicating if the text input is disabled */ |
| 27 |
isDisabled?: boolean; |
| 28 |
} |
| 29 |
|
| 30 |
export const EditableTextCell: React.FunctionComponent<IEditableTextCell> = ({ |
| 31 |
value, |
| 32 |
rowIndex, |
| 33 |
cellIndex, |
| 34 |
props, |
| 35 |
handleTextInputChange, |
| 36 |
inputAriaLabel, |
| 37 |
isDisabled = false |
| 38 |
}: IEditableTextCell) => ( |
| 39 |
<Fragment> |
| 40 |
<div className={inlineStyles.inlineEditValue}>{value}</div> |
| 41 |
<div className={inlineStyles.inlineEditInput}> |
| 42 |
<TextInput |
| 43 |
isDisabled={isDisabled} |
| 44 |
value={props.editableValue !== undefined ? props.editableValue : value} |
| 45 |
validated={props.isValid !== false ? 'default' : 'error'} |
| 46 |
type="text" |
| 47 |
onChange={(event, newValue) => { |
| 48 |
handleTextInputChange(newValue, event, rowIndex, cellIndex); |
| 49 |
}} |
| 50 |
aria-label={inputAriaLabel} |
| 51 |
/> |
| 52 |
<div className={css(formStyles.formHelperText)} aria-live="polite"> |
| 53 |
{props.errorText} |
| 54 |
</div> |
| 55 |
</div> |
| 56 |
</Fragment> |
| 57 |
); |
| 58 |
EditableTextCell.displayName = 'EditableTextCell'; |