Fix field type not updating when enum name is changed (#193 part2)

This commit is contained in:
1ilit 2024-07-31 19:12:28 +03:00
parent 34d9045229
commit 844b24100e
3 changed files with 41 additions and 4 deletions

View File

@ -308,6 +308,13 @@ export default function ControlPanel({
} }
} else if (a.element === ObjectType.ENUM) { } else if (a.element === ObjectType.ENUM) {
updateEnum(a.id, a.undo); updateEnum(a.id, a.undo);
if (a.updatedFields) {
if (a.undo.name) {
a.updatedFields.forEach((x) =>
updateField(x.tid, x.fid, { type: a.undo.name.toUpperCase() }),
);
}
}
} }
setRedoStack((prev) => [...prev, a]); setRedoStack((prev) => [...prev, a]);
} else if (a.action === Action.PAN) { } else if (a.action === Action.PAN) {
@ -477,6 +484,13 @@ export default function ControlPanel({
} }
} else if (a.element === ObjectType.ENUM) { } else if (a.element === ObjectType.ENUM) {
updateEnum(a.id, a.redo); updateEnum(a.id, a.redo);
if (a.updatedFields) {
if (a.redo.name) {
a.updatedFields.forEach((x) =>
updateField(x.tid, x.fid, { type: a.redo.name.toUpperCase() }),
);
}
}
} }
setUndoStack((prev) => [...prev, a]); setUndoStack((prev) => [...prev, a]);
} else if (a.action === Action.PAN) { } else if (a.action === Action.PAN) {

View File

@ -1,13 +1,14 @@
import { useState } from "react"; import { useState } from "react";
import { Button, Input, TagInput } from "@douyinfe/semi-ui"; import { Button, Input, TagInput } from "@douyinfe/semi-ui";
import { IconDeleteStroked } from "@douyinfe/semi-icons"; import { IconDeleteStroked } from "@douyinfe/semi-icons";
import { useEnums, useUndoRedo } from "../../../hooks"; import { useDiagram, useEnums, useUndoRedo } from "../../../hooks";
import { Action, ObjectType } from "../../../data/constants"; import { Action, ObjectType } from "../../../data/constants";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
export default function EnumDetails({ data, i }) { export default function EnumDetails({ data, i }) {
const { t } = useTranslation(); const { t } = useTranslation();
const { deleteEnum, updateEnum } = useEnums(); const { deleteEnum, updateEnum } = useEnums();
const { tables, updateField } = useDiagram();
const { setUndoStack, setRedoStack } = useUndoRedo(); const { setUndoStack, setRedoStack } = useUndoRedo();
const [editField, setEditField] = useState({}); const [editField, setEditField] = useState({});
@ -19,10 +20,29 @@ export default function EnumDetails({ data, i }) {
value={data.name} value={data.name}
placeholder={t("name")} placeholder={t("name")}
validateStatus={data.name.trim() === "" ? "error" : "default"} validateStatus={data.name.trim() === "" ? "error" : "default"}
onChange={(value) => updateEnum(i, { name: value })} onChange={(value) => {
updateEnum(i, { name: value });
tables.forEach((table, i) => {
table.fields.forEach((field, j) => {
if (field.type.toLowerCase() === data.name.toLowerCase()) {
updateField(i, j, { type: value.toUpperCase() });
}
});
});
}}
onFocus={(e) => setEditField({ name: e.target.value })} onFocus={(e) => setEditField({ name: e.target.value })}
onBlur={(e) => { onBlur={(e) => {
if (e.target.value === editField.name) return; if (e.target.value === editField.name) return;
const updatedFields = tables.reduce((acc, table) => {
table.fields.forEach((field, i) => {
if (field.type.toLowerCase() === data.name.toLowerCase()) {
acc.push({ tid: table.id, fid: i });
}
});
return acc;
}, []);
setUndoStack((prev) => [ setUndoStack((prev) => [
...prev, ...prev,
{ {
@ -31,6 +51,7 @@ export default function EnumDetails({ data, i }) {
id: i, id: i,
undo: editField, undo: editField,
redo: { name: e.target.value }, redo: { name: e.target.value },
updatedFields,
message: t("edit_enum", { message: t("edit_enum", {
enumName: e.target.value, enumName: e.target.value,
extra: "[name]", extra: "[name]",

View File

@ -53,8 +53,10 @@ export default function TypeInfo({ index, data }) {
if (e.target.value === editField.name) return; if (e.target.value === editField.name) return;
const updatedFields = tables.reduce((acc, table) => { const updatedFields = tables.reduce((acc, table) => {
table.fields.forEach((_, i) => { table.fields.forEach((field, i) => {
acc.push({ tid: table.id, fid: i }); if (field.type.toLowerCase() === data.name.toLowerCase()) {
acc.push({ tid: table.id, fid: i });
}
}); });
return acc; return acc;
}, []); }, []);