informed icon indicating copy to clipboard operation
informed copied to clipboard

Question about dynamic fields in ArrayField Nested Form approach

Open calebklc opened this issue 6 years ago • 20 comments

Hi Joe,

I am trying ArrayField to generate dynamic fields using custom component inputs.

I added 3 sets of fields and filled all the fields. When I tried to remove the second one, the third one was also removed.

matrixdynamic

Here is some part of my code.

https://gist.github.com/KLC215/34f8ecf60488b3670fc5a35f4015f2a1

  • MainPage.js is the usage part
  • MatrixDynamic.js renderFields(fields) is the render part

I tried to use copy this example

https://joepuzzo.github.io/informed/?path=/story/arrays--nested-form

and replaced origin inputs to my custom input, it works as expected.

So my question is, What is the best practice / correct way to render dynamic fields inside ArrayField?

Thanks.

calebklc avatar Feb 24 '20 16:02 calebklc

Try to use my key prop directly and not modify the key. Also try to simplify the code down to its most basic form, get it working, then add back complexity until it fails. This should work fine. If u can create a code sandbox and recreate issue there it would help but I believe it has somthing to do with how the component is rendering

joepuzzo avatar Feb 24 '20 16:02 joepuzzo

But this seems to be a key issue if i were to guess

joepuzzo avatar Feb 24 '20 17:02 joepuzzo

You get it fixed ?

joepuzzo avatar Feb 25 '20 00:02 joepuzzo

Oh....sorry. I just saw these messages.

I will get it a try now.

If I still stuck on it, I will create a code sandbox to simulate the case.

Thanks for your solution :)

calebklc avatar Feb 25 '20 00:02 calebklc

No problem. I do think its really a Key issue. One thing to try is add two fields and then delete one. If all the values after the one you delete go away its def an issue with the key prop.

joepuzzo avatar Feb 25 '20 00:02 joepuzzo

Too elaborate in your example you had three fields. Try to add four and see how it reacts after deletion.

joepuzzo avatar Feb 25 '20 00:02 joepuzzo

Yes. It is a key issue and my mistake.

For my code, I iterated rows and iterated each cell inside 1 row.

Before: I set the original keys to each cell but not the row.

Fix: I set the original keys to each row only. and it seems that I cannot add any keys in each cell, so I remove the keys in each cell.

It can fix the issue.

Thank you so much~~~

calebklc avatar Feb 25 '20 01:02 calebklc

Awesome glad I could help

joepuzzo avatar Feb 25 '20 01:02 joepuzzo

Oops....Sorry...

After I applied the multi-step form approach, I found that the ArrayField is not working correctly.

When I add 4 rows and delete the second row. The UI works fine, but the last row has been duplicated in values.

paneldynamic

I have updated the code to replicate the issue. https://stackblitz.com/edit/react-kw8ntc

It is basically the same steps in https://joepuzzo.github.io/informed/?path=/story/multistep-forms--complex-multistep

I added a ArrayField with my PanelDynamic UI wrapper in step EpiPen. Also, I added keepState and relevant on both ArrayField and Fields inside ArrayField.

Is it a correct way to do it?

calebklc avatar Feb 27 '20 09:02 calebklc

<ArrayField field="friends" relevant={relevant} keepState>
          {props => (
            <PanelDynamic {...props}>
              {({ field, key, index, initialValue, remove }) => (
                <div key={key}>
                  <h5>Panel {index}</h5>
                  <Text
                    field={`${field}.name`}
                    initialValue={initialValue && initialValue.name}
                    placeholder="Name"
                    relevant={relevant}
                  />
                  <Text
                    initialValue={initialValue && initialValue.age}
                    field={`${field}.age`}
                    placeholder="age"
                    relevant={relevant}
                  />
                  <button onClick={remove}>Remove</button>
                </div>
              )}
            </PanelDynamic>
          )}
        </ArrayField>

joepuzzo avatar Feb 27 '20 10:02 joepuzzo

keep state on the array field and not the fields themselves and added key prop to div. Works now

joepuzzo avatar Feb 27 '20 10:02 joepuzzo

Wait no that still has issue i need to look into this

joepuzzo avatar Feb 27 '20 10:02 joepuzzo

Ok so the code you had is fine! I had a bug that is now fixed! Update to the latest release

joepuzzo avatar Feb 27 '20 11:02 joepuzzo

Just make sure you have keepState on the ArrayField and the Fields you want to keep

joepuzzo avatar Feb 27 '20 12:02 joepuzzo

It seems that the issue is still happen.

This time, when I add 4 panels and remove the 2nd panel, the last panel value will be gone, and the 3rd panel value will be duplicated.

paneldynamic2

I have added key to div inside the PanelDynamic.js.

  renderPanels = () => {
    const { fields, children, relevant } = this.props;

    return fields.map(({ field, key, remove, initialValue }, i) => (
      <div
        key={key}
        style={{ backgroundColor: "#FFFFE0", padding: 32, marginBottom: 24 }}
      >
        {children({ field, index: i + 1, initialValue, remove })}
      </div>
    ));
  };

and also I have updated the informed version to 3.5.1: https://stackblitz.com/edit/react-kw8ntc

calebklc avatar Feb 28 '20 06:02 calebklc

Update to the latest release! I fixed this issue yesterday

joepuzzo avatar Feb 28 '20 12:02 joepuzzo

3.5.2

joepuzzo avatar Feb 28 '20 12:02 joepuzzo

Sorry about that.....didn't check the update. 😄

Yeah~ Above issue is fixed, Thank you.

But I just found another issue.

paneldynamic3

I add 4 panels and remove all 4. When I re-add 4 panels, it seems that the values are still be saved. https://stackblitz.com/edit/react-kw8ntc

Sorry to bother you again. 😅

calebklc avatar Feb 28 '20 13:02 calebklc

I'm using version 3.6 and facing the same problem like this issue mentioned. Is there any update on this issue? Many thanks.

hoyhung avatar Mar 02 '20 10:03 hoyhung

Check that you are passing the key correctly. I bet its the same issue as originally

joepuzzo avatar Mar 02 '20 10:03 joepuzzo