|
6 | 6 | Button, |
7 | 7 | Card, |
8 | 8 | Select, |
| 9 | + Checkbox, |
9 | 10 | } from "@douyinfe/semi-ui"; |
10 | 11 | import ColorPicker from "../ColorPicker"; |
11 | 12 | import { IconDeleteStroked } from "@douyinfe/semi-icons"; |
@@ -72,11 +73,11 @@ export default function TableInfo({ data }) { |
72 | 73 | const inheritedFieldNames = |
73 | 74 | Array.isArray(data.inherits) && data.inherits.length > 0 |
74 | 75 | ? data.inherits |
75 | | - .map((parentName) => { |
76 | | - const parent = tables.find((t) => t.name === parentName); |
77 | | - return parent ? parent.fields.map((f) => f.name) : []; |
78 | | - }) |
79 | | - .flat() |
| 76 | + .map((parentName) => { |
| 77 | + const parent = tables.find((t) => t.name === parentName); |
| 78 | + return parent ? parent.fields.map((f) => f.name) : []; |
| 79 | + }) |
| 80 | + .flat() |
80 | 81 | : []; |
81 | 82 |
|
82 | 83 | return ( |
@@ -337,6 +338,134 @@ export default function TableInfo({ data }) { |
337 | 338 | /> |
338 | 339 | </div> |
339 | 340 | </div> |
| 341 | + <div className="flex justify-between items-center gap-2 mb-2"> |
| 342 | + <Checkbox |
| 343 | + checked={data.fields.some((f) => f.name === "created_at")} |
| 344 | + disabled={layout.readOnly} |
| 345 | + onChange={(e) => { |
| 346 | + const exists = data.fields.some((f) => f.name === "created_at"); |
| 347 | + if (e.target.checked && !exists) { |
| 348 | + const id = nanoid(); |
| 349 | + const newField = { |
| 350 | + id, |
| 351 | + name: "created_at", |
| 352 | + type: "TIMESTAMP", |
| 353 | + default: "CURRENT_TIMESTAMP", |
| 354 | + check: "", |
| 355 | + primary: false, |
| 356 | + unique: false, |
| 357 | + notNull: false, |
| 358 | + increment: false, |
| 359 | + comment: "", |
| 360 | + }; |
| 361 | + setUndoStack((prev) => [ |
| 362 | + ...prev, |
| 363 | + { |
| 364 | + action: Action.EDIT, |
| 365 | + element: ObjectType.TABLE, |
| 366 | + component: "field_add", |
| 367 | + tid: data.id, |
| 368 | + fid: id, |
| 369 | + message: t("edit_table", { |
| 370 | + tableName: data.name, |
| 371 | + extra: "[add created_at]", |
| 372 | + }), |
| 373 | + }, |
| 374 | + ]); |
| 375 | + setRedoStack([]); |
| 376 | + updateTable(data.id, { |
| 377 | + fields: [...data.fields, newField], |
| 378 | + }); |
| 379 | + } else if (!e.target.checked && exists) { |
| 380 | + const field = data.fields.find((f) => f.name === "created_at"); |
| 381 | + setUndoStack((prev) => [ |
| 382 | + ...prev, |
| 383 | + { |
| 384 | + action: Action.EDIT, |
| 385 | + element: ObjectType.TABLE, |
| 386 | + component: "field_delete", |
| 387 | + tid: data.id, |
| 388 | + fid: field.id, |
| 389 | + data: field, |
| 390 | + message: t("edit_table", { |
| 391 | + tableName: data.name, |
| 392 | + extra: "[delete created_at]", |
| 393 | + }), |
| 394 | + }, |
| 395 | + ]); |
| 396 | + setRedoStack([]); |
| 397 | + updateTable(data.id, { |
| 398 | + fields: data.fields.filter((f) => f.name !== "created_at"), |
| 399 | + }); |
| 400 | + } |
| 401 | + }} |
| 402 | + > |
| 403 | + created_at |
| 404 | + </Checkbox> |
| 405 | + <Checkbox |
| 406 | + checked={data.fields.some((f) => f.name === "updated_at")} |
| 407 | + disabled={layout.readOnly} |
| 408 | + onChange={(e) => { |
| 409 | + const exists = data.fields.some((f) => f.name === "updated_at"); |
| 410 | + if (e.target.checked && !exists) { |
| 411 | + const id = nanoid(); |
| 412 | + const newField = { |
| 413 | + id, |
| 414 | + name: "updated_at", |
| 415 | + type: "TIMESTAMP", |
| 416 | + default: "CURRENT_TIMESTAMP", |
| 417 | + check: "", |
| 418 | + primary: false, |
| 419 | + unique: false, |
| 420 | + notNull: false, |
| 421 | + increment: false, |
| 422 | + comment: "", |
| 423 | + }; |
| 424 | + setUndoStack((prev) => [ |
| 425 | + ...prev, |
| 426 | + { |
| 427 | + action: Action.EDIT, |
| 428 | + element: ObjectType.TABLE, |
| 429 | + component: "field_add", |
| 430 | + tid: data.id, |
| 431 | + fid: id, |
| 432 | + message: t("edit_table", { |
| 433 | + tableName: data.name, |
| 434 | + extra: "[add updated_at]", |
| 435 | + }), |
| 436 | + }, |
| 437 | + ]); |
| 438 | + setRedoStack([]); |
| 439 | + updateTable(data.id, { |
| 440 | + fields: [...data.fields, newField], |
| 441 | + }); |
| 442 | + } else if (!e.target.checked && exists) { |
| 443 | + const field = data.fields.find((f) => f.name === "updated_at"); |
| 444 | + setUndoStack((prev) => [ |
| 445 | + ...prev, |
| 446 | + { |
| 447 | + action: Action.EDIT, |
| 448 | + element: ObjectType.TABLE, |
| 449 | + component: "field_delete", |
| 450 | + tid: data.id, |
| 451 | + fid: field.id, |
| 452 | + data: field, |
| 453 | + message: t("edit_table", { |
| 454 | + tableName: data.name, |
| 455 | + extra: "[delete updated_at]", |
| 456 | + }), |
| 457 | + }, |
| 458 | + ]); |
| 459 | + setRedoStack([]); |
| 460 | + updateTable(data.id, { |
| 461 | + fields: data.fields.filter((f) => f.name !== "updated_at"), |
| 462 | + }); |
| 463 | + } |
| 464 | + }} |
| 465 | + > |
| 466 | + updated_at |
| 467 | + </Checkbox> |
| 468 | + </div> |
340 | 469 | </div> |
341 | 470 | ); |
342 | 471 | } |
0 commit comments