Skip to content

Commit

Permalink
Fix field type not updating when Type name is changed (#193 part1)
Browse files Browse the repository at this point in the history
  • Loading branch information
1ilit committed Jul 31, 2024
1 parent 1a2ac74 commit 52248f1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/components/EditorHeader/ControlPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ export default function ControlPanel({
);
} else if (a.component === "self") {
updateType(a.tid, a.undo);
if (a.updatedFields) {
if (a.undo.name) {
a.updatedFields.forEach((x) =>
updateField(x.tid, x.fid, { type: a.undo.name.toUpperCase() }),
);
}
}
}
} else if (a.element === ObjectType.ENUM) {
updateEnum(a.id, a.undo);
Expand Down Expand Up @@ -460,6 +467,13 @@ export default function ControlPanel({
});
} else if (a.component === "self") {
updateType(a.tid, a.redo);
if (a.updatedFields) {
if (a.redo.name) {
a.updatedFields.forEach((x) =>
updateField(x.tid, x.fid, { type: a.redo.name.toUpperCase() }),
);
}
}
}
} else if (a.element === ObjectType.ENUM) {
updateEnum(a.id, a.redo);
Expand Down
23 changes: 21 additions & 2 deletions src/components/EditorSidePanel/TypesTab/TypeInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import {
Card,
} from "@douyinfe/semi-ui";
import { IconDeleteStroked, IconPlus } from "@douyinfe/semi-icons";
import { useUndoRedo, useTypes } from "../../../hooks";
import { useUndoRedo, useTypes, useDiagram } from "../../../hooks";
import TypeField from "./TypeField";
import { useTranslation } from "react-i18next";

export default function TypeInfo({ index, data }) {
const { deleteType, updateType } = useTypes();
const { tables, updateField } = useDiagram();
const { setUndoStack, setRedoStack } = useUndoRedo();
const [editField, setEditField] = useState({});
const { t } = useTranslation();
Expand All @@ -37,10 +38,27 @@ export default function TypeInfo({ index, data }) {
validateStatus={data.name === "" ? "error" : "default"}
placeholder={t("name")}
className="ms-2"
onChange={(value) => updateType(index, { name: value })}
onChange={(value) => {
updateType(index, { 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 })}
onBlur={(e) => {
if (e.target.value === editField.name) return;

const updatedFields = tables.reduce((acc, table) => {
table.fields.forEach((_, i) => {
acc.push({ tid: table.id, fid: i });
});
return acc;
}, []);

setUndoStack((prev) => [
...prev,
{
Expand All @@ -50,6 +68,7 @@ export default function TypeInfo({ index, data }) {
tid: index,
undo: editField,
redo: { name: e.target.value },
updatedFields,
message: t("edit_type", {
typeName: data.name,
extra: "[name]",
Expand Down

0 comments on commit 52248f1

Please sign in to comment.