final-form / react-final-form-arrays

A component for rendering and editing arrays 🏁 React Final Form
MIT License
204 stars 69 forks source link

`fields.remove` call with array of file inputs always removes last input in array, regardless of index argument #92

Open GyrosOfWar opened 5 years ago

GyrosOfWar commented 5 years ago

Are you submitting a bug report or a feature request?

A bug report

What is the current behavior?

Input arrays don't work with file inputs (<input type="file" />) as expected. When clicking any of the remove buttons in the supplied sandbox, the last entry in the list of inputs is removed, not the one selected. (the fields.remove function always removes the last input, regardless of the index argument). When substituting text input fields instead of file uploads, it works as expected. (see second sandbox link)

What is the expected behavior?

The fields.remove function should remove the input at the index it was passed, not the last one.

Sandbox Link

File inputs: https://codesandbox.io/s/silly-stonebraker-vyi6l Text inputs: https://codesandbox.io/s/jovial-kalam-8esq7

What's your environment?

"final-form": "4.16.1"
"final-form-arrays": "1.1.2"
"react": "16.8.6"
"react-dom": "16.8.6"
"react-final-form": "6.3.0"
"react-final-form-arrays": "3.1.0"

Other information

None.

rickysullivan commented 5 years ago

This is the same for <input type="checkbox" />

lShinal commented 4 years ago

The same problem, maybe anybody resolves this?

TonyDalian commented 4 years ago

Hi @lShinal @rickysullivan @GyrosOfWar I think it's not a bug, I just checked the source code,

 remove: (index: number) => FieldValue . 

the function of remove accept a para "index", not the value in the array. We can use it like this below:

if (value) fields.push(option);
else fields.remove(fields.value.indexOf(option));
IzaGz commented 4 years ago

same trouble issue "react-final-form": "^6.3.0", "react-final-form-arrays": "^3.1.1",

Anyone solved this?

@litao8976 why you do `

if (value) fields.push(option);
else fields.remove(fields.value.indexOf(option));

` why push here?

@GyrosOfWar , how do you solved this?