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

Input level default values aren't applied to the formState on mount #7603

Closed
andrico1234 opened this issue Apr 29, 2022 · 1 comment · Fixed by #8340
Closed

Input level default values aren't applied to the formState on mount #7603

andrico1234 opened this issue Apr 29, 2022 · 1 comment · Fixed by #8340
Labels

Comments

@andrico1234
Copy link
Contributor

andrico1234 commented Apr 29, 2022

With code like this:

const PostCreate = () => {
  return (
    <Create
      transform={(r) => {
        console.log(r);
        return r;
      }}
    >
      <SimpleForm>
        <BooleanInput source="hi" defaultValue />
        <FormDataConsumer>
          {({ formData }) => {
            if (formData.hi) return null;

            return <TextInput source="boo" />;
          }}
        </FormDataConsumer>
      </SimpleForm>
    </Create>
  );
};

I would expect to not see the TextInput rendered on page load. This is because we don't display it if the BooleanInput is set to true, (which is its default value).

You can see this behaviour in the PostCreate page

It seems that if an input has a defaultValue that value isn't applied to the formState immediately. The formState is only then updated once we change one of the form's inputs.

In addition to the formState not being up to date, it also means that the save button is disabled until we change one of the inputs, which shouldn't be the case if we've set default values on one or more of our inputs

A temporary work around is to set these values from within the Form's defaultValues object.

EDIT:

It seems that even with the temporary workaround, the save button is still disabled until we make a change to one of the inputs. My initial thought is that this not expected behaviour. For a good UX, we might want to set sensible defaults to our form, which our end-user could save without having to make any additional changes themselves.

I assumed that there may be a way to override this behaviour via the Form's props, but I can't see an attribute to override the SaveButton's disabled state. I figure I would need to pass through a custom save button with the alwaysEnable prop set to true.

Environment

  • React-admin version:
  • Last version that did not exhibit the issue (if applicable):
  • React version:
  • Browser:
  • Stack trace (in case of a JS error):
@andrico1234 andrico1234 changed the title Default values aren't applied to the formState immediately Input level default values aren't applied to the formState on mount Apr 29, 2022
@slax57 slax57 added the bug label Apr 29, 2022
@slax57
Copy link
Contributor

slax57 commented Apr 29, 2022

Hi!
I reproduced the issue, thanks!

Regarding the Save Button's enabled state, I believe setting alwaysEnable to true is the simplest solution you can have, and it doesn't seem that unsuitable to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants