@skopon-cool/form-sdk 0.4.0 → 0.4.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.
- package/dist/index.js +775 -781
- package/package.json +1 -1
- package/src/components/AskUserFormCard.tsx +24 -24
package/package.json
CHANGED
|
@@ -243,6 +243,12 @@ function AskUserFormCardBody({
|
|
|
243
243
|
return { ...cardValues, ...extraValues }
|
|
244
244
|
}, [fieldNames, extraValues])
|
|
245
245
|
|
|
246
|
+
// live options 走 ref,避免 options 刷新导致 reportSnapshot 引用变化、打断 Init 定时器
|
|
247
|
+
const liveOptionsRef = useRef(liveOptionsCtx.liveOptions)
|
|
248
|
+
liveOptionsRef.current = liveOptionsCtx.liveOptions
|
|
249
|
+
const pendingChangeRef = useRef(liveOptionsCtx.pendingChangeFields)
|
|
250
|
+
pendingChangeRef.current = liveOptionsCtx.pendingChangeFields
|
|
251
|
+
|
|
246
252
|
const reportSnapshot = useCallback(
|
|
247
253
|
(formChange: string[]) => {
|
|
248
254
|
const snapshot = buildFormRuntimeSnapshot({
|
|
@@ -250,49 +256,49 @@ function AskUserFormCardBody({
|
|
|
250
256
|
description: surfaceDescription,
|
|
251
257
|
blocks: renderBlocks,
|
|
252
258
|
values: collectAnswer(),
|
|
253
|
-
liveOptions:
|
|
259
|
+
liveOptions: liveOptionsRef.current,
|
|
254
260
|
formChange,
|
|
255
261
|
})
|
|
256
262
|
runtime.reportData(snapshot)
|
|
257
263
|
},
|
|
258
|
-
[
|
|
259
|
-
surfaceTitle,
|
|
260
|
-
surfaceDescription,
|
|
261
|
-
renderBlocks,
|
|
262
|
-
collectAnswer,
|
|
263
|
-
liveOptionsCtx.liveOptions,
|
|
264
|
-
runtime,
|
|
265
|
-
],
|
|
264
|
+
[surfaceTitle, surfaceDescription, renderBlocks, collectAnswer, runtime],
|
|
266
265
|
)
|
|
266
|
+
const reportSnapshotRef = useRef(reportSnapshot)
|
|
267
|
+
reportSnapshotRef.current = reportSnapshot
|
|
267
268
|
|
|
268
269
|
// Init:surface 就绪或 doc 内容变化时上报(form_change: [])
|
|
270
|
+
// lastInitKey 仅在实际上报后写入;定时器被取消时不占位,避免 Init 被永久跳过。
|
|
269
271
|
useEffect(() => {
|
|
270
|
-
if (!hasFormSurface
|
|
272
|
+
if (!hasFormSurface) {
|
|
273
|
+
lastInitKeyRef.current = ''
|
|
274
|
+
return
|
|
275
|
+
}
|
|
276
|
+
if (!docKey) return
|
|
271
277
|
const initKey = `${surfaceId}::${docKey}`
|
|
272
278
|
if (lastInitKeyRef.current === initKey) return
|
|
273
|
-
lastInitKeyRef.current = initKey
|
|
274
|
-
// 等下一帧,确保 SkoponFormRenderer dataModel 已就绪
|
|
275
279
|
const timer = setTimeout(() => {
|
|
276
280
|
if (!mountedRef.current) return
|
|
277
|
-
|
|
281
|
+
if (lastInitKeyRef.current === initKey) return
|
|
282
|
+
lastInitKeyRef.current = initKey
|
|
283
|
+
reportSnapshotRef.current([])
|
|
278
284
|
}, 0)
|
|
279
285
|
return () => clearTimeout(timer)
|
|
280
|
-
}, [hasFormSurface, surfaceId, docKey
|
|
286
|
+
}, [hasFormSurface, surfaceId, docKey])
|
|
281
287
|
|
|
282
288
|
// Refresh:live options 变更后 debounce 上报(form_change 为变更字段)
|
|
283
289
|
useEffect(() => {
|
|
284
290
|
if (!hasFormSurface || liveOptionsCtx.version === 0) return
|
|
285
|
-
if (
|
|
291
|
+
if (pendingChangeRef.current.length === 0) return
|
|
286
292
|
if (refreshTimerRef.current != null) {
|
|
287
293
|
clearTimeout(refreshTimerRef.current)
|
|
288
294
|
}
|
|
289
295
|
refreshTimerRef.current = setTimeout(() => {
|
|
290
296
|
refreshTimerRef.current = null
|
|
291
297
|
if (!mountedRef.current) return
|
|
292
|
-
const changed = [...
|
|
298
|
+
const changed = [...pendingChangeRef.current]
|
|
293
299
|
liveOptionsCtx.clearPendingChangeFields()
|
|
294
300
|
if (changed.length === 0) return
|
|
295
|
-
|
|
301
|
+
reportSnapshotRef.current(changed)
|
|
296
302
|
}, REFRESH_REPORT_DEBOUNCE_MS)
|
|
297
303
|
return () => {
|
|
298
304
|
if (refreshTimerRef.current != null) {
|
|
@@ -300,13 +306,7 @@ function AskUserFormCardBody({
|
|
|
300
306
|
refreshTimerRef.current = null
|
|
301
307
|
}
|
|
302
308
|
}
|
|
303
|
-
}, [
|
|
304
|
-
hasFormSurface,
|
|
305
|
-
liveOptionsCtx.version,
|
|
306
|
-
liveOptionsCtx.pendingChangeFields,
|
|
307
|
-
liveOptionsCtx.clearPendingChangeFields,
|
|
308
|
-
reportSnapshot,
|
|
309
|
-
])
|
|
309
|
+
}, [hasFormSurface, liveOptionsCtx.version, liveOptionsCtx.clearPendingChangeFields])
|
|
310
310
|
|
|
311
311
|
const handleSubmit = useCallback(async () => {
|
|
312
312
|
if (submitting) return
|