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,5 +1,5 @@
1
- /* Form designer styles: fc icon font + designer shell (MIT, form-create-designer)
2
- and the config item styles adapted from its ConfigItem component.
3
- 配置项样式在 src/index.js 里紧跟本文件单独引入,保证它排在 fc 外壳之后(覆盖顺序不能变)。 */
4
- @import './fc/icon.css';
1
+ /* Form designer styles: fc icon font + designer shell (MIT, form-create-designer)
2
+ and the config item styles adapted from its ConfigItem component.
3
+ 配置项样式在 src/index.js 里紧跟本文件单独引入,保证它排在 fc 外壳之后(覆盖顺序不能变)。 */
4
+ @import './fc/icon.css';
5
5
  @import './fc/designer.css';
@@ -1,301 +1,301 @@
1
- <template>
2
- <div class="form-viewer">
3
- <!-- 分栏组件 -->
4
- <div v-if="component.type === COMPONENT_TYPES.ROW" class="row-container">
5
- <el-row :gutter="component.props?.gutter || 20">
6
- <el-col
7
- v-for="(column, colIndex) in (component.props?.columns || [])"
8
- :key="colIndex"
9
- :span="column.span || 12"
10
- >
11
- <div class="column-wrapper">
12
- <FormField
13
- v-for="(child, childIndex) in (component.children?.[colIndex] || [])"
14
- :key="childIndex"
15
- :component="child"
16
- :form-data="formData"
17
- :form-rules="formRules"
18
- :readonly="readonly"
19
- />
20
- </div>
21
- </el-col>
22
- </el-row>
23
- </div>
24
-
25
- <!-- 文本输入框 -->
26
- <el-form-item
27
- v-else-if="component.type === COMPONENT_TYPES.INPUT"
28
- :label="component.props?.label || ''"
29
- :prop="getFieldName(component)"
30
- :required="component.required"
31
- >
32
- <el-input
33
- v-model="currentValue"
34
- :placeholder="component.props?.placeholder || '请输入文本'"
35
- style="width: 100%"
36
- :disabled="readonly"
37
- @input="handleInput"
38
- />
39
- </el-form-item>
40
-
41
- <!-- 多行文本 -->
42
- <el-form-item
43
- v-else-if="component.type === COMPONENT_TYPES.TEXTAREA"
44
- :label="component.props?.label || ''"
45
- :prop="getFieldName(component)"
46
- :required="component.required"
47
- >
48
- <el-input
49
- v-model="currentValue"
50
- type="textarea"
51
- :rows="component.props?.rows || 3"
52
- :placeholder="component.props?.placeholder || '请输入内容'"
53
- style="width: 100%"
54
- :disabled="readonly"
55
- @input="handleInput"
56
- />
57
- </el-form-item>
58
-
59
- <!-- 数字输入框 -->
60
- <el-form-item
61
- v-else-if="component.type === COMPONENT_TYPES.INPUT_NUMBER"
62
- :label="component.props?.label || ''"
63
- :prop="getFieldName(component)"
64
- :required="component.required"
65
- >
66
- <el-input-number
67
- v-model="currentValue"
68
- :min="component.min || 1"
69
- :max="component.max || 100"
70
- style="width: 100%"
71
- :disabled="readonly"
72
- @change="handleInput"
73
- />
74
- </el-form-item>
75
-
76
- <!-- 单选框 -->
77
- <el-form-item
78
- v-else-if="component.type === COMPONENT_TYPES.RADIO_GROUP"
79
- :label="component.props?.label || ''"
80
- :prop="getFieldName(component)"
81
- :required="component.required"
82
- >
83
- <el-radio-group
84
- v-model="currentValue"
85
- style="width: 100%"
86
- :disabled="readonly"
87
- @change="handleInput"
88
- >
89
- <el-radio
90
- v-for="option in (component.props?.options || ['选项1', '选项2', '选项3'])"
91
- :key="option.value || option"
92
- :value="option.value || option"
93
- >
94
- {{ option.label || option }}
95
- </el-radio>
96
- </el-radio-group>
97
- </el-form-item>
98
-
99
- <!-- 下拉选择 -->
100
- <el-form-item
101
- v-else-if="component.type === COMPONENT_TYPES.SELECT"
102
- :label="component.props?.label || ''"
103
- :prop="getFieldName(component)"
104
- :required="component.required"
105
- >
106
- <el-select
107
- v-model="currentValue"
108
- :placeholder="component.props?.placeholder || '请选择'"
109
- style="width: 100%"
110
- :disabled="readonly"
111
- @change="handleInput"
112
- >
113
- <el-option
114
- v-for="option in (component.props?.options || [])"
115
- :key="option.value || option"
116
- :label="option.label || option"
117
- :value="option.value || option"
118
- />
119
- </el-select>
120
- </el-form-item>
121
-
122
- <!-- 多选框 -->
123
- <el-form-item
124
- v-else-if="component.type === COMPONENT_TYPES.CHECKBOX_GROUP"
125
- :label="component.props?.label || ''"
126
- :prop="getFieldName(component)"
127
- :required="component.required"
128
- >
129
- <el-checkbox-group
130
- v-model="currentValue"
131
- style="width: 100%"
132
- :disabled="readonly"
133
- @change="handleInput"
134
- >
135
- <el-checkbox
136
- v-for="option in (component.props?.options || [])"
137
- :key="option.value || option"
138
- :value="option.value || option"
139
- >
140
- {{ option.label || option }}
141
- </el-checkbox>
142
- </el-checkbox-group>
143
- </el-form-item>
144
-
145
- <!-- 开关 -->
146
- <el-form-item
147
- v-else-if="component.type === COMPONENT_TYPES.SWITCH"
148
- :label="component.props?.label || ''"
149
- :prop="getFieldName(component)"
150
- :required="component.required"
151
- >
152
- <el-switch
153
- v-model="currentValue"
154
- :active-text="component.props?.activeText || ''"
155
- :inactive-text="component.props?.inactiveText || ''"
156
- :disabled="readonly"
157
- @change="handleInput"
158
- />
159
- </el-form-item>
160
-
161
- <!-- 日期选择器 -->
162
- <el-form-item
163
- v-else-if="component.type === COMPONENT_TYPES.DATE_PICKER"
164
- :label="component.props?.label || ''"
165
- :prop="getFieldName(component)"
166
- :required="component.required"
167
- >
168
- <el-date-picker
169
- v-model="currentValue"
170
- type="date"
171
- :placeholder="component.props?.placeholder || '选择日期'"
172
- style="width: 100%"
173
- :disabled="readonly"
174
- @change="handleInput"
175
- />
176
- </el-form-item>
177
- </div>
178
- </template>
179
-
180
- <script setup>
181
-
182
- defineOptions({ name: 'FormField' })
183
-
184
- import { computed, watch, onMounted } from 'vue'
185
- import { COMPONENT_TYPES } from '../design/constants/componentTypes.js'
186
- import { getDefaultValue } from '../design/composables/useDefaultValue.js'
187
- import { normalizeNumberValue, isEmpty, getFieldName } from '../design/utils/componentUtils.js'
188
-
189
- const props = defineProps({
190
- component: {
191
- type: Object,
192
- required: true
193
- },
194
- formData: {
195
- type: Object,
196
- required: true
197
- },
198
- formRules: {
199
- type: Object,
200
- required: true
201
- },
202
- readonly: {
203
- type: Boolean,
204
- default: false
205
- }
206
- })
207
-
208
- const emit = defineEmits(['update:value'])
209
-
210
- const fieldName = computed(() => getFieldName(props.component))
211
-
212
- // 初始化表单数据
213
- onMounted(() => {
214
- if (!(fieldName.value in props.formData)) {
215
- // 应用默认值
216
- const defaultValue = getDefaultValue(props.component)
217
- if (defaultValue !== null && defaultValue !== undefined) {
218
- props.formData[fieldName.value] = defaultValue
219
- } else {
220
- // 根据组件类型设置初始值
221
- if (props.component.type === COMPONENT_TYPES.INPUT_NUMBER) {
222
- props.formData[fieldName.value] = null
223
- } else if (props.component.type === COMPONENT_TYPES.RADIO_GROUP
224
- || props.component.type === COMPONENT_TYPES.SELECT
225
- || props.component.type === COMPONENT_TYPES.TEXTAREA) {
226
- props.formData[fieldName.value] = ''
227
- } else if (props.component.type === COMPONENT_TYPES.DATE_PICKER) {
228
- props.formData[fieldName.value] = null
229
- } else if (props.component.type === COMPONENT_TYPES.CHECKBOX_GROUP) {
230
- props.formData[fieldName.value] = []
231
- } else if (props.component.type === COMPONENT_TYPES.SWITCH) {
232
- props.formData[fieldName.value] = false
233
- } else {
234
- props.formData[fieldName.value] = ''
235
- }
236
- }
237
- }
238
- })
239
-
240
- // 当前值
241
- const currentValue = computed({
242
- get() {
243
- let value = props.formData[fieldName.value]
244
-
245
- if (props.component.type === COMPONENT_TYPES.INPUT_NUMBER) {
246
- return normalizeNumberValue(value)
247
- }
248
-
249
- if (isEmpty(value) && props.component.defaultValue !== undefined && props.component.defaultValue !== null) {
250
- const defaultValue = getDefaultValue(props.component)
251
- if (defaultValue !== null && defaultValue !== undefined) {
252
- return defaultValue
253
- }
254
- }
255
-
256
- return value
257
- },
258
- set(value) {
259
- if (props.component.type === COMPONENT_TYPES.INPUT_NUMBER) {
260
- props.formData[fieldName.value] = normalizeNumberValue(value)
261
- } else if (props.component.type === COMPONENT_TYPES.CHECKBOX_GROUP) {
262
- props.formData[fieldName.value] = Array.isArray(value) ? [...value] : []
263
- } else if (props.component.type === COMPONENT_TYPES.SWITCH) {
264
- props.formData[fieldName.value] = value === true
265
- } else {
266
- props.formData[fieldName.value] = value
267
- }
268
- emit('update:value', fieldName.value, props.formData[fieldName.value])
269
- }
270
- })
271
-
272
- // 处理输入
273
- const handleInput = (value) => {
274
- emit('update:value', fieldName.value, value)
275
- }
276
- </script>
277
-
278
- <style scoped lang="scss">
279
- .form-viewer {
280
- margin-top: 10px;
281
- width: 100%;
282
- }
283
-
284
- // 确保 el-form-item 不会破坏 flex 布局
285
- :deep(.el-form-item) {
286
- margin-bottom: 0;
287
- width: 100%;
288
- }
289
-
290
- .row-container {
291
- width: 100%;
292
- margin-bottom: 20px;
293
- }
294
-
295
- .column-wrapper {
296
- display: flex;
297
- flex-direction: column;
298
- gap: 20px;
299
- }
300
- </style>
301
-
1
+ <template>
2
+ <div class="form-viewer">
3
+ <!-- 分栏组件 -->
4
+ <div v-if="component.type === COMPONENT_TYPES.ROW" class="row-container">
5
+ <el-row :gutter="component.props?.gutter || 20">
6
+ <el-col
7
+ v-for="(column, colIndex) in (component.props?.columns || [])"
8
+ :key="colIndex"
9
+ :span="column.span || 12"
10
+ >
11
+ <div class="column-wrapper">
12
+ <FormField
13
+ v-for="(child, childIndex) in (component.children?.[colIndex] || [])"
14
+ :key="childIndex"
15
+ :component="child"
16
+ :form-data="formData"
17
+ :form-rules="formRules"
18
+ :readonly="readonly"
19
+ />
20
+ </div>
21
+ </el-col>
22
+ </el-row>
23
+ </div>
24
+
25
+ <!-- 文本输入框 -->
26
+ <el-form-item
27
+ v-else-if="component.type === COMPONENT_TYPES.INPUT"
28
+ :label="component.props?.label || ''"
29
+ :prop="getFieldName(component)"
30
+ :required="component.required"
31
+ >
32
+ <el-input
33
+ v-model="currentValue"
34
+ :placeholder="component.props?.placeholder || '请输入文本'"
35
+ style="width: 100%"
36
+ :disabled="readonly"
37
+ @input="handleInput"
38
+ />
39
+ </el-form-item>
40
+
41
+ <!-- 多行文本 -->
42
+ <el-form-item
43
+ v-else-if="component.type === COMPONENT_TYPES.TEXTAREA"
44
+ :label="component.props?.label || ''"
45
+ :prop="getFieldName(component)"
46
+ :required="component.required"
47
+ >
48
+ <el-input
49
+ v-model="currentValue"
50
+ type="textarea"
51
+ :rows="component.props?.rows || 3"
52
+ :placeholder="component.props?.placeholder || '请输入内容'"
53
+ style="width: 100%"
54
+ :disabled="readonly"
55
+ @input="handleInput"
56
+ />
57
+ </el-form-item>
58
+
59
+ <!-- 数字输入框 -->
60
+ <el-form-item
61
+ v-else-if="component.type === COMPONENT_TYPES.INPUT_NUMBER"
62
+ :label="component.props?.label || ''"
63
+ :prop="getFieldName(component)"
64
+ :required="component.required"
65
+ >
66
+ <el-input-number
67
+ v-model="currentValue"
68
+ :min="component.min || 1"
69
+ :max="component.max || 100"
70
+ style="width: 100%"
71
+ :disabled="readonly"
72
+ @change="handleInput"
73
+ />
74
+ </el-form-item>
75
+
76
+ <!-- 单选框 -->
77
+ <el-form-item
78
+ v-else-if="component.type === COMPONENT_TYPES.RADIO_GROUP"
79
+ :label="component.props?.label || ''"
80
+ :prop="getFieldName(component)"
81
+ :required="component.required"
82
+ >
83
+ <el-radio-group
84
+ v-model="currentValue"
85
+ style="width: 100%"
86
+ :disabled="readonly"
87
+ @change="handleInput"
88
+ >
89
+ <el-radio
90
+ v-for="option in (component.props?.options || ['选项1', '选项2', '选项3'])"
91
+ :key="option.value || option"
92
+ :value="option.value || option"
93
+ >
94
+ {{ option.label || option }}
95
+ </el-radio>
96
+ </el-radio-group>
97
+ </el-form-item>
98
+
99
+ <!-- 下拉选择 -->
100
+ <el-form-item
101
+ v-else-if="component.type === COMPONENT_TYPES.SELECT"
102
+ :label="component.props?.label || ''"
103
+ :prop="getFieldName(component)"
104
+ :required="component.required"
105
+ >
106
+ <el-select
107
+ v-model="currentValue"
108
+ :placeholder="component.props?.placeholder || '请选择'"
109
+ style="width: 100%"
110
+ :disabled="readonly"
111
+ @change="handleInput"
112
+ >
113
+ <el-option
114
+ v-for="option in (component.props?.options || [])"
115
+ :key="option.value || option"
116
+ :label="option.label || option"
117
+ :value="option.value || option"
118
+ />
119
+ </el-select>
120
+ </el-form-item>
121
+
122
+ <!-- 多选框 -->
123
+ <el-form-item
124
+ v-else-if="component.type === COMPONENT_TYPES.CHECKBOX_GROUP"
125
+ :label="component.props?.label || ''"
126
+ :prop="getFieldName(component)"
127
+ :required="component.required"
128
+ >
129
+ <el-checkbox-group
130
+ v-model="currentValue"
131
+ style="width: 100%"
132
+ :disabled="readonly"
133
+ @change="handleInput"
134
+ >
135
+ <el-checkbox
136
+ v-for="option in (component.props?.options || [])"
137
+ :key="option.value || option"
138
+ :value="option.value || option"
139
+ >
140
+ {{ option.label || option }}
141
+ </el-checkbox>
142
+ </el-checkbox-group>
143
+ </el-form-item>
144
+
145
+ <!-- 开关 -->
146
+ <el-form-item
147
+ v-else-if="component.type === COMPONENT_TYPES.SWITCH"
148
+ :label="component.props?.label || ''"
149
+ :prop="getFieldName(component)"
150
+ :required="component.required"
151
+ >
152
+ <el-switch
153
+ v-model="currentValue"
154
+ :active-text="component.props?.activeText || ''"
155
+ :inactive-text="component.props?.inactiveText || ''"
156
+ :disabled="readonly"
157
+ @change="handleInput"
158
+ />
159
+ </el-form-item>
160
+
161
+ <!-- 日期选择器 -->
162
+ <el-form-item
163
+ v-else-if="component.type === COMPONENT_TYPES.DATE_PICKER"
164
+ :label="component.props?.label || ''"
165
+ :prop="getFieldName(component)"
166
+ :required="component.required"
167
+ >
168
+ <el-date-picker
169
+ v-model="currentValue"
170
+ type="date"
171
+ :placeholder="component.props?.placeholder || '选择日期'"
172
+ style="width: 100%"
173
+ :disabled="readonly"
174
+ @change="handleInput"
175
+ />
176
+ </el-form-item>
177
+ </div>
178
+ </template>
179
+
180
+ <script setup>
181
+
182
+ defineOptions({ name: 'FormField' })
183
+
184
+ import { computed, watch, onMounted } from 'vue'
185
+ import { COMPONENT_TYPES } from '../design/constants/componentTypes.js'
186
+ import { getDefaultValue } from '../design/composables/useDefaultValue.js'
187
+ import { normalizeNumberValue, isEmpty, getFieldName } from '../design/utils/componentUtils.js'
188
+
189
+ const props = defineProps({
190
+ component: {
191
+ type: Object,
192
+ required: true
193
+ },
194
+ formData: {
195
+ type: Object,
196
+ required: true
197
+ },
198
+ formRules: {
199
+ type: Object,
200
+ required: true
201
+ },
202
+ readonly: {
203
+ type: Boolean,
204
+ default: false
205
+ }
206
+ })
207
+
208
+ const emit = defineEmits(['update:value'])
209
+
210
+ const fieldName = computed(() => getFieldName(props.component))
211
+
212
+ // 初始化表单数据
213
+ onMounted(() => {
214
+ if (!(fieldName.value in props.formData)) {
215
+ // 应用默认值
216
+ const defaultValue = getDefaultValue(props.component)
217
+ if (defaultValue !== null && defaultValue !== undefined) {
218
+ props.formData[fieldName.value] = defaultValue
219
+ } else {
220
+ // 根据组件类型设置初始值
221
+ if (props.component.type === COMPONENT_TYPES.INPUT_NUMBER) {
222
+ props.formData[fieldName.value] = null
223
+ } else if (props.component.type === COMPONENT_TYPES.RADIO_GROUP
224
+ || props.component.type === COMPONENT_TYPES.SELECT
225
+ || props.component.type === COMPONENT_TYPES.TEXTAREA) {
226
+ props.formData[fieldName.value] = ''
227
+ } else if (props.component.type === COMPONENT_TYPES.DATE_PICKER) {
228
+ props.formData[fieldName.value] = null
229
+ } else if (props.component.type === COMPONENT_TYPES.CHECKBOX_GROUP) {
230
+ props.formData[fieldName.value] = []
231
+ } else if (props.component.type === COMPONENT_TYPES.SWITCH) {
232
+ props.formData[fieldName.value] = false
233
+ } else {
234
+ props.formData[fieldName.value] = ''
235
+ }
236
+ }
237
+ }
238
+ })
239
+
240
+ // 当前值
241
+ const currentValue = computed({
242
+ get() {
243
+ let value = props.formData[fieldName.value]
244
+
245
+ if (props.component.type === COMPONENT_TYPES.INPUT_NUMBER) {
246
+ return normalizeNumberValue(value)
247
+ }
248
+
249
+ if (isEmpty(value) && props.component.defaultValue !== undefined && props.component.defaultValue !== null) {
250
+ const defaultValue = getDefaultValue(props.component)
251
+ if (defaultValue !== null && defaultValue !== undefined) {
252
+ return defaultValue
253
+ }
254
+ }
255
+
256
+ return value
257
+ },
258
+ set(value) {
259
+ if (props.component.type === COMPONENT_TYPES.INPUT_NUMBER) {
260
+ props.formData[fieldName.value] = normalizeNumberValue(value)
261
+ } else if (props.component.type === COMPONENT_TYPES.CHECKBOX_GROUP) {
262
+ props.formData[fieldName.value] = Array.isArray(value) ? [...value] : []
263
+ } else if (props.component.type === COMPONENT_TYPES.SWITCH) {
264
+ props.formData[fieldName.value] = value === true
265
+ } else {
266
+ props.formData[fieldName.value] = value
267
+ }
268
+ emit('update:value', fieldName.value, props.formData[fieldName.value])
269
+ }
270
+ })
271
+
272
+ // 处理输入
273
+ const handleInput = (value) => {
274
+ emit('update:value', fieldName.value, value)
275
+ }
276
+ </script>
277
+
278
+ <style scoped lang="scss">
279
+ .form-viewer {
280
+ margin-top: 10px;
281
+ width: 100%;
282
+ }
283
+
284
+ // 确保 el-form-item 不会破坏 flex 布局
285
+ :deep(.el-form-item) {
286
+ margin-bottom: 0;
287
+ width: 100%;
288
+ }
289
+
290
+ .row-container {
291
+ width: 100%;
292
+ margin-bottom: 20px;
293
+ }
294
+
295
+ .column-wrapper {
296
+ display: flex;
297
+ flex-direction: column;
298
+ gap: 20px;
299
+ }
300
+ </style>
301
+