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

FormDataConsumer inside ArrayInput is adding its source prop to each array item (usually undefined) #8059

Closed
martdavidson opened this issue Aug 9, 2022 · 3 comments · Fixed by #8061
Assignees
Labels

Comments

@martdavidson
Copy link

martdavidson commented Aug 9, 2022

What you were expecting:

Expected to be able to use a <FormDataConsumer> inside an <ArrayInput><SimpleFormIterator></SimpleFormIterator></ArrayInput> as described in https://marmelab.com/react-admin/Inputs.html#linking-two-inputs:

import {
  ArrayInput,
  Create,
  FormDataConsumer,
  SelectInput,
  SimpleForm,
  SimpleFormIterator,
  TextInput,
} from 'react-admin';
import {useFormContext} from 'react-hook-form';

const TestingCreate = () => {
  return (
    <Create>
      <SimpleForm>
        <TestingCreateLayout />
      </SimpleForm>
    </Create>
  );
};

const TestingCreateLayout = () => {
  const form = useFormContext();
  console.log(form.getValues());
  
  return (
    <ArrayInput source="authors">
      <SimpleFormIterator>
        <TextInput source="name" />
        <FormDataConsumer>
          {({
              formData, // The whole form data
              scopedFormData, // The data for this item of the ArrayInput
              getSource, // A function to get the valid source inside an ArrayInput
              ...rest
            }) =>
            scopedFormData && scopedFormData.name ? (
              <SelectInput
                source={getSource?.('role')} // Will translate to "authors[0].role"
                choices={[{ id: 1, name: 'Head Writer' }, { id: 2, name: 'Co-Writer' }]}
                {...rest}
              />
            ) : null
          }
        </FormDataConsumer>
      </SimpleFormIterator>
    </ArrayInput>
  );
};

What happened instead:

It looks like <FormDataConsumer> is being treated as an additional field by the SimpleFormIterator - because there is no source prop, it's adding an entry of undefined: "" to each item in the authors array. If I do <FormDataConsumer source={'something'}>, then it shows something: "" in each entry instead.

{
    "authors": [
        {
            "name": "MyName",
            "undefined": "",
            "role": 1
        }
    ]
}

Steps to reproduce:

Past the above in a Resources create prop and check the console after adding an item.

Environment

  • React-admin version: react-admin@4.2.5
  • React version: react@18.2.0
  • Browser: Chrome on macOS
@martdavidson martdavidson changed the title FormDataConsumer inside ArrayInput is adding its source props to each array item (usually undefined) FormDataConsumer inside ArrayInput is adding its source prop to each array item (usually undefined) Aug 9, 2022
@martdavidson
Copy link
Author

https://codesandbox.io/s/inspiring-rain-mn5iwf?file=/src/index.tsx

Navigate the imbeded browser to https://mn5iwf.sse.codesandbox.io/#/posts/create if it's not already there and watch the console as you add items.

@fzaninotto
Copy link
Member

Reproduced, thanks!

@martdavidson
Copy link
Author

Hey, thanks so much for the quick fix.

JinParc pushed a commit to JinParc/react-admin that referenced this issue Oct 25, 2022
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