@longhongguo/form-create-ant-design-vue 3.3.9 → 3.3.10

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@longhongguo/form-create-ant-design-vue",
3
- "version": "3.3.09",
3
+ "version": "3.3.10",
4
4
  "description": "AntDesignVue版本低代码表单|FormCreate 是一个可以通过 JSON 生成具有动态渲染、数据收集、验证和提交功能的低代码表单生成组件。支持6个UI框架,适配移动端,并且支持生成任何 Vue 组件。内置20种常用表单组件和自定义组件,再复杂的表单都可以轻松搞定。",
5
5
  "main": "./dist/form-create.min.js",
6
6
  "module": "./dist/form-create.esm.js",
@@ -49,11 +49,17 @@ export default {
49
49
  )
50
50
  }
51
51
  } catch (e) {
52
- console.error('Failed to parse loadData:', e)
52
+ // 解析 loadData 失败,静默处理
53
53
  }
54
54
  }
55
55
 
56
+ // 保存原始的 parsedFn,用于回显时调用(需要在 if 块外定义)
57
+ let originalLoadDataFn = null
58
+
56
59
  if (is.Function(parsedFn)) {
60
+ // 保存原始的 parsedFn,用于回显时调用
61
+ originalLoadDataFn = parsedFn
62
+
57
63
  // 包装 loadData 函数
58
64
  props.loadData = function (selectedOptions) {
59
65
  if (!selectedOptions || selectedOptions.length === 0) return
@@ -75,27 +81,38 @@ export default {
75
81
 
76
82
  // 递归查找并更新选项的函数
77
83
  // 通过 value 路径来查找,避免引用问题
78
- const updateOptionRecursive = (optionsList, pathValues, depth, newChildren) => {
84
+ const updateOptionRecursive = (
85
+ optionsList,
86
+ pathValues,
87
+ depth,
88
+ newChildren
89
+ ) => {
79
90
  if (!pathValues || pathValues.length === 0) {
80
91
  // 如果没有路径,直接返回深拷贝的选项
81
- return optionsList.map(item => ({
92
+ return optionsList.map((item) => ({
82
93
  ...item,
83
- children: item.children ? item.children.map(child => ({ ...child })) : undefined
94
+ children: item.children
95
+ ? item.children.map((child) => ({ ...child }))
96
+ : undefined
84
97
  }))
85
98
  }
86
-
99
+
87
100
  const currentValue = pathValues[depth]
88
101
  const isLast = depth === pathValues.length - 1
89
-
90
- return optionsList.map(item => {
102
+
103
+ return optionsList.map((item) => {
91
104
  const newItem = { ...item }
92
-
105
+
93
106
  // 如果当前项匹配路径的当前层级值
94
- if ((item.value !== undefined && item.value === currentValue) ||
95
- (item.id !== undefined && item.id === currentValue)) {
107
+ if (
108
+ (item.value !== undefined && item.value === currentValue) ||
109
+ (item.id !== undefined && item.id === currentValue)
110
+ ) {
96
111
  if (isLast) {
97
112
  // 是最后一个值(目标项),更新 children
98
- newItem.children = newChildren ? [...newChildren] : undefined
113
+ newItem.children = newChildren
114
+ ? [...newChildren]
115
+ : undefined
99
116
  newItem.loading = false
100
117
  // 保持其他属性不变,但深拷贝 children
101
118
  return newItem
@@ -116,7 +133,7 @@ export default {
116
133
  return newItem
117
134
  }
118
135
  }
119
-
136
+
120
137
  // 不匹配的项,也需要递归处理其 children(以防路径在其他分支)
121
138
  if (item.children && item.children.length > 0) {
122
139
  newItem.children = updateOptionRecursive(
@@ -129,14 +146,14 @@ export default {
129
146
  // 如果没有 children,深拷贝基本结构
130
147
  newItem.children = undefined
131
148
  }
132
-
149
+
133
150
  return newItem
134
151
  })
135
152
  }
136
153
 
137
154
  // 从 selectedOptions 中提取 value 路径
138
- const pathValues = selectedOptions.map(opt => opt.value || opt.id)
139
-
155
+ const pathValues = selectedOptions.map((opt) => opt.value || opt.id)
156
+
140
157
  // 使用递归函数更新选项(从第0层开始)
141
158
  const newOptions = updateOptionRecursive(
142
159
  currentOptions,
@@ -206,6 +223,159 @@ export default {
206
223
  return result
207
224
  }
208
225
  }
226
+
227
+ // 处理回显时的数据加载 - 当 value 变化时触发(包括初始值和 setValue)
228
+ // 使用一个函数来处理,可以在多个地方调用
229
+ const triggerLoadDataForValue = (valueToLoad) => {
230
+ if (
231
+ !valueToLoad ||
232
+ !Array.isArray(valueToLoad) ||
233
+ valueToLoad.length <= 1 ||
234
+ !props.loadData ||
235
+ typeof props.loadData !== 'function'
236
+ ) {
237
+ return
238
+ }
239
+
240
+ // 延迟执行,等待 effect.fetch 完成第一级数据加载
241
+ const timerKey = `_loadDataTimer_${ctx.rule._fc_id || Date.now()}`
242
+ if (ctx.rule[timerKey]) {
243
+ clearTimeout(ctx.rule[timerKey])
244
+ }
245
+
246
+ ctx.rule[timerKey] = setTimeout(() => {
247
+ let retryCount = 0
248
+ const maxRetries = 50 // 最多重试50次,即5秒
249
+
250
+ const loadInitialData = async () => {
251
+ retryCount++
252
+ const fetchData = ctxRef.effectData('fetch')
253
+
254
+ // 如果超过最大重试次数,停止
255
+ if (retryCount > maxRetries) {
256
+ return
257
+ }
258
+
259
+ // 如果还在加载第一级数据,等待
260
+ if (fetchData && fetchData.loading === true) {
261
+ setTimeout(loadInitialData, 100)
262
+ return
263
+ }
264
+
265
+ let currentOptions = props.options || []
266
+
267
+ if (!currentOptions || currentOptions.length === 0) {
268
+ // 检查 fetchData 中是否有 options
269
+ if (fetchData && fetchData.props && fetchData.props.options) {
270
+ currentOptions = fetchData.props.options
271
+ props.options = currentOptions
272
+ } else {
273
+ setTimeout(loadInitialData, 100)
274
+ return
275
+ }
276
+ }
277
+
278
+ if (!currentOptions || currentOptions.length === 0) {
279
+ return
280
+ }
281
+
282
+ // 递归加载缺失的数据
283
+ const loadMissingData = async (pathValues, startDepth) => {
284
+ if (startDepth >= pathValues.length - 1) {
285
+ return
286
+ }
287
+
288
+ // 重新获取最新的 options
289
+ currentOptions = props.options || []
290
+
291
+ // 构建 selectedOptions(从根到当前层级)
292
+ const selectedOptions = []
293
+ let currentLevelOptions = currentOptions
294
+ for (let i = 0; i <= startDepth; i++) {
295
+ const val = pathValues[i]
296
+ const option = currentLevelOptions.find(
297
+ (opt) => opt.value === val || opt.id === val
298
+ )
299
+ if (option) {
300
+ selectedOptions.push(option)
301
+ currentLevelOptions = option.children || []
302
+ } else {
303
+ return
304
+ }
305
+ }
306
+
307
+ const currentOption = selectedOptions[startDepth]
308
+
309
+ // 如果已经是叶子节点,不需要加载
310
+ if (currentOption.isLeaf === true) {
311
+ return
312
+ }
313
+
314
+ // 检查是否需要加载子级数据
315
+ const nextValue = pathValues[startDepth + 1]
316
+ if (nextValue === undefined || nextValue === null) {
317
+ return
318
+ }
319
+
320
+ // 检查是否已有子级数据
321
+ const hasChildren =
322
+ currentOption.children &&
323
+ Array.isArray(currentOption.children) &&
324
+ currentOption.children.length > 0
325
+
326
+ if (!hasChildren) {
327
+ // 需要触发 loadData 加载
328
+ // 设置 loading 状态
329
+ currentOption.loading = true
330
+
331
+ try {
332
+ const result = props.loadData(selectedOptions)
333
+
334
+ // 如果返回 Promise,等待完成
335
+ if (result && typeof result.then === 'function') {
336
+ await result
337
+ }
338
+
339
+ // 等待数据更新
340
+ await new Promise((resolve) => setTimeout(resolve, 300))
341
+
342
+ // 继续加载下一级
343
+ if (startDepth + 1 < pathValues.length - 1) {
344
+ await loadMissingData(pathValues, startDepth + 1)
345
+ }
346
+ } catch (error) {
347
+ currentOption.loading = false
348
+ }
349
+ } else {
350
+ // 已经有 children,直接继续加载下一级
351
+ await loadMissingData(pathValues, startDepth + 1)
352
+ }
353
+ }
354
+
355
+ // 开始加载数据
356
+ loadMissingData(valueToLoad, 0).catch(() => {
357
+ // 静默处理错误
358
+ })
359
+ }
360
+
361
+ loadInitialData()
362
+ }, 500)
363
+ }
364
+
365
+ // 在 mergeProp 中触发(处理初始值和 setValue 的情况)
366
+ const currentValue = ctx.rule.value
367
+ // 检查 value 是否变化(通过比较字符串化的值)
368
+ const valueKey = JSON.stringify(currentValue || [])
369
+ const lastValueKey = ctx.rule._lastLoadDataValueKey
370
+
371
+ if (
372
+ valueKey !== lastValueKey &&
373
+ currentValue &&
374
+ Array.isArray(currentValue)
375
+ ) {
376
+ ctx.rule._lastLoadDataValueKey = valueKey
377
+ triggerLoadDataForValue(currentValue)
378
+ }
209
379
  }
210
380
  },
211
381
  render(children, ctx) {
@@ -223,5 +393,22 @@ export default {
223
393
 
224
394
  // 调用默认渲染
225
395
  return ctx.$render.defaultRender(ctx, children)
396
+ },
397
+ mounted(ctx) {
398
+ // mounted 时也触发一次,确保初始值能正确加载
399
+ const value = ctx.rule.value
400
+ const props = ctx.prop.props
401
+
402
+ if (value && Array.isArray(value) && value.length > 1 && props.loadData) {
403
+ // 延迟一点,确保 mergeProp 已完成
404
+ setTimeout(() => {
405
+ const loadDataFn = ctx.prop.props.loadData
406
+ if (loadDataFn && typeof loadDataFn === 'function') {
407
+ // 重新触发一次加载逻辑(会通过 mergeProp 中的逻辑处理)
408
+ // 通过触发 value 变化来触发加载
409
+ ctx.rule._lastLoadDataValueKey = null
410
+ }
411
+ }, 100)
412
+ }
226
413
  }
227
414
  }