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,216 +1,216 @@
1
- <template>
2
- <div class="form-view-container">
3
- <div class="form-view-content">
4
- <el-form
5
- ref="formRef"
6
- :model="formData"
7
- :rules="formRules"
8
- :disabled="readonly"
9
- label-width="120px"
10
- v-if="formConfig && formConfig.length > 0"
11
- class="form-wrapper"
12
- >
13
- <div
14
- v-for="(component, index) in formConfig"
15
- :key="index"
16
- class="form-item-wrapper"
17
- :class="{ 'is-row': component.type === COMPONENT_TYPES.ROW }"
18
- :style="getItemStyle(component)"
19
- :data-width="component.style?.width"
20
- >
21
- <FormField
22
- :component="component"
23
- :form-data="formData"
24
- :form-rules="formRules"
25
- :readonly="readonly"
26
- @update:value="handleValueUpdate"
27
- />
28
- </div>
29
- </el-form>
30
-
31
- <div v-else class="empty-state">
32
- <el-empty description="暂无表单配置,请先设计表单" />
33
- </div>
34
- </div>
35
-
36
- <div v-if="showResult" class="form-result">
37
- <el-divider>提交结果</el-divider>
38
- <pre>{{ JSON.stringify(formData, null, 2) }}</pre>
39
- </div>
40
- </div>
41
- </template>
42
-
43
- <script setup>
44
-
45
- defineOptions({ name: 'GbFormViewer' })
46
- import { ref, reactive, computed, watch } from 'vue'
47
- import { ElMessage } from 'element-plus'
48
- import FormField from './FormField.vue'
49
- import { COMPONENT_TYPES } from '../design/constants/componentTypes.js'
50
- import { useFormValidation } from './composables/useFormValidation.js'
51
-
52
- const props = defineProps({
53
- formConfig: {
54
- type: Array,
55
- default: () => []
56
- },
57
- showActions: {
58
- type: Boolean,
59
- default: true
60
- },
61
- readonly: {
62
- type: Boolean,
63
- default: false
64
- }
65
- })
66
-
67
- const formRef = ref(null)
68
- const formData = reactive({})
69
- const formRules = reactive({})
70
- const showResult = ref(false)
71
-
72
- const { generateFormRules, validateForm } = useFormValidation()
73
-
74
- // 获取表单项样式
75
- const getItemStyle = (component) => {
76
- if (component.type === COMPONENT_TYPES.ROW) {
77
- return { width: '100%' }
78
- }
79
-
80
- if (component.style?.width) {
81
- const width = component.style.width
82
- let widthValue = width
83
-
84
- // 如果是百分比,直接使用
85
- if (typeof width === 'string' && width.includes('%')) {
86
- widthValue = width
87
- }
88
- // 如果是数字,转换为百分比
89
- else if (typeof width === 'number') {
90
- widthValue = `${width}%`
91
- }
92
-
93
- return {
94
- width: widthValue,
95
- '--item-width': widthValue
96
- }
97
- }
98
-
99
- return { width: '100%' }
100
- }
101
-
102
- // 初始化表单数据和验证规则
103
- const initializeForm = () => {
104
- // 清空现有数据
105
- Object.keys(formData).forEach(key => delete formData[key])
106
- Object.keys(formRules).forEach(key => delete formRules[key])
107
-
108
- // 生成表单数据和验证规则
109
- const { data, rules } = generateFormRules(props.formConfig)
110
- Object.assign(formData, data)
111
- Object.assign(formRules, rules)
112
- }
113
-
114
- // 监听表单配置变化
115
- watch(() => props.formConfig, () => {
116
- initializeForm()
117
- }, { immediate: true, deep: true })
118
-
119
- // 处理值更新
120
- const handleValueUpdate = (field, value) => {
121
- formData[field] = value
122
- }
123
-
124
-
125
- // 暴露给外部使用
126
- defineExpose({
127
- formData,
128
- formRef,
129
- formRules
130
- })
131
- </script>
132
-
133
- <style scoped lang="scss">
134
- .form-view-container {
135
- height: 100%;
136
- display: flex;
137
- flex-direction: column;
138
- background-color: #fff;
139
- }
140
-
141
- .form-view-header {
142
- display: flex;
143
- justify-content: space-between;
144
- align-items: center;
145
- border-bottom: 1px solid #e4e7ed;
146
-
147
- h2 {
148
- margin: 0;
149
- font-size: 20px;
150
- font-weight: 500;
151
- }
152
- }
153
-
154
- .form-actions {
155
- display: flex;
156
- gap: 10px;
157
- }
158
-
159
- .form-view-content {
160
- flex: 1;
161
- overflow-y: auto;
162
- }
163
-
164
- .form-wrapper {
165
- display: flex;
166
- flex-wrap: wrap;
167
- gap: 10px;
168
- align-items: flex-start;
169
- }
170
-
171
- .form-item-wrapper {
172
- box-sizing: border-box;
173
- min-width: 0;
174
- &.is-row {
175
- width: 100%;
176
- flex: 0 0 100%;
177
- }
178
-
179
- // 当没有设置宽度时,默认占满整行
180
- &:not(.is-row):not([style*="width"]) {
181
- width: 100%;
182
- flex: 0 0 100%;
183
- }
184
-
185
- // 处理有宽度设置的组件,使用 calc 减去 gap 的一半,确保能并排显示
186
- &:not(.is-row)[style*="--item-width"] {
187
- flex: 0 0 calc(var(--item-width) - 5px);
188
- max-width: var(--item-width);
189
- }
190
- }
191
-
192
- .empty-state {
193
- display: flex;
194
- justify-content: center;
195
- align-items: center;
196
- height: 400px;
197
- }
198
-
199
- .form-result {
200
- margin-top: 20px;
201
- padding: 20px;
202
- background-color: #f5f7fa;
203
- border-radius: 4px;
204
-
205
- pre {
206
- margin: 0;
207
- padding: 15px;
208
- background-color: #fff;
209
- border-radius: 4px;
210
- overflow-x: auto;
211
- font-size: 12px;
212
- line-height: 1.6;
213
- }
214
- }
215
- </style>
216
-
1
+ <template>
2
+ <div class="form-view-container">
3
+ <div class="form-view-content">
4
+ <el-form
5
+ ref="formRef"
6
+ :model="formData"
7
+ :rules="formRules"
8
+ :disabled="readonly"
9
+ label-width="120px"
10
+ v-if="formConfig && formConfig.length > 0"
11
+ class="form-wrapper"
12
+ >
13
+ <div
14
+ v-for="(component, index) in formConfig"
15
+ :key="index"
16
+ class="form-item-wrapper"
17
+ :class="{ 'is-row': component.type === COMPONENT_TYPES.ROW }"
18
+ :style="getItemStyle(component)"
19
+ :data-width="component.style?.width"
20
+ >
21
+ <FormField
22
+ :component="component"
23
+ :form-data="formData"
24
+ :form-rules="formRules"
25
+ :readonly="readonly"
26
+ @update:value="handleValueUpdate"
27
+ />
28
+ </div>
29
+ </el-form>
30
+
31
+ <div v-else class="empty-state">
32
+ <el-empty description="暂无表单配置,请先设计表单" />
33
+ </div>
34
+ </div>
35
+
36
+ <div v-if="showResult" class="form-result">
37
+ <el-divider>提交结果</el-divider>
38
+ <pre>{{ JSON.stringify(formData, null, 2) }}</pre>
39
+ </div>
40
+ </div>
41
+ </template>
42
+
43
+ <script setup>
44
+
45
+ defineOptions({ name: 'GbFormViewer' })
46
+ import { ref, reactive, computed, watch } from 'vue'
47
+ import { ElMessage } from 'element-plus'
48
+ import FormField from './FormField.vue'
49
+ import { COMPONENT_TYPES } from '../design/constants/componentTypes.js'
50
+ import { useFormValidation } from './composables/useFormValidation.js'
51
+
52
+ const props = defineProps({
53
+ formConfig: {
54
+ type: Array,
55
+ default: () => []
56
+ },
57
+ showActions: {
58
+ type: Boolean,
59
+ default: true
60
+ },
61
+ readonly: {
62
+ type: Boolean,
63
+ default: false
64
+ }
65
+ })
66
+
67
+ const formRef = ref(null)
68
+ const formData = reactive({})
69
+ const formRules = reactive({})
70
+ const showResult = ref(false)
71
+
72
+ const { generateFormRules, validateForm } = useFormValidation()
73
+
74
+ // 获取表单项样式
75
+ const getItemStyle = (component) => {
76
+ if (component.type === COMPONENT_TYPES.ROW) {
77
+ return { width: '100%' }
78
+ }
79
+
80
+ if (component.style?.width) {
81
+ const width = component.style.width
82
+ let widthValue = width
83
+
84
+ // 如果是百分比,直接使用
85
+ if (typeof width === 'string' && width.includes('%')) {
86
+ widthValue = width
87
+ }
88
+ // 如果是数字,转换为百分比
89
+ else if (typeof width === 'number') {
90
+ widthValue = `${width}%`
91
+ }
92
+
93
+ return {
94
+ width: widthValue,
95
+ '--item-width': widthValue
96
+ }
97
+ }
98
+
99
+ return { width: '100%' }
100
+ }
101
+
102
+ // 初始化表单数据和验证规则
103
+ const initializeForm = () => {
104
+ // 清空现有数据
105
+ Object.keys(formData).forEach(key => delete formData[key])
106
+ Object.keys(formRules).forEach(key => delete formRules[key])
107
+
108
+ // 生成表单数据和验证规则
109
+ const { data, rules } = generateFormRules(props.formConfig)
110
+ Object.assign(formData, data)
111
+ Object.assign(formRules, rules)
112
+ }
113
+
114
+ // 监听表单配置变化
115
+ watch(() => props.formConfig, () => {
116
+ initializeForm()
117
+ }, { immediate: true, deep: true })
118
+
119
+ // 处理值更新
120
+ const handleValueUpdate = (field, value) => {
121
+ formData[field] = value
122
+ }
123
+
124
+
125
+ // 暴露给外部使用
126
+ defineExpose({
127
+ formData,
128
+ formRef,
129
+ formRules
130
+ })
131
+ </script>
132
+
133
+ <style scoped lang="scss">
134
+ .form-view-container {
135
+ height: 100%;
136
+ display: flex;
137
+ flex-direction: column;
138
+ background-color: #fff;
139
+ }
140
+
141
+ .form-view-header {
142
+ display: flex;
143
+ justify-content: space-between;
144
+ align-items: center;
145
+ border-bottom: 1px solid #e4e7ed;
146
+
147
+ h2 {
148
+ margin: 0;
149
+ font-size: 20px;
150
+ font-weight: 500;
151
+ }
152
+ }
153
+
154
+ .form-actions {
155
+ display: flex;
156
+ gap: 10px;
157
+ }
158
+
159
+ .form-view-content {
160
+ flex: 1;
161
+ overflow-y: auto;
162
+ }
163
+
164
+ .form-wrapper {
165
+ display: flex;
166
+ flex-wrap: wrap;
167
+ gap: 10px;
168
+ align-items: flex-start;
169
+ }
170
+
171
+ .form-item-wrapper {
172
+ box-sizing: border-box;
173
+ min-width: 0;
174
+ &.is-row {
175
+ width: 100%;
176
+ flex: 0 0 100%;
177
+ }
178
+
179
+ // 当没有设置宽度时,默认占满整行
180
+ &:not(.is-row):not([style*="width"]) {
181
+ width: 100%;
182
+ flex: 0 0 100%;
183
+ }
184
+
185
+ // 处理有宽度设置的组件,使用 calc 减去 gap 的一半,确保能并排显示
186
+ &:not(.is-row)[style*="--item-width"] {
187
+ flex: 0 0 calc(var(--item-width) - 5px);
188
+ max-width: var(--item-width);
189
+ }
190
+ }
191
+
192
+ .empty-state {
193
+ display: flex;
194
+ justify-content: center;
195
+ align-items: center;
196
+ height: 400px;
197
+ }
198
+
199
+ .form-result {
200
+ margin-top: 20px;
201
+ padding: 20px;
202
+ background-color: #f5f7fa;
203
+ border-radius: 4px;
204
+
205
+ pre {
206
+ margin: 0;
207
+ padding: 15px;
208
+ background-color: #fff;
209
+ border-radius: 4px;
210
+ overflow-x: auto;
211
+ font-size: 12px;
212
+ line-height: 1.6;
213
+ }
214
+ }
215
+ </style>
216
+