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.
@@ -1,328 +1,328 @@
1
- /**
2
- * FormVariable - 表单变量标准类
3
- *
4
- * 这个类负责将表单组件配置转换为标准的变量对象
5
- * 自动计算数据类型、提取元数据、标准化默认值
6
- *
7
- * @author Yudi Team
8
- * @version 3.0.0
9
- */
10
-
11
- import {
12
- computeDataType,
13
- getTypeDefaultValue,
14
- isValidValueForType,
15
- BaseDataType,
16
- ComponentType
17
- } from './formVariableTypeSystem.js'
18
-
19
- /**
20
- * FormVariable类
21
- *
22
- * 标准化的表单变量对象,包含所有条件判断所需的信息
23
- * 统一格式:使用 props.label 和 props.variableName
24
- */
25
- export class FormVariable {
26
- /**
27
- * 构造函数
28
- *
29
- * @param {Object} component - 表单组件配置(新格式)
30
- */
31
- constructor(component) {
32
- if (!component) {
33
- throw new Error('FormVariable: component参数不能为空')
34
- }
35
-
36
- // 统一格式:使用 props.variableName 和 props.label
37
- const variableName = component.props?.variableName
38
- if (!variableName) {
39
- throw new Error('FormVariable: 组件必须有props.variableName属性')
40
- }
41
-
42
- // 基本信息
43
- this.id = component.id
44
- this.variableName = variableName
45
- this.label = component.props?.label || variableName
46
-
47
- // 组件类型
48
- this.componentType = component.type
49
-
50
- // 自动计算数据类型(核心功能)
51
- this.dataType = computeDataType(component)
52
-
53
- // 提取元数据
54
- this.metadata = this._extractMetadata(component)
55
-
56
- // 标准化默认值
57
- this.defaultValue = this._normalizeDefaultValue(component)
58
-
59
- // 保留原始组件引用(用于特殊情况)
60
- this.component = component
61
- }
62
-
63
- /**
64
- * 提取组件元数据
65
- *
66
- * @private
67
- * @param {Object} component - 组件配置
68
- * @returns {Object} 元数据对象
69
- */
70
- _extractMetadata(component) {
71
- const props = component.props || {}
72
- const metadata = {
73
- placeholder: props.placeholder || '',
74
- required: component.required !== false // 默认为true
75
- }
76
-
77
- // 根据组件类型提取特定元数据
78
- switch (component.type) {
79
- case ComponentType.RADIO_GROUP:
80
- // 选项列表
81
- metadata.options = Array.isArray(props.options) ? props.options : []
82
- break
83
-
84
- case ComponentType.INPUT_NUMBER:
85
- // 数字输入的配置
86
- metadata.min = component.min != null ? Number(component.min) : undefined
87
- metadata.max = component.max != null ? Number(component.max) : undefined
88
- break
89
-
90
- case ComponentType.DATE_PICKER:
91
- // 日期格式(新格式默认为日期)
92
- metadata.dateFormat = 'date'
93
- break
94
- }
95
-
96
- return metadata
97
- }
98
-
99
- /**
100
- * 标准化默认值
101
- *
102
- * @private
103
- * @param {Object} component - 组件配置
104
- * @returns {any} 标准化后的默认值
105
- */
106
- _normalizeDefaultValue(component) {
107
- const rawValue = component.props?.defaultValue
108
-
109
- // 如果没有默认值,返回类型默认值
110
- if (rawValue === null || rawValue === undefined || rawValue === '') {
111
- return getTypeDefaultValue(this.dataType)
112
- }
113
-
114
- // 验证并转换默认值
115
- if (!isValidValueForType(rawValue, this.dataType)) {
116
- console.warn(
117
- `FormVariable: 默认值 "${rawValue}" 不符合类型 "${this.dataType}",使用类型默认值`,
118
- component
119
- )
120
- return getTypeDefaultValue(this.dataType)
121
- }
122
-
123
- // 根据数据类型进行必要的转换
124
- return this._convertValue(rawValue, this.dataType)
125
- }
126
-
127
- /**
128
- * 转换值到正确的类型
129
- *
130
- * @private
131
- * @param {any} value - 原始值
132
- * @param {string} dataType - 目标数据类型
133
- * @returns {any} 转换后的值
134
- */
135
- _convertValue(value, dataType) {
136
- if (value === null || value === undefined || value === '') {
137
- return getTypeDefaultValue(dataType)
138
- }
139
-
140
- switch (dataType) {
141
- case BaseDataType.INTEGER:
142
- return parseInt(value, 10)
143
-
144
- case BaseDataType.DECIMAL:
145
- return parseFloat(value)
146
-
147
- case BaseDataType.ARRAY:
148
- return Array.isArray(value) ? value : []
149
-
150
- case BaseDataType.BOOLEAN:
151
- return Boolean(value)
152
-
153
- case BaseDataType.STRING:
154
- case BaseDataType.DATE:
155
- case BaseDataType.DATETIME:
156
- default:
157
- return String(value)
158
- }
159
- }
160
-
161
- /**
162
- * 获取用于条件判断的完整信息
163
- *
164
- * @returns {Object} 条件判断所需的信息
165
- */
166
- getConditionInfo() {
167
- return {
168
- variableName: this.variableName,
169
- label: this.label,
170
- dataType: this.dataType,
171
- componentType: this.componentType,
172
- options: this.metadata.options || null,
173
- dateFormat: this.metadata.dateFormat || null,
174
- isInteger: this.dataType === BaseDataType.INTEGER
175
- }
176
- }
177
-
178
- /**
179
- * 判断是否有选项列表
180
- *
181
- * @returns {boolean} 是否有选项
182
- */
183
- hasOptions() {
184
- return Array.isArray(this.metadata.options) && this.metadata.options.length > 0
185
- }
186
-
187
- /**
188
- * 获取选项列表
189
- *
190
- * @returns {Array} 选项列表
191
- */
192
- getOptions() {
193
- return this.metadata.options || []
194
- }
195
-
196
- /**
197
- * 判断是否为数字类型
198
- *
199
- * @returns {boolean} 是否为数字类型
200
- */
201
- isNumeric() {
202
- return this.dataType === BaseDataType.INTEGER || this.dataType === BaseDataType.DECIMAL
203
- }
204
-
205
- /**
206
- * 判断是否为日期类型
207
- *
208
- * @returns {boolean} 是否为日期类型
209
- */
210
- isDate() {
211
- return this.dataType === BaseDataType.DATE || this.dataType === BaseDataType.DATETIME
212
- }
213
-
214
- /**
215
- * 判断是否为整数类型
216
- *
217
- * @returns {boolean} 是否为整数类型
218
- */
219
- isInteger() {
220
- return this.dataType === BaseDataType.INTEGER
221
- }
222
-
223
- /**
224
- * 获取日期格式
225
- *
226
- * @returns {string|null} 日期格式('date' 或 'datetime')
227
- */
228
- getDateFormat() {
229
- return this.metadata.dateFormat || null
230
- }
231
-
232
- /**
233
- * 转换为简单对象(用于序列化)
234
- *
235
- * @returns {Object} 简单对象
236
- */
237
- toJSON() {
238
- return {
239
- id: this.id,
240
- variableName: this.variableName,
241
- label: this.label,
242
- componentType: this.componentType,
243
- dataType: this.dataType,
244
- metadata: this.metadata,
245
- defaultValue: this.defaultValue
246
- }
247
- }
248
-
249
- /**
250
- * 从简单对象创建FormVariable实例
251
- *
252
- * @static
253
- * @param {Object} json - 简单对象
254
- * @returns {FormVariable} FormVariable实例
255
- */
256
- static fromJSON(json) {
257
- // 重建组件配置(新格式)
258
- const component = {
259
- id: json.id,
260
- type: json.componentType,
261
- field: json.variableName,
262
- name: json.label,
263
- props: {
264
- ...json.metadata
265
- }
266
- }
267
-
268
- return new FormVariable(component)
269
- }
270
-
271
- /**
272
- * 批量转换组件列表为FormVariable数组(新格式)
273
- *
274
- * @static
275
- * @param {Array} components - 组件配置数组(新格式)
276
- * @returns {Array<FormVariable>} FormVariable数组
277
- */
278
- static fromComponents(components) {
279
- if (!Array.isArray(components)) {
280
- console.warn('FormVariable.fromComponents: components必须是数组')
281
- return []
282
- }
283
-
284
- return components
285
- .filter(component => {
286
- // 过滤掉无效的组件
287
- if (!component) return false
288
-
289
- // 统一格式:必须有 props.variableName 属性
290
- if (!component.props?.variableName) return false
291
-
292
- // 过滤掉布局组件
293
- if (component.type === ComponentType.ROW) return false
294
- return true
295
- })
296
- .map(component => {
297
- try {
298
- return new FormVariable(component)
299
- } catch (error) {
300
- console.error('FormVariable.fromComponents: 创建FormVariable失败', error, component)
301
- return null
302
- }
303
- })
304
- .filter(v => v !== null)
305
- }
306
-
307
- /**
308
- * 验证FormVariable对象的有效性
309
- *
310
- * @static
311
- * @param {any} obj - 要验证的对象
312
- * @returns {boolean} 是否为有效的FormVariable
313
- */
314
- static isValid(obj) {
315
- if (!obj) return false
316
-
317
- return (
318
- typeof obj.variableName === 'string' &&
319
- typeof obj.label === 'string' &&
320
- typeof obj.componentType === 'string' &&
321
- typeof obj.dataType === 'string' &&
322
- typeof obj.metadata === 'object'
323
- )
324
- }
325
- }
326
-
327
- export default FormVariable
328
-
1
+ /**
2
+ * FormVariable - 表单变量标准类
3
+ *
4
+ * 这个类负责将表单组件配置转换为标准的变量对象
5
+ * 自动计算数据类型、提取元数据、标准化默认值
6
+ *
7
+ * @author Yudi Team
8
+ * @version 3.0.0
9
+ */
10
+
11
+ import {
12
+ computeDataType,
13
+ getTypeDefaultValue,
14
+ isValidValueForType,
15
+ BaseDataType,
16
+ ComponentType
17
+ } from './formVariableTypeSystem.js'
18
+
19
+ /**
20
+ * FormVariable类
21
+ *
22
+ * 标准化的表单变量对象,包含所有条件判断所需的信息
23
+ * 统一格式:使用 props.label 和 props.variableName
24
+ */
25
+ export class FormVariable {
26
+ /**
27
+ * 构造函数
28
+ *
29
+ * @param {Object} component - 表单组件配置(新格式)
30
+ */
31
+ constructor(component) {
32
+ if (!component) {
33
+ throw new Error('FormVariable: component参数不能为空')
34
+ }
35
+
36
+ // 统一格式:使用 props.variableName 和 props.label
37
+ const variableName = component.props?.variableName
38
+ if (!variableName) {
39
+ throw new Error('FormVariable: 组件必须有props.variableName属性')
40
+ }
41
+
42
+ // 基本信息
43
+ this.id = component.id
44
+ this.variableName = variableName
45
+ this.label = component.props?.label || variableName
46
+
47
+ // 组件类型
48
+ this.componentType = component.type
49
+
50
+ // 自动计算数据类型(核心功能)
51
+ this.dataType = computeDataType(component)
52
+
53
+ // 提取元数据
54
+ this.metadata = this._extractMetadata(component)
55
+
56
+ // 标准化默认值
57
+ this.defaultValue = this._normalizeDefaultValue(component)
58
+
59
+ // 保留原始组件引用(用于特殊情况)
60
+ this.component = component
61
+ }
62
+
63
+ /**
64
+ * 提取组件元数据
65
+ *
66
+ * @private
67
+ * @param {Object} component - 组件配置
68
+ * @returns {Object} 元数据对象
69
+ */
70
+ _extractMetadata(component) {
71
+ const props = component.props || {}
72
+ const metadata = {
73
+ placeholder: props.placeholder || '',
74
+ required: component.required !== false // 默认为true
75
+ }
76
+
77
+ // 根据组件类型提取特定元数据
78
+ switch (component.type) {
79
+ case ComponentType.RADIO_GROUP:
80
+ // 选项列表
81
+ metadata.options = Array.isArray(props.options) ? props.options : []
82
+ break
83
+
84
+ case ComponentType.INPUT_NUMBER:
85
+ // 数字输入的配置
86
+ metadata.min = component.min != null ? Number(component.min) : undefined
87
+ metadata.max = component.max != null ? Number(component.max) : undefined
88
+ break
89
+
90
+ case ComponentType.DATE_PICKER:
91
+ // 日期格式(新格式默认为日期)
92
+ metadata.dateFormat = 'date'
93
+ break
94
+ }
95
+
96
+ return metadata
97
+ }
98
+
99
+ /**
100
+ * 标准化默认值
101
+ *
102
+ * @private
103
+ * @param {Object} component - 组件配置
104
+ * @returns {any} 标准化后的默认值
105
+ */
106
+ _normalizeDefaultValue(component) {
107
+ const rawValue = component.props?.defaultValue
108
+
109
+ // 如果没有默认值,返回类型默认值
110
+ if (rawValue === null || rawValue === undefined || rawValue === '') {
111
+ return getTypeDefaultValue(this.dataType)
112
+ }
113
+
114
+ // 验证并转换默认值
115
+ if (!isValidValueForType(rawValue, this.dataType)) {
116
+ console.warn(
117
+ `FormVariable: 默认值 "${rawValue}" 不符合类型 "${this.dataType}",使用类型默认值`,
118
+ component
119
+ )
120
+ return getTypeDefaultValue(this.dataType)
121
+ }
122
+
123
+ // 根据数据类型进行必要的转换
124
+ return this._convertValue(rawValue, this.dataType)
125
+ }
126
+
127
+ /**
128
+ * 转换值到正确的类型
129
+ *
130
+ * @private
131
+ * @param {any} value - 原始值
132
+ * @param {string} dataType - 目标数据类型
133
+ * @returns {any} 转换后的值
134
+ */
135
+ _convertValue(value, dataType) {
136
+ if (value === null || value === undefined || value === '') {
137
+ return getTypeDefaultValue(dataType)
138
+ }
139
+
140
+ switch (dataType) {
141
+ case BaseDataType.INTEGER:
142
+ return parseInt(value, 10)
143
+
144
+ case BaseDataType.DECIMAL:
145
+ return parseFloat(value)
146
+
147
+ case BaseDataType.ARRAY:
148
+ return Array.isArray(value) ? value : []
149
+
150
+ case BaseDataType.BOOLEAN:
151
+ return Boolean(value)
152
+
153
+ case BaseDataType.STRING:
154
+ case BaseDataType.DATE:
155
+ case BaseDataType.DATETIME:
156
+ default:
157
+ return String(value)
158
+ }
159
+ }
160
+
161
+ /**
162
+ * 获取用于条件判断的完整信息
163
+ *
164
+ * @returns {Object} 条件判断所需的信息
165
+ */
166
+ getConditionInfo() {
167
+ return {
168
+ variableName: this.variableName,
169
+ label: this.label,
170
+ dataType: this.dataType,
171
+ componentType: this.componentType,
172
+ options: this.metadata.options || null,
173
+ dateFormat: this.metadata.dateFormat || null,
174
+ isInteger: this.dataType === BaseDataType.INTEGER
175
+ }
176
+ }
177
+
178
+ /**
179
+ * 判断是否有选项列表
180
+ *
181
+ * @returns {boolean} 是否有选项
182
+ */
183
+ hasOptions() {
184
+ return Array.isArray(this.metadata.options) && this.metadata.options.length > 0
185
+ }
186
+
187
+ /**
188
+ * 获取选项列表
189
+ *
190
+ * @returns {Array} 选项列表
191
+ */
192
+ getOptions() {
193
+ return this.metadata.options || []
194
+ }
195
+
196
+ /**
197
+ * 判断是否为数字类型
198
+ *
199
+ * @returns {boolean} 是否为数字类型
200
+ */
201
+ isNumeric() {
202
+ return this.dataType === BaseDataType.INTEGER || this.dataType === BaseDataType.DECIMAL
203
+ }
204
+
205
+ /**
206
+ * 判断是否为日期类型
207
+ *
208
+ * @returns {boolean} 是否为日期类型
209
+ */
210
+ isDate() {
211
+ return this.dataType === BaseDataType.DATE || this.dataType === BaseDataType.DATETIME
212
+ }
213
+
214
+ /**
215
+ * 判断是否为整数类型
216
+ *
217
+ * @returns {boolean} 是否为整数类型
218
+ */
219
+ isInteger() {
220
+ return this.dataType === BaseDataType.INTEGER
221
+ }
222
+
223
+ /**
224
+ * 获取日期格式
225
+ *
226
+ * @returns {string|null} 日期格式('date' 或 'datetime')
227
+ */
228
+ getDateFormat() {
229
+ return this.metadata.dateFormat || null
230
+ }
231
+
232
+ /**
233
+ * 转换为简单对象(用于序列化)
234
+ *
235
+ * @returns {Object} 简单对象
236
+ */
237
+ toJSON() {
238
+ return {
239
+ id: this.id,
240
+ variableName: this.variableName,
241
+ label: this.label,
242
+ componentType: this.componentType,
243
+ dataType: this.dataType,
244
+ metadata: this.metadata,
245
+ defaultValue: this.defaultValue
246
+ }
247
+ }
248
+
249
+ /**
250
+ * 从简单对象创建FormVariable实例
251
+ *
252
+ * @static
253
+ * @param {Object} json - 简单对象
254
+ * @returns {FormVariable} FormVariable实例
255
+ */
256
+ static fromJSON(json) {
257
+ // 重建组件配置(新格式)
258
+ const component = {
259
+ id: json.id,
260
+ type: json.componentType,
261
+ field: json.variableName,
262
+ name: json.label,
263
+ props: {
264
+ ...json.metadata
265
+ }
266
+ }
267
+
268
+ return new FormVariable(component)
269
+ }
270
+
271
+ /**
272
+ * 批量转换组件列表为FormVariable数组(新格式)
273
+ *
274
+ * @static
275
+ * @param {Array} components - 组件配置数组(新格式)
276
+ * @returns {Array<FormVariable>} FormVariable数组
277
+ */
278
+ static fromComponents(components) {
279
+ if (!Array.isArray(components)) {
280
+ console.warn('FormVariable.fromComponents: components必须是数组')
281
+ return []
282
+ }
283
+
284
+ return components
285
+ .filter(component => {
286
+ // 过滤掉无效的组件
287
+ if (!component) return false
288
+
289
+ // 统一格式:必须有 props.variableName 属性
290
+ if (!component.props?.variableName) return false
291
+
292
+ // 过滤掉布局组件
293
+ if (component.type === ComponentType.ROW) return false
294
+ return true
295
+ })
296
+ .map(component => {
297
+ try {
298
+ return new FormVariable(component)
299
+ } catch (error) {
300
+ console.error('FormVariable.fromComponents: 创建FormVariable失败', error, component)
301
+ return null
302
+ }
303
+ })
304
+ .filter(v => v !== null)
305
+ }
306
+
307
+ /**
308
+ * 验证FormVariable对象的有效性
309
+ *
310
+ * @static
311
+ * @param {any} obj - 要验证的对象
312
+ * @returns {boolean} 是否为有效的FormVariable
313
+ */
314
+ static isValid(obj) {
315
+ if (!obj) return false
316
+
317
+ return (
318
+ typeof obj.variableName === 'string' &&
319
+ typeof obj.label === 'string' &&
320
+ typeof obj.componentType === 'string' &&
321
+ typeof obj.dataType === 'string' &&
322
+ typeof obj.metadata === 'object'
323
+ )
324
+ }
325
+ }
326
+
327
+ export default FormVariable
328
+