forked from cloudreve/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditPropsDialog.tsx
More file actions
179 lines (170 loc) · 6.58 KB
/
EditPropsDialog.tsx
File metadata and controls
179 lines (170 loc) · 6.58 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { Box, DialogContent, FormControl, Grid2, Link } from "@mui/material";
import { useEffect, useRef, useState } from "react";
import { Trans, useTranslation } from "react-i18next";
import { CustomProps } from "../../../../api/explorer";
import { DenseFilledTextField } from "../../../Common/StyledComponents";
import DraggableDialog from "../../../Dialogs/DraggableDialog";
import { getPropsContent } from "../../../FileManager/Sidebar/CustomProps/CustomPropsItem";
import SettingForm from "../../../Pages/Setting/SettingForm";
import { NoMarginHelperText } from "../../Settings/Settings";
import { FieldTypes } from "./DraggableCustomPropsRow";
interface EditPropsDialogProps {
open: boolean;
onClose: () => void;
onSave: (props: CustomProps) => void;
isNew: boolean;
props?: CustomProps;
}
const EditPropsDialog = ({ open, onClose, onSave, isNew, props }: EditPropsDialogProps) => {
const { t } = useTranslation("dashboard");
const formRef = useRef<HTMLFormElement>(null);
const [editProps, setEditProps] = useState<CustomProps | undefined>(props);
const handleSave = () => {
if (!formRef.current?.checkValidity()) {
formRef.current?.reportValidity();
return;
}
onSave({ ...editProps } as CustomProps);
onClose();
};
useEffect(() => {
if (props) {
setEditProps({ ...props });
}
if (!open) {
setTimeout(() => {
setEditProps(undefined);
}, 100);
}
}, [open, props]);
if (!editProps || !editProps.type) return null;
const fieldType = FieldTypes[editProps?.type];
return (
<DraggableDialog
title={isNew ? t("customProps.addProp") : t("customProps.editProp")}
showActions
showCancel
onAccept={handleSave}
dialogProps={{
open,
onClose,
fullWidth: true,
maxWidth: "sm",
}}
>
<DialogContent>
<Box component={"form"} ref={formRef} sx={{ display: "flex", flexDirection: "column", gap: 2, mt: 1 }}>
<SettingForm title={t("customProps.id")} lgWidth={12}>
<FormControl fullWidth>
<DenseFilledTextField
disabled={!isNew}
slotProps={{
htmlInput: {
max: 128,
pattern: "^[a-zA-Z0-9_-]+$",
title: t("customProps.idPatternDes"),
},
}}
value={editProps?.id || ""}
required
onChange={(e) => setEditProps({ ...editProps, id: e.target.value } as CustomProps)}
/>
<NoMarginHelperText>{t("customProps.idDes")}</NoMarginHelperText>
</FormControl>
</SettingForm>
<SettingForm title={t("settings.displayName")} lgWidth={12}>
<FormControl fullWidth>
<DenseFilledTextField
value={editProps?.name || ""}
onChange={(e) =>
setEditProps({
...editProps,
name: e.target.value,
} as CustomProps)
}
required
/>
<NoMarginHelperText>{t("settings.displayNameDes")}</NoMarginHelperText>
</FormControl>
</SettingForm>
<SettingForm title={t("customProps.icon")} lgWidth={12}>
<FormControl fullWidth>
<DenseFilledTextField
value={editProps?.icon || ""}
onChange={(e) => setEditProps({ ...editProps, icon: e.target.value } as CustomProps)}
/>
<NoMarginHelperText>
{
<Trans
i18nKey="dashboard:customProps.iconDes"
components={[<Link target="_blank" href="https://icon-sets.iconify.design/" />]}
/>
}
</NoMarginHelperText>
</FormControl>
</SettingForm>
{(fieldType.minTitle || fieldType.maxTitle) && (
<Grid2 container spacing={2} size={{ xs: 12 }}>
{fieldType.minTitle && (
<SettingForm title={t(fieldType.minTitle)} lgWidth={6} noContainer>
<FormControl fullWidth>
<DenseFilledTextField
type="number"
value={editProps?.min || ""}
onChange={(e) => setEditProps({ ...editProps, min: e.target.value === "" ? undefined : parseInt(e.target.value) } as CustomProps)}
/>
{fieldType.minDes && <NoMarginHelperText>{t(fieldType.minDes)}</NoMarginHelperText>}
</FormControl>
</SettingForm>
)}
{fieldType.maxTitle && (
<SettingForm title={t(fieldType.maxTitle)} lgWidth={6} noContainer>
<FormControl fullWidth>
<DenseFilledTextField
type="number"
required={fieldType.maxRequired}
value={editProps?.max || ""}
onChange={(e) => setEditProps({ ...editProps, max: e.target.value === "" ? undefined : parseInt(e.target.value) } as CustomProps)}
/>
{fieldType.maxDes && <NoMarginHelperText>{t(fieldType.maxDes)}</NoMarginHelperText>}
</FormControl>
</SettingForm>
)}
</Grid2>
)}
{fieldType.showOptions && (
<SettingForm title={t("customProps.options")} lgWidth={12}>
<FormControl fullWidth>
<DenseFilledTextField
multiline
rows={4}
value={editProps?.options?.join("\n") || ""}
onChange={(e) => setEditProps({ ...editProps, options: e.target.value.split("\n") } as CustomProps)}
/>
<NoMarginHelperText>{t("customProps.optionsDes")}</NoMarginHelperText>
</FormControl>
</SettingForm>
)}
<SettingForm title={t("customProps.default")} lgWidth={12}>
<FormControl fullWidth>
{getPropsContent(
{
props: editProps,
id: editProps.id,
value: editProps.default ?? "",
},
(value) => {
setEditProps({ ...editProps, default: value } as CustomProps);
},
false,
false,
true,
)}
</FormControl>
</SettingForm>
</Box>
</DialogContent>
</DraggableDialog>
);
};
export default EditPropsDialog;