= ({
{selectedField.type === 'grid' && (
<>
-
-
-
+
+
+ 列配置项
+ }
+ onClick={() => {
+ // 如果没有配置 columnSpans,先初始化为当前列数的平均分配
+ const currentSpans = selectedField.columnSpans || (() => {
+ const cols = selectedField.columns || 2;
+ const avgSpan = Math.floor(24 / cols);
+ return Array(cols).fill(avgSpan);
+ })();
+
+ const total = currentSpans.reduce((a, b) => a + b, 0);
+ if (total >= 24) {
+ return;
+ }
+ const newSpan = Math.min(12, 24 - total);
+ const newSpans = [...currentSpans, newSpan];
+ const currentChildren = selectedField.children || [];
+ const newChildren = [...currentChildren, []];
+
+ onFieldChange({
+ ...selectedField,
+ columnSpans: newSpans,
+ columns: newSpans.length,
+ children: newChildren,
+ });
+ }}
+ disabled={
+ selectedField.columnSpans &&
+ selectedField.columnSpans.reduce((a: number, b: number) => a + b, 0) >= 24
+ }
+ >
+ 添加列
+
+
+
+
+ {(selectedField.columnSpans || [12, 12]).map((span, index) => (
+
+ {index + 1}.
+ {
+ if (value) {
+ const currentSpans = selectedField.columnSpans || [12, 12];
+ const newSpans = [...currentSpans];
+ newSpans[index] = value;
+ onFieldChange({
+ ...selectedField,
+ columnSpans: newSpans,
+ });
+ }
+ }}
+ style={{ flex: 1 }}
+ addonAfter="/24"
+ />
+ }
+ danger
+ onClick={() => {
+ const currentSpans = selectedField.columnSpans || [12, 12];
+ const newSpans = currentSpans.filter((_, i) => i !== index);
+ const newChildren = (selectedField.children || [[], []]).filter((_, i) => i !== index);
+
+ // 如果删除后没有列了,重置为默认的2列配置
+ if (newSpans.length === 0) {
+ onFieldChange({
+ ...selectedField,
+ columnSpans: [12, 12],
+ columns: 2,
+ children: [[], []],
+ });
+ } else {
+ onFieldChange({
+ ...selectedField,
+ columnSpans: newSpans,
+ columns: newSpans.length,
+ children: newChildren,
+ });
+ }
+ }}
+ />
+
+ ))}
+
+
+ 总宽度:{(selectedField.columnSpans || [12, 12]).reduce((a: number, b: number) => a + b, 0)} / 24
+
+
+
diff --git a/frontend/src/pages/FormDesigner/config.ts b/frontend/src/pages/FormDesigner/config.ts
index 79fbadbc..c8730491 100644
--- a/frontend/src/pages/FormDesigner/config.ts
+++ b/frontend/src/pages/FormDesigner/config.ts
@@ -34,6 +34,26 @@ export interface ComponentMeta {
// 组件列表配置
export const COMPONENT_LIST: ComponentMeta[] = [
+ // 布局字段
+ {
+ type: 'grid',
+ label: '栅格布局',
+ icon: BorderOutlined,
+ category: '布局字段',
+ defaultConfig: {
+ columns: 2,
+ columnSpans: [12, 12], // 默认两列,各占12格
+ gutter: 16,
+ children: [[], []], // 初始化两列空数组
+ },
+ },
+ {
+ type: 'divider',
+ label: '分割线',
+ icon: MinusOutlined,
+ category: '布局字段',
+ defaultConfig: {},
+ },
// 基础字段
{
type: 'input',
@@ -131,6 +151,15 @@ export const COMPONENT_LIST: ComponentMeta[] = [
placeholder: '请选择时间',
},
},
+ {
+ type: 'text',
+ label: '文字',
+ icon: FileTextOutlined,
+ category: '基础字段',
+ defaultConfig: {
+ content: '这是一段文字',
+ },
+ },
// 高级字段
{
type: 'switch',
@@ -180,34 +209,6 @@ export const COMPONENT_LIST: ComponentMeta[] = [
] as any,
},
},
- // 布局字段
- {
- type: 'text',
- label: '文字',
- icon: FileTextOutlined,
- category: '布局字段',
- defaultConfig: {
- content: '这是一段文字',
- },
- },
- {
- type: 'grid',
- label: '栅格布局',
- icon: BorderOutlined,
- category: '布局字段',
- defaultConfig: {
- columns: 2,
- gutter: 16,
- children: [[], []], // 初始化两列空数组
- },
- },
- {
- type: 'divider',
- label: '分割线',
- icon: MinusOutlined,
- category: '布局字段',
- defaultConfig: {},
- },
];
// 根据分类分组组件
diff --git a/frontend/src/pages/FormDesigner/types.ts b/frontend/src/pages/FormDesigner/types.ts
index 74ee7881..43635a29 100644
--- a/frontend/src/pages/FormDesigner/types.ts
+++ b/frontend/src/pages/FormDesigner/types.ts
@@ -49,6 +49,7 @@ export interface FieldConfig {
span?: number; // 栅格占位格数(该字段在栅格中占几列)
content?: string; // 文本内容(用于 text 组件)
columns?: number; // 栅格列数(用于 grid 组件)
+ columnSpans?: number[]; // 栅格每列宽度(用于 grid 组件,如 [2, 18, 2],总和不超过24)
gutter?: number; // 栅格间距(用于 grid 组件)
children?: FieldConfig[][]; // 子字段(用于容器组件,二维数组,每个子数组代表一列)
}