Skip to content

Commit

Permalink
Form reset improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
apedroferreira committed Feb 14, 2025
1 parent 357a0db commit 4dd23b1
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 21 deletions.
7 changes: 6 additions & 1 deletion docs/data/toolpad/core/components/crud/CrudAdvanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ function CrudAdvanced(props) {
<Create
initialValues={{ title: 'New note' }}
onSubmitSuccess={handleCreate}
resetOnSubmit={false}
/>
) : null}
{router.pathname !== createPath && showNoteId ? (
Expand All @@ -286,7 +287,11 @@ function CrudAdvanced(props) {
/>
) : null}
{editNoteId ? (
<Edit id={editNoteId} onSubmitSuccess={handleEdit} />
<Edit
id={editNoteId}
onSubmitSuccess={handleEdit}
resetOnSubmit={false}
/>
) : null}
</CrudProvider>
{/* preview-end */}
Expand Down
7 changes: 6 additions & 1 deletion docs/data/toolpad/core/components/crud/CrudAdvanced.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ export default function CrudAdvanced(props: DemoProps) {
<Create<Note>
initialValues={{ title: 'New note' }}
onSubmitSuccess={handleCreate}
resetOnSubmit={false}
/>
) : null}
{router.pathname !== createPath && showNoteId ? (
Expand All @@ -307,7 +308,11 @@ export default function CrudAdvanced(props: DemoProps) {
/>
) : null}
{editNoteId ? (
<Edit<Note> id={editNoteId} onSubmitSuccess={handleEdit} />
<Edit<Note>
id={editNoteId}
onSubmitSuccess={handleEdit}
resetOnSubmit={false}
/>
) : null}
</CrudProvider>
{/* preview-end */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<Create<Note>
initialValues={{ title: 'New note' }}
onSubmitSuccess={handleCreate}
resetOnSubmit={false}
/>
) : null}
{router.pathname !== createPath && showNoteId ? (
Expand All @@ -21,6 +22,10 @@
/>
) : null}
{editNoteId ? (
<Edit<Note> id={editNoteId} onSubmitSuccess={handleEdit} />
<Edit<Note>
id={editNoteId}
onSubmitSuccess={handleEdit}
resetOnSubmit={false}
/>
) : null}
</CrudProvider>
3 changes: 2 additions & 1 deletion docs/pages/toolpad/core/api/create.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"props": {
"dataSource": { "type": { "name": "object" } },
"initialValues": { "type": { "name": "object" }, "default": "{}" },
"onSubmitSuccess": { "type": { "name": "func" } }
"onSubmitSuccess": { "type": { "name": "func" } },
"resetOnSubmit": { "type": { "name": "bool" } }
},
"name": "Create",
"imports": ["import { Create } from '@toolpad/core/Crud';"],
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/toolpad/core/api/edit.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"props": {
"dataSource": { "type": { "name": "object" } },
"onSubmitSuccess": { "type": { "name": "func" } }
"onSubmitSuccess": { "type": { "name": "func" } },
"resetOnSubmit": { "type": { "name": "bool" } }
},
"name": "Edit",
"imports": ["import { Edit } from '@toolpad/core/Crud';"],
Expand Down
5 changes: 4 additions & 1 deletion docs/translations/api-docs/create/create.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"propDescriptions": {
"dataSource": { "description": "Server-side data source." },
"initialValues": { "description": "Initial form values." },
"onSubmitSuccess": { "description": "Callback fired when the form is successfully submitted." }
"onSubmitSuccess": { "description": "Callback fired when the form is successfully submitted." },
"resetOnSubmit": {
"description": "Whether the form fields should reset after the form is submitted."
}
},
"classDescriptions": {}
}
5 changes: 4 additions & 1 deletion docs/translations/api-docs/edit/edit.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"componentDescription": "",
"propDescriptions": {
"dataSource": { "description": "Server-side data source." },
"onSubmitSuccess": { "description": "Callback fired when the form is successfully submitted." }
"onSubmitSuccess": { "description": "Callback fired when the form is successfully submitted." },
"resetOnSubmit": {
"description": "Whether the form fields should reset after the form is submitted."
}
},
"classDescriptions": {}
}
10 changes: 8 additions & 2 deletions packages/toolpad-core/src/CRUD/CRUD.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ function Crud<D extends DataModel>(props: CrudProps<D>) {
);
}
if (match(createPath)(pathname)) {
return <Create<D> initialValues={defaultValues} onSubmitSuccess={handleCreate} />;
return (
<Create<D>
initialValues={defaultValues}
onSubmitSuccess={handleCreate}
resetOnSubmit={false}
/>
);
}
const showMatch = match<{ id: DataModelId }>(showPath)(pathname);
if (showMatch) {
Expand All @@ -107,7 +113,7 @@ function Crud<D extends DataModel>(props: CrudProps<D>) {
if (editMatch) {
const resourceId = editMatch.params.id;
invariant(resourceId, 'No resource ID present in URL.');
return <Edit<D> id={resourceId} onSubmitSuccess={handleEdit} />;
return <Edit<D> id={resourceId} onSubmitSuccess={handleEdit} resetOnSubmit={false} />;
}
return null;
}, [
Expand Down
11 changes: 10 additions & 1 deletion packages/toolpad-core/src/CRUD/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export interface CreateProps<D extends DataModel> {
* Callback fired when the form is successfully submitted.
*/
onSubmitSuccess?: () => void;
/**
* Whether the form fields should reset after the form is submitted.
*/
resetOnSubmit?: boolean;
}
/**
*
Expand All @@ -32,7 +36,7 @@ export interface CreateProps<D extends DataModel> {
* - [Create API](https://mui.com/toolpad/core/api/create)
*/
function Create<D extends DataModel>(props: CreateProps<D>) {
const { initialValues, onSubmitSuccess } = props;
const { initialValues, onSubmitSuccess, resetOnSubmit } = props;

const crudContext = React.useContext(CrudContext);
const dataSource = (props.dataSource ?? crudContext.dataSource) as Exclude<
Expand All @@ -57,6 +61,7 @@ function Create<D extends DataModel>(props: CreateProps<D>) {
initialValues={initialValues}
onSubmit={handleCreate}
onSubmitSuccess={onSubmitSuccess}
resetOnSubmit={resetOnSubmit}
localeText={{
submitButtonLabel: 'Create',
submitSuccessMessage: 'Item created successfully.',
Expand Down Expand Up @@ -84,6 +89,10 @@ Create.propTypes /* remove-proptypes */ = {
* Callback fired when the form is successfully submitted.
*/
onSubmitSuccess: PropTypes.func,
/**
* Whether the form fields should reset after the form is submitted.
*/
resetOnSubmit: PropTypes.bool,
} as any;

export { Create };
13 changes: 11 additions & 2 deletions packages/toolpad-core/src/CRUD/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export interface EditProps<D extends DataModel> {
* Callback fired when the form is successfully submitted.
*/
onSubmitSuccess?: () => void;
/**
* Whether the form fields should reset after the form is submitted.
*/
resetOnSubmit?: boolean;
}
/**
*
Expand All @@ -31,7 +35,7 @@ export interface EditProps<D extends DataModel> {
* - [Edit API](https://mui.com/toolpad/core/api/edit)
*/
function Edit<D extends DataModel>(props: EditProps<D>) {
const { id, onSubmitSuccess } = props;
const { id, onSubmitSuccess, resetOnSubmit } = props;

const crudContext = React.useContext(CrudContext);
const dataSource = (props.dataSource ?? crudContext.dataSource) as Exclude<
Expand Down Expand Up @@ -103,14 +107,15 @@ function Edit<D extends DataModel>(props: EditProps<D>) {
initialValues={data}
onSubmit={handleEdit}
onSubmitSuccess={onSubmitSuccess}
resetOnSubmit={resetOnSubmit}
localeText={{
submitButtonLabel: 'Edit',
submitSuccessMessage: 'Item edited successfully.',
submitErrorMessage: 'Failed to edit item.',
}}
/>
) : null;
}, [data, dataSource, error, handleEdit, isLoading, onSubmitSuccess]);
}, [data, dataSource, error, handleEdit, isLoading, onSubmitSuccess, resetOnSubmit]);

return <Box sx={{ display: 'flex', flex: 1 }}>{renderEdit}</Box>;
}
Expand All @@ -132,6 +137,10 @@ Edit.propTypes /* remove-proptypes */ = {
* Callback fired when the form is successfully submitted.
*/
onSubmitSuccess: PropTypes.func,
/**
* Whether the form fields should reset after the form is submitted.
*/
resetOnSubmit: PropTypes.bool,
} as any;

export { Edit };
8 changes: 6 additions & 2 deletions packages/toolpad-core/src/CRUD/FormPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface FormPageProps<D extends DataModel> {
initialValues?: Partial<OmitId<D>>;
onSubmit: (formValues: Partial<OmitId<D>>) => void | Promise<void>;
onSubmitSuccess?: () => void;
resetOnSubmit?: boolean;
localeText: FormPageLocaleText;
}

Expand All @@ -47,6 +48,7 @@ function FormPage<D extends DataModel>(props: FormPageProps<D>) {
initialValues = {} as Partial<OmitId<D>>,
onSubmit,
onSubmitSuccess,
resetOnSubmit = true,
localeText,
} = props;
const { fields, validate } = dataSource;
Expand Down Expand Up @@ -129,7 +131,9 @@ function FormPage<D extends DataModel>(props: FormPageProps<D>) {

if (onSubmitSuccess) {
onSubmitSuccess();
} else {
}

if (resetOnSubmit) {
handleFormReset();
}

Expand Down Expand Up @@ -349,7 +353,7 @@ function FormPage<D extends DataModel>(props: FormPageProps<D>) {
type="submit"
variant="contained"
size="large"
loading={isSubmitting || (hasSubmittedSuccessfully && !!onSubmitSuccess)}
loading={isSubmitting || (hasSubmittedSuccessfully && !resetOnSubmit)}
>
{localeText.submitButtonLabel}
</Button>
Expand Down
11 changes: 10 additions & 1 deletion packages/toolpad-core/src/Crud/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export interface CreateProps<D extends DataModel> {
* Callback fired when the form is successfully submitted.
*/
onSubmitSuccess?: () => void;
/**
* Whether the form fields should reset after the form is submitted.
*/
resetOnSubmit?: boolean;
}
/**
*
Expand All @@ -32,7 +36,7 @@ export interface CreateProps<D extends DataModel> {
* - [Create API](https://mui.com/toolpad/core/api/create)
*/
function Create<D extends DataModel>(props: CreateProps<D>) {
const { initialValues, onSubmitSuccess } = props;
const { initialValues, onSubmitSuccess, resetOnSubmit } = props;

const crudContext = React.useContext(CrudContext);
const dataSource = (props.dataSource ?? crudContext.dataSource) as Exclude<
Expand All @@ -57,6 +61,7 @@ function Create<D extends DataModel>(props: CreateProps<D>) {
initialValues={initialValues}
onSubmit={handleCreate}
onSubmitSuccess={onSubmitSuccess}
resetOnSubmit={resetOnSubmit}
localeText={{
submitButtonLabel: 'Create',
submitSuccessMessage: 'Item created successfully.',
Expand Down Expand Up @@ -84,6 +89,10 @@ Create.propTypes /* remove-proptypes */ = {
* Callback fired when the form is successfully submitted.
*/
onSubmitSuccess: PropTypes.func,
/**
* Whether the form fields should reset after the form is submitted.
*/
resetOnSubmit: PropTypes.bool,
} as any;

export { Create };
10 changes: 8 additions & 2 deletions packages/toolpad-core/src/Crud/Crud.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ function Crud<D extends DataModel>(props: CrudProps<D>) {
);
}
if (match(createPath)(pathname)) {
return <Create<D> initialValues={defaultValues} onSubmitSuccess={handleCreate} />;
return (
<Create<D>
initialValues={defaultValues}
onSubmitSuccess={handleCreate}
resetOnSubmit={false}
/>
);
}
const showMatch = match<{ id: DataModelId }>(showPath)(pathname);
if (showMatch) {
Expand All @@ -107,7 +113,7 @@ function Crud<D extends DataModel>(props: CrudProps<D>) {
if (editMatch) {
const resourceId = editMatch.params.id;
invariant(resourceId, 'No resource ID present in URL.');
return <Edit<D> id={resourceId} onSubmitSuccess={handleEdit} />;
return <Edit<D> id={resourceId} onSubmitSuccess={handleEdit} resetOnSubmit={false} />;
}
return null;
}, [
Expand Down
13 changes: 11 additions & 2 deletions packages/toolpad-core/src/Crud/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export interface EditProps<D extends DataModel> {
* Callback fired when the form is successfully submitted.
*/
onSubmitSuccess?: () => void;
/**
* Whether the form fields should reset after the form is submitted.
*/
resetOnSubmit?: boolean;
}
/**
*
Expand All @@ -31,7 +35,7 @@ export interface EditProps<D extends DataModel> {
* - [Edit API](https://mui.com/toolpad/core/api/edit)
*/
function Edit<D extends DataModel>(props: EditProps<D>) {
const { id, onSubmitSuccess } = props;
const { id, onSubmitSuccess, resetOnSubmit } = props;

const crudContext = React.useContext(CrudContext);
const dataSource = (props.dataSource ?? crudContext.dataSource) as Exclude<
Expand Down Expand Up @@ -103,14 +107,15 @@ function Edit<D extends DataModel>(props: EditProps<D>) {
initialValues={data}
onSubmit={handleEdit}
onSubmitSuccess={onSubmitSuccess}
resetOnSubmit={resetOnSubmit}
localeText={{
submitButtonLabel: 'Edit',
submitSuccessMessage: 'Item edited successfully.',
submitErrorMessage: 'Failed to edit item.',
}}
/>
) : null;
}, [data, dataSource, error, handleEdit, isLoading, onSubmitSuccess]);
}, [data, dataSource, error, handleEdit, isLoading, onSubmitSuccess, resetOnSubmit]);

return <Box sx={{ display: 'flex', flex: 1 }}>{renderEdit}</Box>;
}
Expand All @@ -132,6 +137,10 @@ Edit.propTypes /* remove-proptypes */ = {
* Callback fired when the form is successfully submitted.
*/
onSubmitSuccess: PropTypes.func,
/**
* Whether the form fields should reset after the form is submitted.
*/
resetOnSubmit: PropTypes.bool,
} as any;

export { Edit };
Loading

0 comments on commit 4dd23b1

Please sign in to comment.