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

Remove support for warnWhenUnsavedChange when react-router doesn't allow it #8272

Merged
merged 1 commit into from
Oct 18, 2022
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
2 changes: 2 additions & 0 deletions docs/EditTutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,8 @@ const Form = ({ onSubmit }) => {

**Tip**: You can customize the message displayed in the confirm dialog by setting the `ra.message.unsaved_changes` message in your i18nProvider.

**Warning**: This feature only works if you have a dependency on react-router 6.3.0 **at most**. The react-router team disabled this possibility in react-router 6.4, so `warnWhenUnsavedChanges` will silently fail with react-router 6.4 or later.

## Submit On Enter

By default, pressing `ENTER` in any of the form inputs submits the form - this is the expected behavior in most cases. To disable the automated form submission on enter, set the `type` prop of the `SaveButton` component to `button`.
Expand Down
1 change: 1 addition & 0 deletions docs/Form.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,4 @@ export const TagEdit = () => (
);
```

**Warning**: This feature only works if you have a dependency on react-router 6.3.0 **at most**. The react-router team disabled this possibility in react-router 6.4, so `warnWhenUnsavedChanges` will silently fail with react-router 6.4 or later.
2 changes: 2 additions & 0 deletions docs/SimpleForm.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ export const TagEdit = () => (
);
```

**Warning**: This feature only works if you have a dependency on react-router 6.3.0 **at most**. The react-router team disabled this possibility in react-router 6.4, so `warnWhenUnsavedChanges` will silently fail with react-router 6.4 or later.

## Using Fields As Children

The basic usage of `<SimpleForm>` is to pass [Input components](./Inputs.md) as children. For non-editable fields, you can pass `disabled` inputs, or even [Field components](./Fields.md). But since `<Field>` components have no label by default, you'll have to wrap your inputs in a `<Labeled>` component in that case:
Expand Down
2 changes: 2 additions & 0 deletions docs/TabbedForm.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ export const TagEdit = () => (
);
```

**Warning**: This feature only works if you have a dependency on react-router 6.3.0 **at most**. The react-router team disabled this possibility in react-router 6.4, so `warnWhenUnsavedChanges` will silently fail with react-router 6.4 or later.

## `<FormTab>`

`<TabbedForm>` expect `<FormTab>` elements as children. `<FormTab>` elements accept four props:
Expand Down
8 changes: 8 additions & 0 deletions packages/ra-core/src/form/useWarnWhenUnsavedChanges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export const useWarnWhenUnsavedChanges = (

useEffect(() => {
if (!enable || !isDirty) return;
if (!navigator.block) {
if (process.env.NODE_ENV !== 'production') {
console.warn(
'warnWhenUnsavedChanged is not compatible with react-router >= 6.4. If you need this feature, please downgrade react-router to 6.3.0'
);
}
return;
}

let unblock = navigator.block((tx: Transition) => {
const newLocationIsInsideForm = tx.location.pathname.startsWith(
Expand Down