Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SaveBoundary to allow saving multiple things (e.g., forms) with a centralized save button #1448

Merged
merged 12 commits into from
Feb 12, 2024

Conversation

nsams
Copy link
Member

@nsams nsams commented Nov 27, 2023

This introduces a SaveBoundary (context) that can contain multiple areas that get saved together using a SaveBoundarySaveButton.

Any component can register itself in the SaveBoundary by providing a hasChanges boolean and a doSave method. This also adds a default implementation for FinalForm:

  • if FinalForm is rendered inside a SaveBoundary, it registers
  • else the FinalForm renders (like previously) a router Prompt

Prompts are not needed for the inner components, instead SaveBoundary renders a single prompt containing all hasChanges from registered components.

See products admin for an implementation example.

Also changed in Demo Products: ProductsPage now contains Toolbar and Save Button for Detail Page. That way the form is now completely independent of where it is used and could also be put into an EditDialog.

Alternative:

A possible alternative would be to re-use the router Prompt component that also has a save action. But I decided for an additional implementation because:

  • PromptHandler stores dirty in a ref instead of state, so enabling/disabling save button is not possible (could be changed to state though)
  • Prompt has no clear edges, as a new SubmissionBoundary has

see #1449 for additional usage of SubmissionBoundary

…) in a SaveRange

This introduces a SaveRange (context) that can contain multiple areas that
get saved together using a SaveRangeSaveButton.

Any component can register itself in the SaveRange by providing a hasChanges boolean and a doSave method.
This also adds a default implementation for FinalForm:
- if FinalForm is rendered inside a SaveRange, it registers
- else the FinalForm renders (like previously) a router Prompt

Prompts are not needed for the inner components, instead SaveRange renders a single prompt containing
all hasChanges from registered components.

See products admin for an implementation example.
@johnnyomair
Copy link
Collaborator

I've had an idea about naming: How about SaveBoundary or SubmissionBoundary? Would be in line with ErrorBoundary

@nsams nsams changed the title Add a centralized Save Button that can save multiple things (eg forms) in a SaveRange Add a centralized Save Button that can save multiple things (eg forms) in a SubmissionBoundary Dec 5, 2023
@vivid-planet vivid-planet deleted a comment from netlify bot Dec 18, 2023
@vivid-planet vivid-planet deleted a comment from netlify bot Dec 18, 2023
.changeset/silver-drinks-perform.md Outdated Show resolved Hide resolved
.changeset/silver-drinks-perform.md Outdated Show resolved Hide resolved
demo/admin/src/products/ProductsPage.tsx Outdated Show resolved Hide resolved
packages/admin/admin/src/index.ts Outdated Show resolved Hide resolved
nsams and others added 2 commits December 18, 2023 18:25
Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
@johnnyomair
Copy link
Collaborator

@nsams please fix the lint workflow.

@johnnyomair johnnyomair changed the title Add a centralized Save Button that can save multiple things (eg forms) in a SubmissionBoundary Add SaveBoundary to allow saving multiple things (e.g., forms) with a centralized save button Feb 12, 2024
@johnnyomair johnnyomair merged commit 8eb1375 into main Feb 12, 2024
10 checks passed
@johnnyomair johnnyomair deleted the centralized-save-button branch February 12, 2024 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants