This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathPropertyPlaceholderRow.tsx
92 lines (86 loc) · 2.65 KB
/
PropertyPlaceholderRow.tsx
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
92
import './MetadataEditorModal.css';
import { HelperText, HelperTextItem, TextInput } from '@patternfly/react-core';
import '@patternfly/react-styles/css/components/Table/table';
import { Td, TdProps, TreeRowWrapper } from '@patternfly/react-table';
import { FormEvent, useState } from 'react';
type PropertyPlaceholderRowProps = {
propertyName: string;
path: string[];
parentModel: any;
rowIndex: number;
level: number;
posinset: number;
isObject: boolean;
onChangeModel: () => void;
};
/**
* Represents a row in the {@link PropertiesField} table.
* @param propertyName
* @param path
* @param parentModel
* @param treeRow
* @param isObject
* @param onChangeModel
* @constructor
*/
export function PropertyPlaceholderRow({
propertyName,
path,
parentModel,
rowIndex,
level,
posinset,
isObject,
onChangeModel,
}: PropertyPlaceholderRowProps) {
const [userInputName, setUserInputName] = useState<string>('');
const [isUserInputNameDuplicate, setUserInputNameDuplicate] = useState<boolean>(false);
const treeRow: TdProps['treeRow'] = {
rowIndex,
onCollapse: () => {},
props: {
isRowSelected: true,
isExpanded: false,
isHidden: false,
'aria-level': level,
'aria-posinset': posinset,
'aria-setsize': 0,
},
};
function handleUserInputName(name: string, event: FormEvent<HTMLInputElement>) {
event.stopPropagation();
setUserInputName(name);
setUserInputNameDuplicate(!!(name && parentModel[name] != null));
}
function commitUserInputName() {
if (!!userInputName && !isUserInputNameDuplicate) {
parentModel[userInputName] = isObject ? {} : '';
}
onChangeModel();
}
return (
<TreeRowWrapper row={{ props: treeRow!.props }} className="pf-m-selected propertyRow">
<Td dataLabel="NAME" treeRow={treeRow}>
<TextInput
autoFocus={true}
aria-label={`${propertyName}-${path.join('-')}-placeholder-name`}
data-testid={`${propertyName}-${path.join('-')}-placeholder-name-input`}
name={`properties-input-${propertyName}-${path.join('-')}-name`}
type="text"
aria-invalid={isUserInputNameDuplicate}
value={userInputName}
onKeyDown={(event) => event.stopPropagation()}
onChange={(value, event) => handleUserInputName(value, event)}
onBlur={commitUserInputName}
/>
{isUserInputNameDuplicate && (
<HelperText>
<HelperTextItem variant="error">Please specify a unique property name</HelperTextItem>
</HelperText>
)}
</Td>
<Td dataLabel="VALUE"></Td>
<Td isActionCell></Td>
</TreeRowWrapper>
);
}