react-final-form-arrays icon indicating copy to clipboard operation
react-final-form-arrays copied to clipboard

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

Open GyrosOfWar opened this issue 5 years ago • 4 comments

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.

GyrosOfWar avatar Jun 26 '19 12:06 GyrosOfWar

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

rickysullivan avatar Aug 01 '19 03:08 rickysullivan

The same problem, maybe anybody resolves this?

lShinal avatar Dec 12 '19 17:12 lShinal

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));

TonyDalian avatar Dec 16 '19 15:12 TonyDalian

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?

IzaGz avatar Mar 17 '20 12:03 IzaGz