gbformdesign 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +84 -84
- package/dist/gbformdesign.css +1 -1
- package/dist/gbformdesign.js +1146 -1051
- package/dist/gbformdesign.js.map +1 -1
- package/dist/gbformdesign.umd.cjs +6 -6
- package/dist/gbformdesign.umd.cjs.map +1 -1
- package/package.json +58 -57
- package/src/core/FormVariable.js +328 -328
- package/src/core/dynamicFormUtils.js +139 -139
- package/src/core/fileIO.js +90 -90
- package/src/core/formVariableTypeSystem.js +327 -327
- package/src/core/index.js +26 -26
- package/src/core/schema.js +192 -192
- package/src/design/composables/useDefaultValue.js +55 -55
- package/src/design/constants/componentTemplates.js +181 -190
- package/src/design/constants/componentTypes.js +70 -70
- package/src/design/utils/componentUtils.js +75 -75
- package/src/designer/fc/FcCanvasField.vue +277 -276
- package/src/designer/fc/FcJsonPanel.vue +113 -113
- package/src/designer/fc/FcPreviewDialog.vue +62 -62
- package/src/designer/fc/FcPropsPanel.vue +373 -373
- package/src/designer/fc/FormDesigner.vue +633 -633
- package/src/designer/fc/codegen.js +120 -120
- package/src/index.js +48 -48
- package/src/styles/config.scss +102 -102
- package/src/styles/fc/designer.css +837 -837
- package/src/styles/fc/icon.css +1053 -1053
- package/src/styles/index.scss +4 -4
- package/src/view/FormField.vue +301 -301
- package/src/view/FormViewer.vue +216 -216
- package/src/view/composables/useFormValidation.js +201 -201
|
@@ -1,201 +1,201 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 表单验证的组合式函数
|
|
3
|
-
*/
|
|
4
|
-
import { COMPONENT_TYPES } from '../../design/constants/componentTypes.js'
|
|
5
|
-
import { isEmpty, getFieldName } from '../../design/utils/componentUtils.js'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 生成表单验证规则
|
|
9
|
-
*/
|
|
10
|
-
export function useFormValidation() {
|
|
11
|
-
/**
|
|
12
|
-
* 为单个组件生成验证规则
|
|
13
|
-
*/
|
|
14
|
-
const generateComponentRule = (component) => {
|
|
15
|
-
const fieldName = getFieldName(component)
|
|
16
|
-
const rules = []
|
|
17
|
-
|
|
18
|
-
// 必填验证
|
|
19
|
-
if (component.required) {
|
|
20
|
-
const isArrayType = component.type === COMPONENT_TYPES.CHECKBOX_GROUP
|
|
21
|
-
const isChoiceType = component.type === COMPONENT_TYPES.RADIO_GROUP
|
|
22
|
-
|| component.type === COMPONENT_TYPES.SELECT
|
|
23
|
-
rules.push({
|
|
24
|
-
required: true,
|
|
25
|
-
...(isArrayType ? { type: 'array' } : {}),
|
|
26
|
-
message: isArrayType || isChoiceType
|
|
27
|
-
? `请选择${component.props?.label || '该字段'}`
|
|
28
|
-
: `请输入${component.props?.label || '该字段'}`,
|
|
29
|
-
trigger: isChoiceType || isArrayType ? 'change' : 'blur'
|
|
30
|
-
})
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// 数字范围验证
|
|
34
|
-
if (component.type === COMPONENT_TYPES.INPUT_NUMBER) {
|
|
35
|
-
if (component.min !== undefined) {
|
|
36
|
-
rules.push({
|
|
37
|
-
type: 'number',
|
|
38
|
-
min: component.min,
|
|
39
|
-
message: `数值不能小于 ${component.min}`,
|
|
40
|
-
trigger: 'blur'
|
|
41
|
-
})
|
|
42
|
-
}
|
|
43
|
-
if (component.max !== undefined) {
|
|
44
|
-
rules.push({
|
|
45
|
-
type: 'number',
|
|
46
|
-
max: component.max,
|
|
47
|
-
message: `数值不能大于 ${component.max}`,
|
|
48
|
-
trigger: 'blur'
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return rules.length > 0 ? rules : undefined
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 递归生成表单数据和验证规则
|
|
58
|
-
*/
|
|
59
|
-
const generateFormRules = (components) => {
|
|
60
|
-
const data = {}
|
|
61
|
-
const rules = {}
|
|
62
|
-
|
|
63
|
-
const processComponent = (component) => {
|
|
64
|
-
// 处理分栏组件
|
|
65
|
-
if (component.type === COMPONENT_TYPES.ROW) {
|
|
66
|
-
if (component.children) {
|
|
67
|
-
component.children.forEach(columnChildren => {
|
|
68
|
-
if (Array.isArray(columnChildren)) {
|
|
69
|
-
columnChildren.forEach(child => {
|
|
70
|
-
processComponent(child)
|
|
71
|
-
})
|
|
72
|
-
}
|
|
73
|
-
})
|
|
74
|
-
}
|
|
75
|
-
return
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// 处理普通组件
|
|
79
|
-
const fieldName = getFieldName(component)
|
|
80
|
-
const rule = generateComponentRule(component)
|
|
81
|
-
|
|
82
|
-
if (rule) {
|
|
83
|
-
rules[fieldName] = rule
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// 初始化数据值(优先使用默认值)
|
|
87
|
-
if (!(fieldName in data)) {
|
|
88
|
-
if (component.defaultValue !== undefined && component.defaultValue !== null) {
|
|
89
|
-
// 有默认值时使用默认值
|
|
90
|
-
if (component.type === COMPONENT_TYPES.DATE_PICKER) {
|
|
91
|
-
// 日期类型需要特殊处理
|
|
92
|
-
const defaultValue = component.defaultValue
|
|
93
|
-
if (defaultValue === 'today') {
|
|
94
|
-
data[fieldName] = new Date()
|
|
95
|
-
} else if (defaultValue === 'tomorrow') {
|
|
96
|
-
const tomorrow = new Date()
|
|
97
|
-
tomorrow.setDate(tomorrow.getDate() + 1)
|
|
98
|
-
data[fieldName] = tomorrow
|
|
99
|
-
} else {
|
|
100
|
-
data[fieldName] = defaultValue
|
|
101
|
-
}
|
|
102
|
-
} else {
|
|
103
|
-
data[fieldName] = component.defaultValue
|
|
104
|
-
}
|
|
105
|
-
} else {
|
|
106
|
-
// 没有默认值时使用空值
|
|
107
|
-
if (component.type === COMPONENT_TYPES.INPUT_NUMBER) {
|
|
108
|
-
data[fieldName] = null
|
|
109
|
-
} else if (component.type === COMPONENT_TYPES.RADIO_GROUP
|
|
110
|
-
|| component.type === COMPONENT_TYPES.SELECT
|
|
111
|
-
|| component.type === COMPONENT_TYPES.TEXTAREA) {
|
|
112
|
-
data[fieldName] = ''
|
|
113
|
-
} else if (component.type === COMPONENT_TYPES.DATE_PICKER) {
|
|
114
|
-
data[fieldName] = null
|
|
115
|
-
} else if (component.type === COMPONENT_TYPES.CHECKBOX_GROUP) {
|
|
116
|
-
data[fieldName] = []
|
|
117
|
-
} else if (component.type === COMPONENT_TYPES.SWITCH) {
|
|
118
|
-
data[fieldName] = false
|
|
119
|
-
} else {
|
|
120
|
-
data[fieldName] = ''
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (Array.isArray(components)) {
|
|
127
|
-
components.forEach(component => {
|
|
128
|
-
processComponent(component)
|
|
129
|
-
})
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return { data, rules }
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* 验证表单
|
|
137
|
-
*/
|
|
138
|
-
const validateForm = async (formRef) => {
|
|
139
|
-
if (!formRef) {
|
|
140
|
-
throw new Error('表单引用不存在')
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return await formRef.validate()
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* 重置表单
|
|
148
|
-
*/
|
|
149
|
-
const resetForm = (formRef) => {
|
|
150
|
-
if (formRef) {
|
|
151
|
-
formRef.resetFields()
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* 获取表单数据
|
|
157
|
-
*/
|
|
158
|
-
const getFormData = (formData, components) => {
|
|
159
|
-
const result = {}
|
|
160
|
-
|
|
161
|
-
const processComponent = (component) => {
|
|
162
|
-
if (component.type === COMPONENT_TYPES.ROW) {
|
|
163
|
-
if (component.children) {
|
|
164
|
-
component.children.forEach(columnChildren => {
|
|
165
|
-
if (Array.isArray(columnChildren)) {
|
|
166
|
-
columnChildren.forEach(child => {
|
|
167
|
-
processComponent(child)
|
|
168
|
-
})
|
|
169
|
-
}
|
|
170
|
-
})
|
|
171
|
-
}
|
|
172
|
-
return
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const fieldName = getFieldName(component)
|
|
176
|
-
if (fieldName in formData) {
|
|
177
|
-
result[fieldName] = {
|
|
178
|
-
name: component.props?.label || '',
|
|
179
|
-
value: formData[fieldName],
|
|
180
|
-
type: component.type
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (Array.isArray(components)) {
|
|
186
|
-
components.forEach(component => {
|
|
187
|
-
processComponent(component)
|
|
188
|
-
})
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return result
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return {
|
|
195
|
-
generateFormRules,
|
|
196
|
-
validateForm,
|
|
197
|
-
resetForm,
|
|
198
|
-
getFormData
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 表单验证的组合式函数
|
|
3
|
+
*/
|
|
4
|
+
import { COMPONENT_TYPES } from '../../design/constants/componentTypes.js'
|
|
5
|
+
import { isEmpty, getFieldName } from '../../design/utils/componentUtils.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 生成表单验证规则
|
|
9
|
+
*/
|
|
10
|
+
export function useFormValidation() {
|
|
11
|
+
/**
|
|
12
|
+
* 为单个组件生成验证规则
|
|
13
|
+
*/
|
|
14
|
+
const generateComponentRule = (component) => {
|
|
15
|
+
const fieldName = getFieldName(component)
|
|
16
|
+
const rules = []
|
|
17
|
+
|
|
18
|
+
// 必填验证
|
|
19
|
+
if (component.required) {
|
|
20
|
+
const isArrayType = component.type === COMPONENT_TYPES.CHECKBOX_GROUP
|
|
21
|
+
const isChoiceType = component.type === COMPONENT_TYPES.RADIO_GROUP
|
|
22
|
+
|| component.type === COMPONENT_TYPES.SELECT
|
|
23
|
+
rules.push({
|
|
24
|
+
required: true,
|
|
25
|
+
...(isArrayType ? { type: 'array' } : {}),
|
|
26
|
+
message: isArrayType || isChoiceType
|
|
27
|
+
? `请选择${component.props?.label || '该字段'}`
|
|
28
|
+
: `请输入${component.props?.label || '该字段'}`,
|
|
29
|
+
trigger: isChoiceType || isArrayType ? 'change' : 'blur'
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 数字范围验证
|
|
34
|
+
if (component.type === COMPONENT_TYPES.INPUT_NUMBER) {
|
|
35
|
+
if (component.min !== undefined) {
|
|
36
|
+
rules.push({
|
|
37
|
+
type: 'number',
|
|
38
|
+
min: component.min,
|
|
39
|
+
message: `数值不能小于 ${component.min}`,
|
|
40
|
+
trigger: 'blur'
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
if (component.max !== undefined) {
|
|
44
|
+
rules.push({
|
|
45
|
+
type: 'number',
|
|
46
|
+
max: component.max,
|
|
47
|
+
message: `数值不能大于 ${component.max}`,
|
|
48
|
+
trigger: 'blur'
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return rules.length > 0 ? rules : undefined
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 递归生成表单数据和验证规则
|
|
58
|
+
*/
|
|
59
|
+
const generateFormRules = (components) => {
|
|
60
|
+
const data = {}
|
|
61
|
+
const rules = {}
|
|
62
|
+
|
|
63
|
+
const processComponent = (component) => {
|
|
64
|
+
// 处理分栏组件
|
|
65
|
+
if (component.type === COMPONENT_TYPES.ROW) {
|
|
66
|
+
if (component.children) {
|
|
67
|
+
component.children.forEach(columnChildren => {
|
|
68
|
+
if (Array.isArray(columnChildren)) {
|
|
69
|
+
columnChildren.forEach(child => {
|
|
70
|
+
processComponent(child)
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// 处理普通组件
|
|
79
|
+
const fieldName = getFieldName(component)
|
|
80
|
+
const rule = generateComponentRule(component)
|
|
81
|
+
|
|
82
|
+
if (rule) {
|
|
83
|
+
rules[fieldName] = rule
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 初始化数据值(优先使用默认值)
|
|
87
|
+
if (!(fieldName in data)) {
|
|
88
|
+
if (component.defaultValue !== undefined && component.defaultValue !== null) {
|
|
89
|
+
// 有默认值时使用默认值
|
|
90
|
+
if (component.type === COMPONENT_TYPES.DATE_PICKER) {
|
|
91
|
+
// 日期类型需要特殊处理
|
|
92
|
+
const defaultValue = component.defaultValue
|
|
93
|
+
if (defaultValue === 'today') {
|
|
94
|
+
data[fieldName] = new Date()
|
|
95
|
+
} else if (defaultValue === 'tomorrow') {
|
|
96
|
+
const tomorrow = new Date()
|
|
97
|
+
tomorrow.setDate(tomorrow.getDate() + 1)
|
|
98
|
+
data[fieldName] = tomorrow
|
|
99
|
+
} else {
|
|
100
|
+
data[fieldName] = defaultValue
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
data[fieldName] = component.defaultValue
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
// 没有默认值时使用空值
|
|
107
|
+
if (component.type === COMPONENT_TYPES.INPUT_NUMBER) {
|
|
108
|
+
data[fieldName] = null
|
|
109
|
+
} else if (component.type === COMPONENT_TYPES.RADIO_GROUP
|
|
110
|
+
|| component.type === COMPONENT_TYPES.SELECT
|
|
111
|
+
|| component.type === COMPONENT_TYPES.TEXTAREA) {
|
|
112
|
+
data[fieldName] = ''
|
|
113
|
+
} else if (component.type === COMPONENT_TYPES.DATE_PICKER) {
|
|
114
|
+
data[fieldName] = null
|
|
115
|
+
} else if (component.type === COMPONENT_TYPES.CHECKBOX_GROUP) {
|
|
116
|
+
data[fieldName] = []
|
|
117
|
+
} else if (component.type === COMPONENT_TYPES.SWITCH) {
|
|
118
|
+
data[fieldName] = false
|
|
119
|
+
} else {
|
|
120
|
+
data[fieldName] = ''
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (Array.isArray(components)) {
|
|
127
|
+
components.forEach(component => {
|
|
128
|
+
processComponent(component)
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return { data, rules }
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 验证表单
|
|
137
|
+
*/
|
|
138
|
+
const validateForm = async (formRef) => {
|
|
139
|
+
if (!formRef) {
|
|
140
|
+
throw new Error('表单引用不存在')
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return await formRef.validate()
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 重置表单
|
|
148
|
+
*/
|
|
149
|
+
const resetForm = (formRef) => {
|
|
150
|
+
if (formRef) {
|
|
151
|
+
formRef.resetFields()
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* 获取表单数据
|
|
157
|
+
*/
|
|
158
|
+
const getFormData = (formData, components) => {
|
|
159
|
+
const result = {}
|
|
160
|
+
|
|
161
|
+
const processComponent = (component) => {
|
|
162
|
+
if (component.type === COMPONENT_TYPES.ROW) {
|
|
163
|
+
if (component.children) {
|
|
164
|
+
component.children.forEach(columnChildren => {
|
|
165
|
+
if (Array.isArray(columnChildren)) {
|
|
166
|
+
columnChildren.forEach(child => {
|
|
167
|
+
processComponent(child)
|
|
168
|
+
})
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
return
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const fieldName = getFieldName(component)
|
|
176
|
+
if (fieldName in formData) {
|
|
177
|
+
result[fieldName] = {
|
|
178
|
+
name: component.props?.label || '',
|
|
179
|
+
value: formData[fieldName],
|
|
180
|
+
type: component.type
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (Array.isArray(components)) {
|
|
186
|
+
components.forEach(component => {
|
|
187
|
+
processComponent(component)
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return result
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
generateFormRules,
|
|
196
|
+
validateForm,
|
|
197
|
+
resetForm,
|
|
198
|
+
getFormData
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|