|
@@ -13,7 +13,7 @@ import {
|
|
|
} from "antd";
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
import CustomColorPicker from "@/components/CustomColorPicker";
|
|
|
-import { ColumnItem, TableItemType } from "@/type";
|
|
|
+import { ColumnItem as ColumnItemType, TableItemType } from "@/type";
|
|
|
import { TABLE_TYPE_OPTIONS, DATA_TYPE_OPTIONS } from "@/constants";
|
|
|
import { uuid } from "@/utils";
|
|
|
import { DataType } from "@/enum";
|
|
@@ -21,6 +21,8 @@ import { useModel } from "umi";
|
|
|
import { DndContext } from "@dnd-kit/core";
|
|
|
import type { DragEndEvent } from "@dnd-kit/core";
|
|
|
import { SortableContext, arrayMove, verticalListSortingStrategy } from "@dnd-kit/sortable";
|
|
|
+import { restrictToParentElement } from "@dnd-kit/modifiers";
|
|
|
+import ColumnItem from "./ColumnItem";
|
|
|
|
|
|
export default function TableItem({
|
|
|
data,
|
|
@@ -74,7 +76,7 @@ export default function TableItem({
|
|
|
|
|
|
// 添加字段
|
|
|
const handleAddColumn = () => {
|
|
|
- const newColumn: ColumnItem = {
|
|
|
+ const newColumn: ColumnItemType = {
|
|
|
id: uuid(),
|
|
|
schemaName: "",
|
|
|
name: "",
|
|
@@ -104,8 +106,20 @@ export default function TableItem({
|
|
|
});
|
|
|
};
|
|
|
|
|
|
- const handleDragEnd = (event: DragEndEvent) => {
|
|
|
-
|
|
|
+ const handleDragEnd = (dragItem: DragEndEvent) => {
|
|
|
+ const { active, over } = dragItem;
|
|
|
+ if (!active || !over) return; // 处理边界情况
|
|
|
+
|
|
|
+ const activeIndex = tableColumnList.findIndex((item) => item.id === active.id);
|
|
|
+ const overIndex = tableColumnList.findIndex((item) => item.id === over.id);
|
|
|
+
|
|
|
+ const newList = arrayMove(tableColumnList, activeIndex, overIndex);
|
|
|
+ setList(newList);
|
|
|
+ onChange({
|
|
|
+ table,
|
|
|
+ tableColumnList: newList,
|
|
|
+ isTable: true,
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
return (
|
|
@@ -274,221 +288,16 @@ export default function TableItem({
|
|
|
|
|
|
<div className="column-content border-solid border-1px border-#e4e4e4 border-x-none p-y-10px">
|
|
|
{/* 字段内容 */}
|
|
|
- <DndContext onDragEnd={handleDragEnd}>
|
|
|
+ <DndContext onDragEnd={handleDragEnd} modifiers={[restrictToParentElement]}>
|
|
|
<SortableContext items={list.map(item => item.id)} strategy={verticalListSortingStrategy}>
|
|
|
{list.map((column, index) => {
|
|
|
return (
|
|
|
- <div
|
|
|
+ <ColumnItem
|
|
|
key={column.id}
|
|
|
- className="column-item flex gap-4px items-center jutify-space-between hover:bg-gray-100 mb-4px"
|
|
|
- >
|
|
|
- <HolderOutlined className="cursor-move" />
|
|
|
- <Tooltip title="字段编码">
|
|
|
- <Input
|
|
|
- placeholder="编码"
|
|
|
- defaultValue={column.schemaName}
|
|
|
- className="flex-1"
|
|
|
- onChange={(e) =>
|
|
|
- handleChangeColumn(
|
|
|
- index,
|
|
|
- "schemaName",
|
|
|
- e.target.value
|
|
|
- )
|
|
|
- }
|
|
|
- />
|
|
|
- </Tooltip>
|
|
|
- <Tooltip title="字段类型">
|
|
|
- <Select
|
|
|
- placeholder="类型"
|
|
|
- className="w-80px"
|
|
|
- options={DATA_TYPE_OPTIONS}
|
|
|
- value={column.type}
|
|
|
- onChange={(value) =>
|
|
|
- handleChangeColumn(index, "type", value)
|
|
|
- }
|
|
|
- dropdownStyle={{ width: 120 }}
|
|
|
- />
|
|
|
- </Tooltip>
|
|
|
- <Tooltip title="非空">
|
|
|
- <div
|
|
|
- className="
|
|
|
- rounded-4px
|
|
|
- cus-btn
|
|
|
- w-32px
|
|
|
- h-32px
|
|
|
- bg-#eee
|
|
|
- flex-none
|
|
|
- text-center
|
|
|
- leading-32px
|
|
|
- cursor-pointer
|
|
|
- hover:bg-#ddd"
|
|
|
- style={
|
|
|
- column.isRequired
|
|
|
- ? { background: "#1677ff", color: "#fff" }
|
|
|
- : {}
|
|
|
- }
|
|
|
- onClick={() =>
|
|
|
- handleChangeColumn(
|
|
|
- index,
|
|
|
- "isRequired",
|
|
|
- !column.isRequired
|
|
|
- )
|
|
|
- }
|
|
|
- >
|
|
|
- !
|
|
|
- </div>
|
|
|
- </Tooltip>
|
|
|
- <Tooltip title="唯一">
|
|
|
- <div
|
|
|
- className="
|
|
|
- rounded-4px
|
|
|
- cus-btn
|
|
|
- w-32px
|
|
|
- h-32px
|
|
|
- bg-#eee
|
|
|
- flex-none
|
|
|
- text-center
|
|
|
- leading-32px
|
|
|
- cursor-pointer
|
|
|
- hover:bg-#ddd"
|
|
|
- style={
|
|
|
- column.isUnique
|
|
|
- ? { background: "#1677ff", color: "#fff" }
|
|
|
- : {}
|
|
|
- }
|
|
|
- onClick={() =>
|
|
|
- handleChangeColumn(
|
|
|
- index,
|
|
|
- "isUnique",
|
|
|
- !column.isUnique
|
|
|
- )
|
|
|
- }
|
|
|
- >
|
|
|
- 1
|
|
|
- </div>
|
|
|
- </Tooltip>
|
|
|
- <Popover
|
|
|
- trigger="click"
|
|
|
- placement="right"
|
|
|
- content={
|
|
|
- <div
|
|
|
- className="w-360px max-h-400px overflow-hidden"
|
|
|
- onClick={(e) => e.stopPropagation()}
|
|
|
- >
|
|
|
- <Form layout="vertical">
|
|
|
- <Row gutter={8}>
|
|
|
- <Col span={12}>
|
|
|
- <Form.Item label="字段名称" name="pkName">
|
|
|
- <Input
|
|
|
- className="w-full"
|
|
|
- placeholder="中文"
|
|
|
- value={column.cn_name}
|
|
|
- onChange={(e) =>
|
|
|
- handleChangeColumn(
|
|
|
- index,
|
|
|
- "cn_name",
|
|
|
- e.target.value
|
|
|
- )
|
|
|
- }
|
|
|
- />
|
|
|
- <Input
|
|
|
- className="w-full"
|
|
|
- placeholder="英文"
|
|
|
- value={column.en_name}
|
|
|
- onChange={(e) =>
|
|
|
- handleChangeColumn(
|
|
|
- index,
|
|
|
- "en_name",
|
|
|
- e.target.value
|
|
|
- )
|
|
|
- }
|
|
|
- />
|
|
|
- </Form.Item>
|
|
|
- </Col>
|
|
|
- <Col span={12}>
|
|
|
- <Form.Item label="默认值" name="pkName">
|
|
|
- <Input
|
|
|
- className="w-full"
|
|
|
- placeholder="默认值"
|
|
|
- value={column.defaultValue}
|
|
|
- onChange={(e) =>
|
|
|
- handleChangeColumn(
|
|
|
- index,
|
|
|
- "defaultValue",
|
|
|
- e.target.value
|
|
|
- )
|
|
|
- }
|
|
|
- />
|
|
|
- </Form.Item>
|
|
|
- </Col>
|
|
|
- </Row>
|
|
|
- <Row gutter={8}>
|
|
|
- <Col span={12}>
|
|
|
- <Form.Item label="长度">
|
|
|
- <InputNumber
|
|
|
- placeholder="请输入"
|
|
|
- min={0}
|
|
|
- className="w-full"
|
|
|
- value={column.maxLength}
|
|
|
- onChange={(num) =>
|
|
|
- handleChangeColumn(
|
|
|
- index,
|
|
|
- "maxLength",
|
|
|
- num
|
|
|
- )
|
|
|
- }
|
|
|
- />
|
|
|
- </Form.Item>
|
|
|
- </Col>
|
|
|
- <Col span={12}>
|
|
|
- <Form.Item label="精度">
|
|
|
- <InputNumber
|
|
|
- placeholder="请输入"
|
|
|
- min={0}
|
|
|
- className="w-full"
|
|
|
- value={column.precision}
|
|
|
- onChange={(num) =>
|
|
|
- handleChangeColumn(
|
|
|
- index,
|
|
|
- "precision",
|
|
|
- num
|
|
|
- )
|
|
|
- }
|
|
|
- />
|
|
|
- </Form.Item>
|
|
|
- </Col>
|
|
|
- </Row>
|
|
|
- <Row>
|
|
|
- <Col span={24}>
|
|
|
- <Form.Item label="描述" name="pkName">
|
|
|
- <Input.TextArea
|
|
|
- className="w-full"
|
|
|
- placeholder="描述中文..."
|
|
|
- />
|
|
|
- <Input.TextArea
|
|
|
- className="w-full"
|
|
|
- placeholder="描述英文..."
|
|
|
- />
|
|
|
- </Form.Item>
|
|
|
- </Col>
|
|
|
- </Row>
|
|
|
- </Form>
|
|
|
- <Button
|
|
|
- type="default"
|
|
|
- danger
|
|
|
- className="w-full"
|
|
|
- onClick={() => handleDeleteColumn(column.id)}
|
|
|
- >
|
|
|
- 删除
|
|
|
- </Button>
|
|
|
- </div>
|
|
|
- }
|
|
|
- >
|
|
|
- <div className="rounded-4px cus-btn w-32px h-32px bg-#eee flex-none text-center leading-32px cursor-pointer hover:bg-#ddd">
|
|
|
- <i className="iconfont icon-gengduo" />
|
|
|
- </div>
|
|
|
- </Popover>
|
|
|
- </div>
|
|
|
+ column={column}
|
|
|
+ onChange={(key, val) => handleChangeColumn(index, key, val)}
|
|
|
+ onDelete={handleDeleteColumn}
|
|
|
+ />
|
|
|
);
|
|
|
})}
|
|
|
</SortableContext>
|