marmelab / react-admin

A frontend Framework for single-page applications on top of REST/GraphQL APIs, using TypeScript, React and Material Design
http://marmelab.com/react-admin
MIT License
24.89k stars 5.23k forks source link

Array input populating existing field values when reordering using conditional form fields #8236

Closed andypwarren closed 9 months ago

andypwarren commented 2 years ago

I have a form which has an array input, inside there are fields which are displayed conditionally based on a type field. When I add a new item and move it above an existing one, then select the same type as the the one that was previously at that index. It copies the old field values from the item previously at that index to the new one.

What you were expecting: When reordering a new item and selecting the type I expect the fields not to be populated with any data.

What happened instead: Fields were populated with existing data from the item previously at that index

Steps to reproduce: I have created a stackblitz to reproduce the issue Application Code

  1. Add a new card to the array
  2. Move the card up above the existing editorial card
  3. Select the editorial type (same as the card that was at this index before)
  4. Observe the field value from the card that was previously at that index is injected into the new field

Here's a video to demonstrate

https://user-images.githubusercontent.com/8376300/194025927-3e097199-8396-46cf-9ad8-7a0a7729d427.mov

Related code: https://stackblitz.com/edit/react-c8qwgj?file=src/App.js Here is the edit form I'm using

<Edit {...props}>
  <SimpleForm>
    <ArrayInput source="cards">
      <SimpleFormIterator>
        <SelectInput source="type" label="Card type" choices={types} />
        <FormDataConsumer>
          {({ scopedFormData, getSource }) => {
            if (!scopedFormData || !getSource) {
              return null;
            }
            switch (scopedFormData.type) {
              case 0:
                return (
                  <>
                    <TextInput
                      source={getSource("product.id")}
                      label="id"
                    />
                  </>
                );
              case 1:
                return (
                  <>
                    <TextInput
                      source={getSource("editorial.title")}
                      label="title"
                    />
                  </>
                );
              default:
                return null;
            }
          }}
        </FormDataConsumer>
      </SimpleFormIterator>
    </ArrayInput>
  </SimpleForm>
</Edit>

Environment

WiXSL commented 2 years ago

Reproduced! Thanks for the report

djhi commented 9 months ago

Can't reproduce anymore