Skip to content

Commit

Permalink
fix(Forms): enhance Form.useData data sync when id is used
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Feb 4, 2025
1 parent 0512db1 commit ce78a0e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ export type SharedAttachments<Data = unknown> = {
setShowAllErrors?: ContextState['setShowAllErrors']
setSubmitState?: ContextState['setSubmitState']
rerenderUseDataHook?: () => void
updateDataValue?: ContextState['updateDataValue']
clearData?: () => void
setData?: ContextState['setData']
fieldConnectionsRef?: ContextState['fieldConnectionsRef']
}

Expand Down Expand Up @@ -756,49 +758,6 @@ export default function Provider<Data extends JsonObject>(
}) // Delay so the field validation error message are not shown
}, [emptyData, id, onClear, setSharedData])

useLayoutEffect(() => {
// Set the shared state, if initialData was given
if (id) {
if (initialData && !sharedData.data) {
extendSharedData(initialData, { preventSyncOfSameInstance: true })
}
}
}, [id, initialData, extendSharedData, sharedData.data])

useLayoutEffect(() => {
if (id) {
extendAttachment(
{
visibleDataHandler,
filterDataHandler,
hasErrors,
hasFieldError,
setShowAllErrors,
setSubmitState,
clearData,
fieldConnectionsRef,
},
{ preventSyncOfSameInstance: true }
)
if (filterSubmitData) {
rerenderUseDataHook?.()
}
}
}, [
extendAttachment,
visibleDataHandler,
filterDataHandler,
filterSubmitData,
hasErrors,
hasFieldError,
id,
rerenderUseDataHook,
setShowAllErrors,
setSubmitState,
clearData,
extendSharedData,
])

useMemo(() => {
executeAjvValidator()

Expand Down Expand Up @@ -1325,6 +1284,53 @@ export default function Provider<Data extends JsonObject>(
})
}, [setFormState, setSubmitState])

useLayoutEffect(() => {
// Set the shared state, if initialData was given
if (id) {
if (initialData && !sharedData.data) {
extendSharedData(initialData, { preventSyncOfSameInstance: true })
}
}
}, [id, initialData, extendSharedData, sharedData.data])

useLayoutEffect(() => {
if (id) {
extendAttachment(
{
visibleDataHandler,
filterDataHandler,
hasErrors,
hasFieldError,
setShowAllErrors,
setSubmitState,
clearData,
updateDataValue,
setData,
fieldConnectionsRef,
},
{ preventSyncOfSameInstance: true }
)
if (filterSubmitData) {
rerenderUseDataHook?.()
}
}
}, [
extendAttachment,
visibleDataHandler,
filterDataHandler,
filterSubmitData,
hasErrors,
hasFieldError,
id,
rerenderUseDataHook,
setShowAllErrors,
setSubmitState,
clearData,
extendSharedData,
updateDataValue,
setData,
])

const { bufferedFormState: formState } = useFormStatusBuffer({
formState: formStateRef.current,
waitFor: hasFieldState('pending'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,20 @@ export default function useData<Data = JsonObject>(
dataContext.filterDataHandler
}

const updateDataValue = dataContext?.updateDataValue
const setData = dataContext?.setData
const attachments = sharedAttachmentsRef.current?.data
const updateDataValue =
attachments?.updateDataValue || dataContext?.updateDataValue
const setData = attachments?.setData || dataContext?.setData

const set = useCallback(
(newData: Data) => {
if (id) {
if (!setData) {
sharedDataRef.current.update(newData)
} else {
setData?.(newData)
}
},
[id, setData]
[setData]
)

const update = useCallback<UseDataReturnUpdate<Data>>(
Expand All @@ -139,15 +141,14 @@ export default function useData<Data = JsonObject>(
// Update existing data
pointer.set(existingData, path, newValue)

// Update provider with new data
if (id) {
if (!updateDataValue) {
sharedDataRef.current.extend(existingData)
} else {
updateDataValue(path, newValue)
}
}
},
[id, updateDataValue]
[updateDataValue]
)

const remove = useCallback<UseDataReturn<Data>['remove']>(
Expand All @@ -161,14 +162,14 @@ export default function useData<Data = JsonObject>(
pointer.remove(existingData, path)

// Update provider with new data
if (id) {
if (!updateDataValue) {
sharedDataRef.current.set(existingData)
} else {
updateDataValue(path, undefined)
}
}
},
[id, updateDataValue]
[updateDataValue]
)

const reduceToVisibleFields = useCallback<
Expand Down

0 comments on commit ce78a0e

Please sign in to comment.