af-mobile-client-vue3 1.2.40 → 1.2.42

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "af-mobile-client-vue3",
3
3
  "type": "module",
4
- "version": "1.2.40",
4
+ "version": "1.2.42",
5
5
  "packageManager": "pnpm@10.12.3",
6
6
  "description": "Vue + Vite component lib",
7
7
  "engines": {
@@ -108,6 +108,6 @@
108
108
  "commit-msg": "pnpm commitlint $1"
109
109
  },
110
110
  "lint-staged": {
111
- "*": "eslint --fix"
111
+ "*.{js,ts,vue}": "eslint --fix"
112
112
  }
113
113
  }
@@ -210,15 +210,16 @@ function initComponent() {
210
210
  }
211
211
  configContent.value = result
212
212
  // 扁平化 type=group 的 groupItems,保持顺序
213
- const flatFormJson = []
214
- result.formJson.forEach((item) => {
215
- if (item.type === 'group' && Array.isArray(item.groupItems)) {
216
- flatFormJson.push(...item.groupItems)
217
- }
218
- else {
219
- flatFormJson.push(item)
220
- }
221
- })
213
+ let flatFormJson = []
214
+ flatFormJson = result.formJson.filter(item => (!item.isOnlyAddOrEdit && item.type !== 'group') || (item.type === 'group' && item.groupItems.some(groupItem => !groupItem.isOnlyAddOrEdit)))
215
+ // result.formJson.forEach((item) => {
216
+ // if (item.type === 'group' && Array.isArray(item.groupItems)) {
217
+ // flatFormJson.push(...item.groupItems)
218
+ // }
219
+ // else {
220
+ // flatFormJson.push(item)
221
+ // }
222
+ // })
222
223
  formQueryList.value = flatFormJson
223
224
  console.log('formQueryList', formQueryList.value)
224
225
  if (result.buttonState) {
@@ -0,0 +1,122 @@
1
+ <script setup lang="ts">
2
+ import { Cell as VanCell, CellGroup as VanCellGroup, Field as VanField, Switch as VanSwitch } from 'vant'
3
+ import { defineExpose, defineProps, onMounted, onUnmounted, ref } from 'vue'
4
+
5
+ interface DeviceFormProps {
6
+ setRef?: (refValue: any) => void
7
+ removeRef?: (refValue: any) => void
8
+ formGroupName?: string
9
+ formData?: any
10
+ }
11
+
12
+ const props = withDefaults(defineProps<DeviceFormProps>(), {
13
+ setRef: () => {},
14
+ removeRef: () => {},
15
+ formGroupName: 'deviceInfo',
16
+ formData: () => ({}),
17
+ })
18
+
19
+ const formData = ref({
20
+ deviceId: '',
21
+ deviceName: '',
22
+ deviceType: '',
23
+ status: 'active',
24
+ isOnline: false,
25
+ ...props.formData,
26
+ })
27
+
28
+ async function validate() {
29
+ if (!formData.value.deviceId) {
30
+ throw new Error('设备ID不能为空')
31
+ }
32
+ if (!formData.value.deviceName) {
33
+ throw new Error('设备名称不能为空')
34
+ }
35
+ return Promise.resolve()
36
+ }
37
+
38
+ function getFormData() {
39
+ return {
40
+ deviceId: formData.value.deviceId,
41
+ deviceName: formData.value.deviceName,
42
+ deviceType: formData.value.deviceType,
43
+ status: formData.value.status,
44
+ isOnline: formData.value.isOnline,
45
+ }
46
+ }
47
+
48
+ function resetForm() {
49
+ formData.value = {
50
+ deviceId: '',
51
+ deviceName: '',
52
+ deviceType: '',
53
+ status: 'active',
54
+ isOnline: false,
55
+ }
56
+ }
57
+
58
+ const exposeObj = {
59
+ validate,
60
+ getFormData,
61
+ resetForm,
62
+ formGroupName: props.formGroupName,
63
+ }
64
+
65
+ defineExpose(exposeObj)
66
+
67
+ onMounted(() => {
68
+ props.setRef(exposeObj)
69
+ })
70
+
71
+ onUnmounted(() => {
72
+ props.removeRef(exposeObj)
73
+ })
74
+ </script>
75
+
76
+ <template>
77
+ <div class="device-form">
78
+ <VanCellGroup inset>
79
+ <VanField
80
+ v-model="formData.deviceId"
81
+ label="设备ID"
82
+ placeholder="请输入设备ID"
83
+ :rules="[{ required: true, message: '请输入设备ID' }]"
84
+ />
85
+ <VanField
86
+ v-model="formData.deviceName"
87
+ label="设备名称"
88
+ placeholder="请输入设备名称"
89
+ :rules="[{ required: true, message: '请输入设备名称' }]"
90
+ />
91
+ <VanField
92
+ v-model="formData.deviceType"
93
+ label="设备类型"
94
+ placeholder="请输入设备类型"
95
+ />
96
+ <VanCell title="设备状态">
97
+ <template #value>
98
+ <span :class="formData.status === 'active' ? 'text-success' : 'text-warning'">
99
+ {{ formData.status === 'active' ? '正常' : '异常' }}
100
+ </span>
101
+ </template>
102
+ </VanCell>
103
+ <VanCell title="在线状态">
104
+ <template #value>
105
+ <VanSwitch v-model="formData.isOnline" />
106
+ </template>
107
+ </VanCell>
108
+ </VanCellGroup>
109
+ </div>
110
+ </template>
111
+
112
+ <style scoped lang="less">
113
+ .device-form {
114
+ padding: 16px;
115
+ .text-success {
116
+ color: #07c160;
117
+ }
118
+ .text-warning {
119
+ color: #ff976a;
120
+ }
121
+ }
122
+ </style>
@@ -0,0 +1,56 @@
1
+ <script setup lang="ts">
2
+ import XFormGroup from '@af-mobile-client-vue3/components/data/XFormGroup/index.vue'
3
+ import NormalDataLayout from '@af-mobile-client-vue3/components/layout/NormalDataLayout/index.vue'
4
+ import { showToast } from 'vant'
5
+ import { ref } from 'vue'
6
+ import DeviceForm from './DeviceForm.vue'
7
+ import UserForm from './UserForm.vue'
8
+
9
+ const configName = ref('complexFormGroup')
10
+ const serviceName = ref('af-apply')
11
+ const formData = ref({
12
+ userInfo: {},
13
+ deviceInfo: {},
14
+ })
15
+
16
+ const formGroup = ref()
17
+
18
+ function handleSubmit(result: any) {
19
+ console.log('提交数据:', result)
20
+ showToast('提交成功')
21
+ }
22
+ </script>
23
+
24
+ <template>
25
+ <NormalDataLayout title="表单组示例">
26
+ <template #layout_content>
27
+ <XFormGroup
28
+ ref="formGroup"
29
+ :config-name="configName"
30
+ :service-name="serviceName"
31
+ :group-form-data="formData"
32
+ mode="新增"
33
+ @submit="handleSubmit"
34
+ >
35
+ <!-- 用户信息插槽 -->
36
+ <template #user="{ setRef, removeRef, userFormData }">
37
+ <UserForm
38
+ :set-ref="setRef"
39
+ :remove-ref="removeRef"
40
+ form-group-name="userInfo"
41
+ :form-data="userFormData"
42
+ />
43
+ </template>
44
+ <!-- 设备信息插槽 -->
45
+ <template #device="{ setRef, removeRef, deviceFormData }">
46
+ <DeviceForm
47
+ :set-ref="setRef"
48
+ :remove-ref="removeRef"
49
+ form-group-name="deviceInfo"
50
+ :form-data="deviceFormData"
51
+ />
52
+ </template>
53
+ </XFormGroup>
54
+ </template>
55
+ </NormalDataLayout>
56
+ </template>
@@ -0,0 +1,273 @@
1
+ # XFormGroup 表单组组件
2
+
3
+ ## 组件概述
4
+
5
+ XFormGroup 是一个基于 Vue 3 + TypeScript + Vant 4 开发的表单组组件,支持多标签页表单展示,可以通过配置实现复杂的表单组合。组件支持两种模式:普通表单模式和插槽(slot)模式。
6
+
7
+ ## 功能特性
8
+
9
+ - 🏷️ **多标签页支持**:使用 Vant Tabs 组件实现标签页切换
10
+ - 🔧 **插槽模式**:支持通过 slot 自定义表单内容
11
+ - 📝 **表单验证**:支持表单数据验证和提交
12
+ - 🎯 **动态配置**:通过配置文件动态生成表单结构
13
+ - 🔄 **数据绑定**:支持表单数据的双向绑定
14
+ - 📱 **移动端优化**:基于 Vant 4 组件库,专为移动端设计
15
+
16
+ ## 基础用法
17
+
18
+ ```vue
19
+ // 见 FormGroupDemo.vue
20
+ ```
21
+
22
+ ## Props 参数详解
23
+
24
+ ### configName
25
+ - **类型**: `string`
26
+ - **默认值**: `''`
27
+ - **说明**: 表单配置名称,用于从后端获取表单配置信息
28
+ - **示例**: `'appapplyuserinfoFormGroup'`
29
+
30
+ ### serviceName
31
+ - **类型**: `string | undefined`
32
+ - **默认值**: `undefined`
33
+ - **说明**: 服务名称,用于指定后端服务
34
+ - **示例**: `'af-apply'`
35
+
36
+ ### groupFormData
37
+ - **类型**: `object`
38
+ - **默认值**: `() => ({})`
39
+ - **说明**: 表单组数据,用于初始化表单数据
40
+ - **示例**:
41
+
42
+ ```js
43
+ // -{
44
+ // - userInfo: { name: '张三', age: 25 },
45
+ // - deviceInfo: { deviceId: '001', status: 'active' }
46
+ // -}
47
+ ```
48
+
49
+ ### mode
50
+ - **类型**: `string`
51
+ - **默认值**: `'查询'`
52
+ - **说明**: `表单模式,控制表单的显示和操作模式`
53
+ - **可选值**: `'查询'` | `'新增'` | `'编辑'` | `'查看'`
54
+
55
+ ## 插槽(Slot)使用详解
56
+
57
+ ### 插槽模式配置
58
+
59
+ 当配置中的 `formGroupType` 为 `'slot'` 时,组件会渲染插槽内容而不是默认的 XForm 组件。
60
+
61
+ #### 插槽参数
62
+
63
+ 插槽会接收以下参数:
64
+
65
+ - **userItem/deviceItem**: 当前标签页的配置项
66
+ - **userFormData/deviceFormData**: 当前标签页对应的表单数据
67
+ - **setRef**: 注册表单实例的函数
68
+ - **removeRef**: 注销表单实例的函数
69
+
70
+ ### 完整使用示例
71
+
72
+ #### 1. 主页面组件
73
+
74
+ > 见 [FormGroupDemo.vue](FormGroupDemo.vue)
75
+
76
+ #### 2. 用户信息表单组件
77
+
78
+ > 见 [UserForm.vue](UserForm.vue)
79
+
80
+ #### 3. 设备信息表单组件
81
+
82
+ > 见 [DeviceForm.vue](DeviceForm.vue)
83
+
84
+ #### 4. 配置结构示例
85
+
86
+ ```json
87
+ {
88
+ "groups": [
89
+ {
90
+ "groupName": "userInfo",
91
+ "describe": "用户信息",
92
+ "tableName": "用户信息表",
93
+ "formGroupType": "slot",
94
+ "slotName": "user",
95
+ "showSubmitBtn": false
96
+ },
97
+ {
98
+ "groupName": "deviceInfo",
99
+ "describe": "设备信息",
100
+ "tableName": "设备信息表",
101
+ "formGroupType": "slot",
102
+ "slotName": "device",
103
+ "showSubmitBtn": true
104
+ }
105
+ ],
106
+ "showSubmitBtn": true
107
+ }
108
+ ```
109
+
110
+ #### 5. setRef 和 removeRef 工作原理
111
+
112
+ ```js
113
+ // XFormGroup 组件内部的实现逻辑
114
+ const xFormListRef = ref([])
115
+
116
+ function setRef(refValue) {
117
+ if (refValue && !xFormListRef.value.includes(refValue)) {
118
+ xFormListRef.value.push(refValue)
119
+ console.log('注册表单实例:', refValue.formGroupName)
120
+ }
121
+ }
122
+
123
+ function removeRef(refValue) {
124
+ const idx = xFormListRef.value.indexOf(refValue)
125
+ if (idx !== -1) {
126
+ xFormListRef.value.splice(idx, 1)
127
+ console.log('注销表单实例:', refValue.formGroupName)
128
+ }
129
+ }
130
+
131
+ async function submit() {
132
+ const allFormData = {}
133
+ for (const formInstance of xFormListRef.value) {
134
+ try {
135
+ await formInstance.validate()
136
+ if (formInstance.formGroupName && typeof formInstance.getFormData === 'function') {
137
+ allFormData[formInstance.formGroupName] = formInstance.getFormData()
138
+ }
139
+ } catch (error) {
140
+ console.error('表单验证失败:', error)
141
+ return
142
+ }
143
+ }
144
+ emit('submit', allFormData)
145
+ }
146
+ ```
147
+
148
+ ### 关键要点
149
+
150
+ 1. **必须实现的方法**:
151
+ - `validate()`: 表单验证方法
152
+ - `getFormData()`: 获取表单数据方法
153
+ - `formGroupName`: 表单组名称
154
+ 2. **生命周期管理**:
155
+ - 在 `onMounted` 时调用 `setRef` 注册
156
+ - 在 `onUnmounted` 时调用 `removeRef` 注销
157
+ 3. **数据传递**:
158
+ - 通过 `defineExpose` 暴露方法给父组件
159
+ - 通过 `props` 接收父组件传递的参数
160
+ 4. **插槽使用**:
161
+ - 插槽名称必须与配置中的 `slotName` 一致
162
+ - 必须传递 `setRef` 和 `removeRef` 给子组件
163
+
164
+ ## 事件
165
+
166
+ ### submit
167
+ - **参数**: `formData: object`
168
+ - **说明**: 表单提交事件,当用户点击提交按钮时触发
169
+ - **示例**:
170
+ ```javascript
171
+ function handleSubmit(formData) {
172
+ console.log('表单数据:', formData)
173
+ // 处理表单提交逻辑
174
+ }
175
+ ```
176
+
177
+ ## 方法
178
+
179
+ ### init(params: Form)
180
+ - **参数**: `params` - 初始化参数对象
181
+ - **说明**: 手动初始化组件,可以动态改变配置
182
+ - **示例**:
183
+ ```javascript
184
+ formGroup.value.init({
185
+ configName: 'newConfig',
186
+ serviceName: 'newService',
187
+ groupFormData: { ... },
188
+ mode: '新增'
189
+ })
190
+ ```
191
+
192
+ ### setRef(refValue)
193
+ - **参数**: `refValue` - 表单实例
194
+ - **说明**: 注册表单实例到组件中,用于统一管理和提交
195
+
196
+ ### removeRef(refValue)
197
+ - **参数**: `refValue` - 表单实例
198
+ - **说明**: 从组件中注销表单实例
199
+
200
+ ## 配置结构说明
201
+
202
+ ### 基础配置结构
203
+ ```javascript
204
+ {
205
+ groups: [
206
+ {
207
+ "groupName": "userInfo",
208
+ "describe": "用户信息",
209
+ "tableName": "用户信息表",
210
+ "formGroupType": "form", // 或 "slot"
211
+ "slotName": "user", // 当 formGroupType 为 "slot" 时使用
212
+ "showSubmitBtn": false
213
+ },
214
+ {
215
+ "groupName": "deviceInfo",
216
+ "describe": "设备信息",
217
+ "tableName": "设备信息表",
218
+ "formGroupType": "slot",
219
+ "slotName": "device",
220
+ "showSubmitBtn": true
221
+ }
222
+ ],
223
+ showSubmitBtn: true
224
+ }
225
+ ```
226
+
227
+ ### 配置项说明
228
+
229
+ | 字段名 | 类型 | 说明 |
230
+ |--------|------|------|
231
+ | groupName | string | 分组名称,用于数据分组 |
232
+ | describe | string | 标签页显示标题 |
233
+ | tableName | string | 表名,当 describe 为空时作为标题 |
234
+ | formGroupType | string | 表单类型,'form' 为普通表单,'slot' 为插槽 |
235
+ | slotName | string | 插槽名称,当 formGroupType 为 'slot' 时使用 |
236
+ | showSubmitBtn | boolean | 是否显示提交按钮 |
237
+
238
+ ## 样式定制
239
+
240
+ 组件使用 Less 预处理器,可以通过以下方式定制样式:
241
+
242
+ ```less
243
+ #x-form-group {
244
+ background-color: #f7f8fa;
245
+ padding-bottom: 10px;
246
+
247
+ .x-form-group-item {
248
+ margin: 20px 0;
249
+ }
250
+
251
+ // 自定义标签页样式
252
+ :deep(.van-tabs__nav) {
253
+ background-color: #fff;
254
+ }
255
+
256
+ // 自定义按钮样式
257
+ :deep(.van-button--primary) {
258
+ background-color: #1989fa;
259
+ }
260
+ }
261
+ ```
262
+
263
+ ## 注意事项
264
+
265
+ 1. **插槽注册**: 使用插槽模式时,子组件必须通过 `setRef` 和 `removeRef` 注册到父组件
266
+ 2. **数据同步**: 插槽组件需要实现 `getFormData` 方法返回表单数据
267
+ 3. **表单验证**: 插槽组件可以实现 `validate` 方法进行表单验证
268
+ 4. **生命周期**: 插槽组件需要在 `onMounted` 时注册,`onUnmounted` 时注销
269
+ 5. **配置加载**: 组件会在 `onBeforeMount` 时自动加载配置,也可以通过 `init` 方法手动初始化
270
+
271
+ ```
272
+
273
+ ## 这个文档详细介绍了 XFormGroup 组件的使用方法,特别是插槽的使用方式和各个参数的详细说明。开发者可以根据这个文档快速上手使用该组件。
@@ -0,0 +1,102 @@
1
+ <script setup lang="ts">
2
+ import { CellGroup as VanCellGroup, Field as VanField } from 'vant'
3
+ import { defineExpose, defineProps, onMounted, onUnmounted, ref } from 'vue'
4
+
5
+ interface UserFormProps {
6
+ setRef?: (refValue: any) => void
7
+ removeRef?: (refValue: any) => void
8
+ formGroupName?: string
9
+ formData?: any
10
+ }
11
+
12
+ const props = withDefaults(defineProps<UserFormProps>(), {
13
+ setRef: () => {},
14
+ removeRef: () => {},
15
+ formGroupName: 'userInfo',
16
+ formData: () => ({}),
17
+ })
18
+
19
+ const formData = ref({
20
+ name: '',
21
+ phone: '',
22
+ email: '',
23
+ ...props.formData,
24
+ })
25
+
26
+ async function validate() {
27
+ if (!formData.value.name) {
28
+ throw new Error('姓名不能为空')
29
+ }
30
+ if (!formData.value.phone) {
31
+ throw new Error('手机号不能为空')
32
+ }
33
+ const phoneRegex = /^1[3-9]\d{9}$/
34
+ if (!phoneRegex.test(formData.value.phone)) {
35
+ throw new Error('手机号格式不正确')
36
+ }
37
+ return Promise.resolve()
38
+ }
39
+
40
+ function getFormData() {
41
+ return {
42
+ name: formData.value.name,
43
+ phone: formData.value.phone,
44
+ email: formData.value.email,
45
+ }
46
+ }
47
+
48
+ function resetForm() {
49
+ formData.value = {
50
+ name: '',
51
+ phone: '',
52
+ email: '',
53
+ }
54
+ }
55
+
56
+ const exposeObj = {
57
+ validate,
58
+ getFormData,
59
+ resetForm,
60
+ formGroupName: props.formGroupName,
61
+ }
62
+
63
+ defineExpose(exposeObj)
64
+
65
+ onMounted(() => {
66
+ props.setRef(exposeObj)
67
+ })
68
+
69
+ onUnmounted(() => {
70
+ props.removeRef(exposeObj)
71
+ })
72
+ </script>
73
+
74
+ <template>
75
+ <div class="user-form">
76
+ <VanCellGroup inset>
77
+ <VanField
78
+ v-model="formData.name"
79
+ label="姓名"
80
+ placeholder="请输入姓名"
81
+ :rules="[{ required: true, message: '请输入姓名' }]"
82
+ />
83
+ <VanField
84
+ v-model="formData.phone"
85
+ label="手机号"
86
+ placeholder="请输入手机号"
87
+ :rules="[{ required: true, message: '请输入手机号' }]"
88
+ />
89
+ <VanField
90
+ v-model="formData.email"
91
+ label="邮箱"
92
+ placeholder="请输入邮箱"
93
+ />
94
+ </VanCellGroup>
95
+ </div>
96
+ </template>
97
+
98
+ <style scoped lang="less">
99
+ .user-form {
100
+ padding: 16px;
101
+ }
102
+ </style>