-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMetadataTextField.tsx
More file actions
109 lines (106 loc) · 3.12 KB
/
MetadataTextField.tsx
File metadata and controls
109 lines (106 loc) · 3.12 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import React, { useState } from "react";
import { ClowderMetadataTextField } from "../../styledComponents/ClowderMetadataTextField";
import { MetadataEditButton } from "./MetadataEditButton";
import { Grid } from "@mui/material";
import { AuthWrapper } from "../../auth/AuthWrapper";
import { FrozenWrapper } from "../../auth/FrozenWrapper";
export const MetadataTextField = (props) => {
const {
widgetName,
fieldName,
content,
setMetadata,
metadataId,
updateMetadata,
resourceId,
initialReadOnly,
isRequired,
datasetRole,
frozen,
frozenVersionNum,
} = props;
// FIX: Properly initialize localContent to always have the field
const [localContent, setLocalContent] = useState(() => {
if (content && content[fieldName] !== undefined) {
return content;
}
return { [fieldName]: "" }; // Ensure the field always exists
});
const [readOnly, setReadOnly] = useState(initialReadOnly);
const [inputChanged, setInputChanged] = useState(false);
const getValue = () => {
if (readOnly) {
// Read-only mode: show content or "null"
if (content && content[fieldName] !== undefined && content[fieldName] !== null) {
return content[fieldName];
}
if (localContent && localContent[fieldName] !== undefined && localContent[fieldName] !== null) {
return localContent[fieldName];
}
return "null";
} else {
// Edit mode: show localContent value (what user is typing)
// FIX: This will now always return a string, never undefined
return localContent[fieldName] || "";
}
};
return (
<Grid container spacing={2} sx={{ alignItems: "center" }}>
<Grid item xs={11} sm={11} md={11} lg={11} xl={11}>
<ClowderMetadataTextField
label={fieldName}
variant="outlined"
margin="normal"
fullWidth
name={widgetName}
required={isRequired}
value={getValue()}
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
setInputChanged(true);
const tempContents: { [key: string]: string | number } = {};
tempContents[fieldName] = event.target.value;
setLocalContent(tempContents);
setMetadata
? metadataId
? setMetadata({
id: metadataId,
definition: widgetName,
content: tempContents,
})
: setMetadata({
definition: widgetName,
content: tempContents,
})
: null;
}}
disabled={readOnly}
helperText={
inputChanged
? "* You have changed this field. Remember to save/ update."
: ""
}
/>
</Grid>
<Grid item xs={1} sm={1} md={1} lg={1} xl={1}>
<FrozenWrapper frozen={frozen} frozenVersionNum={frozenVersionNum}>
<AuthWrapper
currRole={datasetRole.role}
allowedRoles={["owner", "editor", "uploader"]}
>
<MetadataEditButton
readOnly={readOnly}
setReadOnly={setReadOnly}
updateMetadata={updateMetadata}
content={localContent}
metadataId={metadataId}
resourceId={resourceId}
widgetName={widgetName}
setInputChanged={setInputChanged}
setMetadata={setMetadata}
/>
</AuthWrapper>
</FrozenWrapper>
</Grid>
</Grid>
);
};