Skip to content

Commit e5774cf

Browse files
authored
Merge pull request #170 from workfloworchestrator/2059-restore-values
2059 restore values
2 parents e250e43 + f19195a commit e5774cf

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pydantic-forms': patch
3+
---
4+
5+
Adds fieldData storage

frontend/packages/pydantic-forms/src/core/PydanticFormContextProvider.tsx

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,25 @@ function PydanticFormContextProvider({
162162
});
163163

164164
const awaitReset = useCallback(
165-
async (payLoad: FieldValues = {}) => {
166-
reactHookForm.reset(payLoad);
167-
await new Promise((resolve) => setTimeout(resolve, 0)); // wait one tick
165+
async (payLoad?: FieldValues) => {
166+
getHashForArray(formInputData).then(async (hash) => {
167+
let resetPayload = {};
168+
169+
if (payLoad) {
170+
resetPayload = { ...payLoad };
171+
} else {
172+
const currentStepFromHistory = formInputHistory.get(hash);
173+
174+
if (currentStepFromHistory) {
175+
resetPayload = { ...currentStepFromHistory };
176+
}
177+
}
178+
reactHookForm.reset(resetPayload);
179+
await new Promise((resolve) => setTimeout(resolve, 0)); // wait one tick
180+
});
168181
},
169-
[reactHookForm],
182+
183+
[formInputData, formInputHistory, reactHookForm],
170184
);
171185

172186
const addFormInputData = useCallback(
@@ -242,6 +256,42 @@ function PydanticFormContextProvider({
242256
setHasNext(false);
243257
}, [emptyRawSchema]);
244258

259+
const fieldDataStorageRef = useRef<Map<string, Map<string, unknown>>>(
260+
new Map(),
261+
);
262+
263+
const fieldDataStorage = useMemo(
264+
() => ({
265+
has: (fieldId: string, key: string | number) => {
266+
if (
267+
fieldDataStorageRef.current &&
268+
fieldDataStorageRef.current.has(fieldId)
269+
) {
270+
const fieldStorage =
271+
fieldDataStorageRef.current.get(fieldId);
272+
return fieldStorage?.has(key.toString()) ?? false;
273+
}
274+
return false;
275+
},
276+
get: (fieldId: string, key: string | number) => {
277+
const fieldData = fieldDataStorageRef?.current?.get(fieldId);
278+
return fieldData?.get(key.toString());
279+
},
280+
set: (fieldId: string, key: string | number, value: unknown) => {
281+
fieldDataStorageRef.current.set(
282+
fieldId,
283+
new Map([[key.toString(), value]]),
284+
);
285+
},
286+
delete: (fieldId: string) => {
287+
if (fieldDataStorageRef.current?.has(fieldId)) {
288+
fieldDataStorageRef.current.delete(fieldId);
289+
}
290+
},
291+
}),
292+
[],
293+
);
294+
245295
const PydanticFormContextState = {
246296
// to prevent an issue where the sending state hangs
247297
// we check both the SWR hook state as our manual state
@@ -272,6 +322,7 @@ function PydanticFormContextProvider({
272322
formInputData,
273323
hasNext,
274324
initialData,
325+
fieldDataStorage,
275326
};
276327

277328
// a useeffect for whenever the error response updates
@@ -316,7 +367,7 @@ function PydanticFormContextProvider({
316367
// When the formKey changes we need to reset the form input data
317368
setFormInputData([]);
318369
setFormInputHistory(new Map<string, object>());
319-
awaitReset();
370+
awaitReset({});
320371
formRef.current = formKey;
321372
}
322373
}, [awaitReset, formKey]);
@@ -371,15 +422,6 @@ function PydanticFormContextProvider({
371422
z.config(getLocale());
372423
}, [locale]);
373424

374-
useEffect(() => {
375-
getHashForArray(formInputData).then((hash) => {
376-
const currentStepFromHistory = formInputHistory.get(hash);
377-
if (currentStepFromHistory) {
378-
awaitReset(currentStepFromHistory);
379-
}
380-
});
381-
}, [awaitReset, formInputData, formInputHistory, reactHookForm]);
382-
383425
return (
384426
<PydanticFormContext.Provider value={PydanticFormContextState}>
385427
{children(PydanticFormContextState)}

frontend/packages/pydantic-forms/src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ export type PydanticFormControlledElementProps = Omit<
4747
export type PydanticFormControlledElement =
4848
React.JSXElementConstructor<PydanticFormControlledElementProps>;
4949

50+
export type PydanticFormFieldDataStorage = {
51+
set: (fieldId: string, key: string | number, value: unknown) => void;
52+
has: (fieldId: string, key: string | number) => boolean;
53+
get: (fieldId: string, key: string) => unknown;
54+
delete: (fieldId: string) => void;
55+
};
56+
5057
export interface PydanticFormContextProps {
5158
isLoading: boolean;
5259
isSending: boolean;
@@ -71,6 +78,7 @@ export interface PydanticFormContextProps {
7178
hasNext: boolean;
7279
formInputData: object[];
7380
initialData: FieldValues;
81+
fieldDataStorage: PydanticFormFieldDataStorage;
7482
}
7583

7684
export enum PydanticFormState {

0 commit comments

Comments
 (0)