Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions frontend/.changeset/blue-apples-sink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pydantic-forms': patch
---

Adds option to clear form after submission
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,33 @@ const RenderForm = (contextProps: PydanticFormContextProps) => {
title,
headerComponent,
skipSuccessNotice,
loadingComponent,
clearForm,
} = contextProps;
const {
formRenderer,
footerRenderer,
componentMatcher: customComponentMatcher,
resetAfterSubmit,
} = config || {};

if (isLoading && !isSending) {
return <div>Formulier aan het ophalen... A</div>;
}
const LoadingComponent = loadingComponent ?? (
<div>Formulier aan het ophalen...</div>
);

if (!pydanticFormSchema) {
return <div>Formulier aan het ophalen... B</div>;
if (isLoading && !isSending) {
return LoadingComponent;
}

if (isSending) {
return <div>Formulier aan het verzenden...</div>;
if (!pydanticFormSchema || isSending) {
return LoadingComponent;
}

if (isFullFilled) {
if (resetAfterSubmit) {
clearForm();
}

if (skipSuccessNotice) {
return <></>;
}
Expand All @@ -50,11 +62,6 @@ const RenderForm = (contextProps: PydanticFormContextProps) => {
);
}

const {
formRenderer,
footerRenderer,
componentMatcher: customComponentMatcher,
} = config || {};
const Renderer = formRenderer ?? FormRenderer;
const FooterRenderer = footerRenderer ?? Footer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function PydanticFormContextProvider({
sendLabel,
headerComponent,
footerComponent,
loadingComponent,
successNotice,
onSuccess,
onCancel,
Expand Down Expand Up @@ -311,6 +312,12 @@ function PydanticFormContextProvider({
isParsingSchema ||
(customDataProvider ? isLoadingCustomData : false);

const clearForm = useCallback(() => {
setFormInputData([]);
setIsFullFilled(false);
setRawSchema(undefined);
}, []);

const PydanticFormContextState = {
// to prevent an issue where the sending state hangs
// we check both the SWR hook state as our manual state
Expand All @@ -320,6 +327,7 @@ function PydanticFormContextProvider({
pydanticFormSchema,
headerComponent,
footerComponent,
loadingComponent,
onCancel,
title,
sendLabel,
Expand All @@ -338,6 +346,7 @@ function PydanticFormContextProvider({
setSaveToLeavePageInCurrentState,
formKey,
formIdKey,
clearForm,
};

return (
Expand Down
7 changes: 6 additions & 1 deletion frontend/packages/pydantic-forms/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export interface PydanticFormInitialContextProps {
children: (props: PydanticFormContextProps) => React.ReactNode;
headerComponent?: React.ReactNode;
footerComponent?: React.ReactNode;
loadingComponent?: React.ReactNode;
hasCardWrapper?: boolean;

config: PydanticFormsContextConfig;
}

Expand Down Expand Up @@ -65,6 +65,7 @@ export interface PydanticFormContextProps {
successNotice?: React.ReactNode;
headerComponent?: React.ReactNode;
footerComponent?: React.ReactNode;
loadingComponent?: React.ReactNode;
allowUntouchedSubmit?: boolean;
skipSuccessNotice?: boolean;
footerCtaPrimaryVariant?: string;
Expand All @@ -73,6 +74,8 @@ export interface PydanticFormContextProps {
config?: PydanticFormsContextConfig;
formKey: string;
formIdKey?: string;
clearForm: () => void;
resetAfterSubmit?: boolean;
}

export enum PydanticFormState {
Expand Down Expand Up @@ -305,6 +308,8 @@ export interface PydanticFormsContextConfig {

// have an option to change the layout columns of fields
formStructureMutator?: PydanticFormStructureMutator;

resetAfterSubmit?: boolean;
}

export type FormRenderer = React.JSXElementConstructor<{
Expand Down