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

Fix disabled inputs temporarily erase values #10249

Merged
merged 3 commits into from
Oct 1, 2024
Merged

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Sep 30, 2024

Problem

React-hook-form sets the value of disabled inputs to undefined. For most data providers, this isn't a problem (except the disabled field won't be sent in the update).

But FakeRest is local, so it receives the undefined value, and uses it to override the existing value. In effect, this erases the value.

dataProvider.update(123, data: { id: undefined }); // erases the id with FakeRest

I also noticed that we made the same mistake in optimistic updates.

This can be seen e.g. in the simple example, by changing the first input of PostEdit to <TexuInput source="id" disabled" />

Solution

Remove undefined values from the edit payload:

  • In ra-data-fakerest
  • in useUpdate optimistic update
  • in useUpdateMany optimistic update

Supersedes #10242

## Problem

React-hook-form sets the value of `disabled` inputs to `undefined`. For most data providers, this isn't a problem (except the disabled field won't be sent in the update).

But FakeRest is local, so it receives the `undefined` value, and uses it to override the existing value. In effect, this erases the value.

This can be seen e.g. in the simple example, by changing the first input of PostEdit to `<TexuInput source="id" disabled" />`

## Solution

Remove `undefined` values from the edit payload.
@fzaninotto fzaninotto changed the title Fix ra-data-fakerest removes data when a form has an input disabled Fix ra-data-fakerest removes data when a form has a disabled input Sep 30, 2024
Comment on lines 135 to 141
{
...old[index],
// Stringify and parse the data to remove undefined values.
// If we don't do this, an update with { id: undefined } as payload
// would remove the id from the record, which no real data provider does.
...JSON.parse(JSON.stringify(data)),
} as RecordType,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we have tests for this?

Comment on lines 127 to 133
{
...newCollection[index],
// Stringify and parse the data to remove undefined values.
// If we don't do this, an update with { id: undefined } as payload
// would remove the id from the record, which no real data provider does.
...JSON.parse(JSON.stringify(data)),
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

@fzaninotto fzaninotto changed the title Fix ra-data-fakerest removes data when a form has a disabled input Fix disabled inputs temporarily erase values Sep 30, 2024
@djhi djhi merged commit ce040b2 into master Oct 1, 2024
14 checks passed
@djhi djhi deleted the Fix-fakerest-undefined branch October 1, 2024 08:53
@djhi djhi added this to the 5.2.2 milestone Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants