forked from opendatahub-io/odh-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathFieldList.tsx
More file actions
70 lines (62 loc) · 2.02 KB
/
FieldList.tsx
File metadata and controls
70 lines (62 loc) · 2.02 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 * as React from 'react';
import { Stack, StackItem, FormGroup, TextInput } from '@patternfly/react-core';
import { EnvVariableDataEntry } from '#~/pages/projects/types';
import PasswordInput from '#~/components/PasswordInput';
import { trimInputOnBlur, trimInputOnPaste } from '#~/utilities/trimInput';
export type FieldOptions = {
key: string;
label: string;
placeholder?: string;
isRequired?: boolean;
isPassword?: boolean;
};
type FieldListProps = {
values: EnvVariableDataEntry[];
onUpdate: (data: EnvVariableDataEntry[]) => void;
fields: FieldOptions[];
};
type InputFieldProps = {
options: FieldOptions;
onChange: (key: FieldOptions['key'], value: string) => void;
value: string;
};
export const FieldListField = ({
options,
onChange,
value,
}: InputFieldProps): React.JSX.Element => {
const ComponentField = options.isPassword ? PasswordInput : TextInput;
return (
<FormGroup isRequired={options.isRequired} label={options.label}>
<ComponentField
aria-label={`Field list ${options.key}`}
data-testid={`field ${options.key}`}
isRequired={options.isRequired}
value={value}
placeholder={options.placeholder}
onChange={(e, newValue) => onChange(options.key, newValue)}
onBlur={(e) => trimInputOnBlur(value, (trimmed) => onChange(options.key, trimmed))(e)}
onPaste={trimInputOnPaste((trimmed) => onChange(options.key, trimmed))}
/>
</FormGroup>
);
};
const FieldList = ({ values, onUpdate, fields }: FieldListProps): React.JSX.Element => {
const update = (key: FieldOptions['key'], value: string) => {
onUpdate(values.map((d) => (d.key === key ? { key, value } : d)));
};
return (
<Stack hasGutter>
{fields.map((field) => (
<StackItem key={field.key}>
<FieldListField
options={field}
onChange={update}
value={values.find((data) => data.key === field.key)?.value || ''}
/>
</StackItem>
))}
</Stack>
);
};
export default FieldList;