Skip to content

Commit e7cd6c4

Browse files
author
Ruben van Leeuwen
committed
1891: Improve resetAwait function
1 parent dd1c4ab commit e7cd6c4

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

frontend/apps/example/src/app/page.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ export default function Home() {
2121
const pydanticFormApiProvider: PydanticFormApiProvider = async ({
2222
requestBody,
2323
}) => {
24-
const fetchResult = await fetch('http://localhost:8000/form', {
24+
const url = 'http://localhost:8000/form';
25+
26+
const fetchResult = await fetch(url, {
2527
method: 'POST',
28+
body: JSON.stringify(requestBody),
2629
headers: {
2730
'Content-Type': 'application/json',
2831
},
29-
body: JSON.stringify(requestBody),
3032
});
3133
const jsonResult = await fetchResult.json();
3234
return jsonResult;
@@ -87,14 +89,15 @@ export default function Home() {
8789
<h1 style={{ marginBottom: '20px' }}>Pydantic Form </h1>
8890

8991
<PydanticForm
90-
id="theForm"
92+
formKey="theForm"
9193
title="Example form"
92-
successNotice="Custom success notice"
9394
onCancel={() => {
9495
alert('Form cancelled');
9596
}}
97+
onSuccess={() => {
98+
alert('Form submitted successfully');
99+
}}
96100
config={{
97-
allowUntouchedSubmit: true,
98101
apiProvider: pydanticFormApiProvider,
99102
labelProvider: pydanticLabelProvider,
100103
customDataProvider: pydanticCustomDataProvider,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,13 @@ function PydanticFormContextProvider({
164164
values: initialData,
165165
});
166166

167+
/*
168+
This method resets the form and makes sure it waits for the reset to complete
169+
before proceeding. If it finds data in form history, it uses that data to reset the form.
170+
*/
167171
const awaitReset = useCallback(
168172
async (payLoad?: FieldValues) => {
169-
getHashForArray(formInputData).then(async (hash) => {
173+
await getHashForArray(formInputData).then((hash) => {
170174
let resetPayload = {};
171175

172176
if (payLoad) {
@@ -179,7 +183,6 @@ function PydanticFormContextProvider({
179183
}
180184
}
181185
reactHookForm.reset(resetPayload);
182-
await new Promise((resolve) => setTimeout(resolve, 0)); // wait one tick
183186
});
184187
},
185188

0 commit comments

Comments
 (0)