Skip to content

Commit 1f52433

Browse files
author
Ruben van Leeuwen
committed
1663: Readds second step forms. Adds example values in example app
1 parent 71c3679 commit 1f52433

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

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

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
'use client';
22

33
import { PydanticForm } from 'pydantic-forms';
4-
import type { PydanticFormProvider } from 'pydantic-forms';
4+
import type {
5+
PydanticFormApiProvider,
6+
PydanticFormCustomDataProvider,
7+
PydanticFormLabelProvider,
8+
} from 'pydantic-forms';
59

610
import styles from './page.module.css';
711

812
export default function Home() {
9-
const pydanticFormProvider: PydanticFormProvider = async ({
13+
const pydanticFormApiProvider: PydanticFormApiProvider = async ({
1014
requestBody,
1115
}) => {
1216
const fetchResult = await fetch('http://localhost:8000/form', {
@@ -20,17 +24,65 @@ export default function Home() {
2024
return jsonResult;
2125
};
2226

27+
const pydanticLabelProvider: PydanticFormLabelProvider = async () => {
28+
return new Promise((resolve) => {
29+
resolve({
30+
labels: {
31+
name: 'LABEL NAAM',
32+
name_info: 'DESCRIPTION NAAM',
33+
},
34+
data: {
35+
name: 'VALUE NAAM',
36+
},
37+
});
38+
});
39+
};
40+
41+
const pydanticCustomDataProvider: PydanticFormCustomDataProvider = () => {
42+
return new Promise((resolve) => {
43+
resolve({
44+
name: 'CUSTOM VALUE NAAM',
45+
});
46+
});
47+
};
48+
49+
const ResetButtonAlternative = () => (
50+
<button type="button">Alternative reset</button>
51+
);
52+
53+
const CancelButtonAlternative = () => (
54+
<button type="button">Alternative cancel</button>
55+
);
56+
2357
return (
2458
<div className={styles.page}>
2559
<h1>Pydantic Form</h1>
2660

2761
<PydanticForm
2862
id="theForm"
63+
title="The form title"
64+
sendLabel="Send label"
65+
successNotice="Custom success notice"
2966
onSuccess={() => {
67+
// console.log(fieldValues, summaryData, response);
3068
alert('Form submitted successfully');
3169
}}
70+
onCancel={() => {
71+
alert('Form cancelled');
72+
}}
73+
onChange={() => {
74+
// console.log('onChange', fieldValues);
75+
}}
3276
config={{
33-
formProvider: pydanticFormProvider,
77+
apiProvider: pydanticFormApiProvider,
78+
labelProvider: pydanticLabelProvider,
79+
customDataProvider: pydanticCustomDataProvider,
80+
resetButtonAlternative: ResetButtonAlternative(),
81+
cancelButton: CancelButtonAlternative(),
82+
allowUntouchedSubmit: true,
83+
onFieldChangeHandler: () => {
84+
// console.log('calling onFieldChangeHandler', field);
85+
},
3486
}}
3587
headerComponent={<div>HEADER COMPONENT</div>}
3688
footerComponent={<div>FOOTER COMPONENT</div>}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,14 @@ function PydanticFormContextProvider({
100100
const debugMode = false;
101101

102102
// eslint-disable-next-line @typescript-eslint/no-explicit-any
103-
const [formInputData, setFormInputData] = useState<any>([]);
103+
const [formInputData, setFormInputData] = useState<object[]>([]);
104+
105+
const addFormInputData = (formInput: object) => {
106+
setFormInputData((currentInputs) => {
107+
return [...currentInputs, formInput];
108+
});
109+
};
110+
104111
const [errorDetails, setErrorDetails] =
105112
useState<PydanticFormValidationErrorDetails>();
106113
const [isFullFilled, setIsFullFilled] = useState(false);
@@ -304,7 +311,7 @@ function PydanticFormContextProvider({
304311

305312
const submitFormFn = useCallback(() => {
306313
setIsSending(true);
307-
setFormInputData([rhf?.getValues()]);
314+
addFormInputData(rhf?.getValues());
308315

309316
window.scrollTo(0, 0);
310317
}, [rhf]);

0 commit comments

Comments
 (0)