Enable editing index names

This commit is contained in:
1ilit 2024-06-07 14:18:05 +03:00
parent 1283f66a86
commit 77bdb77a9e
5 changed files with 55 additions and 7 deletions

View File

@ -3,11 +3,13 @@ import { Input, Button, Popover, Checkbox, Select } from "@douyinfe/semi-ui";
import { IconMore, IconDeleteStroked } from "@douyinfe/semi-icons"; import { IconMore, IconDeleteStroked } from "@douyinfe/semi-icons";
import { useTables, useUndoRedo } from "../../../hooks"; import { useTables, useUndoRedo } from "../../../hooks";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useState } from "react";
export default function IndexDetails({ data, fields, iid, tid }) { export default function IndexDetails({ data, fields, iid, tid }) {
const { t } = useTranslation(); const { t } = useTranslation();
const { tables, updateTable } = useTables(); const { tables, updateTable } = useTables();
const { setUndoStack, setRedoStack } = useUndoRedo(); const { setUndoStack, setRedoStack } = useUndoRedo();
const [editField, setEditField] = useState({});
return ( return (
<div className="flex justify-between items-center mb-2"> <div className="flex justify-between items-center mb-2">
@ -29,11 +31,9 @@ export default function IndexDetails({ data, fields, iid, tid }) {
iid: iid, iid: iid,
undo: { undo: {
fields: [...data.fields], fields: [...data.fields],
name: `${data.fields.join("_")}_index`,
}, },
redo: { redo: {
fields: [...value], fields: [...value],
name: `${value.join("_")}_index`,
}, },
message: t("edit_table", { message: t("edit_table", {
tableName: tables[tid].name, tableName: tables[tid].name,
@ -48,7 +48,6 @@ export default function IndexDetails({ data, fields, iid, tid }) {
? { ? {
...index, ...index,
fields: [...value], fields: [...value],
name: `${value.join("_")}_index`,
} }
: index, : index,
), ),
@ -59,7 +58,48 @@ export default function IndexDetails({ data, fields, iid, tid }) {
content={ content={
<div className="px-1 popover-theme"> <div className="px-1 popover-theme">
<div className="font-semibold mb-1">{t("name")}: </div> <div className="font-semibold mb-1">{t("name")}: </div>
<Input value={data.name} placeholder={t("name")} disabled /> <Input
value={data.name}
placeholder={t("name")}
validateStatus={data.name.trim() === "" ? "error" : "default"}
onFocus={() =>
setEditField({
name: data.name,
})
}
onChange={(value) =>
updateTable(tid, {
indices: tables[tid].indices.map((index) =>
index.id === iid
? {
...index,
name: value,
}
: index,
),
})
}
onBlur={(e) => {
if (e.target.value === editField.name) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "index",
tid: tid,
iid: iid,
undo: editField,
redo: { name: e.target.value },
message: t("edit_table", {
tableName: tables[tid].name,
extra: "[index]",
}),
},
]);
setRedoStack([]);
}}
/>
<div className="flex justify-between items-center my-3"> <div className="flex justify-between items-center my-3">
<div className="font-medium">{t("unique")}</div> <div className="font-medium">{t("unique")}</div>
<Checkbox <Checkbox

View File

@ -20,7 +20,7 @@ export default function TableField({ data, tid, index }) {
<Col span={7}> <Col span={7}>
<Input <Input
value={data.name} value={data.name}
validateStatus={data.name === "" ? "error" : "default"} validateStatus={data.name.trim() === "" ? "error" : "default"}
placeholder="Name" placeholder="Name"
onChange={(value) => updateField(tid, index, { name: value })} onChange={(value) => updateField(tid, index, { name: value })}
onFocus={(e) => setEditField({ name: e.target.value })} onFocus={(e) => setEditField({ name: e.target.value })}

View File

@ -33,7 +33,7 @@ export default function TableInfo({ data }) {
<div className="text-md font-semibold break-keep">{t("name")}: </div> <div className="text-md font-semibold break-keep">{t("name")}: </div>
<Input <Input
value={data.name} value={data.name}
validateStatus={data.name === "" ? "error" : "default"} validateStatus={data.name.trim() === "" ? "error" : "default"}
placeholder={t("name")} placeholder={t("name")}
className="ms-2" className="ms-2"
onChange={(value) => updateTable(data.id, { name: value })} onChange={(value) => updateTable(data.id, { name: value })}
@ -290,7 +290,7 @@ export default function TableInfo({ data }) {
...data.indices, ...data.indices,
{ {
id: data.indices.length, id: data.indices.length,
name: `index_${data.indices.length}`, name: `${data.name}_index_${data.indices.length}`,
unique: false, unique: false,
fields: [], fields: [],
}, },

View File

@ -212,6 +212,7 @@ const en = {
edit_relationship: "{{extra}} Edit relationship {{refName}}", edit_relationship: "{{extra}} Edit relationship {{refName}}",
delete_relationship: "Delete relationship {{refName}}", delete_relationship: "Delete relationship {{refName}}",
not_found: "Not found", not_found: "Not found",
empty_index_name: "Declared an index with no name in table '{{tableName}}'",
}, },
}; };

View File

@ -168,6 +168,13 @@ export function getIssues(diagram) {
}); });
table.indices.forEach((index) => { table.indices.forEach((index) => {
if (index.name.trim() === "") {
issues.push(
i18n.t("empty_index_name", {
tableName: table.name,
}),
);
}
if (index.fields.length === 0) { if (index.fields.length === 0) {
issues.push( issues.push(
i18n.t("empty_index", { i18n.t("empty_index", {