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

Add support to insert multiple items at once when using useFieldArray #3071

Closed
dwiyatci opened this issue Oct 3, 2020 · 6 comments
Closed

Comments

@dwiyatci
Copy link

dwiyatci commented Oct 3, 2020

Hi! I believe this is related to #2710. Currently I'm developing a "toggle multiple fields" feature in my project wherein I've got to be able to toggle the middle field items (form controls) ON/OFF. Here's some hypothetical snippet:

useEffect(() => {
  if (toggledOff) {
    const middleFieldIndexes = _.range(1, fields.length - 1);

    remove(middleFieldIndexes);
  } else {
    const middleFieldItems = [
      { index: 1, value: { foo: 42 } },
      { index: 2, value: { foo: 43 } },
    ];

    // currently not supported API
    insert(middleFieldItems);
  }
}, [toggledOff]);

While I can comfortably use remove([]) API to remove multiple field item (for toggling off) but I don't have its counterpart insert([]) (for toggling on).

Describe the solution you'd like
Support insert([{ index, value }, { index, value }, ...], { shouldFocus }). 🙂

Thank you 🙇🏻‍♂️

@bluebill1049 bluebill1049 added feature request request a feature to be added waiting-up-vote Waiting for votes from the community. labels Oct 3, 2020
@bluebill1049
Copy link
Member

Thanks @dwiyatci 👍 adding to the board.

@bluebill1049 bluebill1049 added this to Pending Feature Requests in React Hook Form Oct 3, 2020
@dwiyatci
Copy link
Author

@bluebill1049 I'm sorry to bother but this seems to become a requirement in my project sooner than I expected. 😣😖😫 While waiting for the up-votes, do you have any idea of a rather clean workaround to realise this with the current lib API? (clearly multiple actions in a loop won't work as mentioned in #2710)

@bluebill1049
Copy link
Member

hey @dwiyatci I will look into it.

@bluebill1049
Copy link
Member

I don't even know what i have built... can you test this out: https://codesandbox.io/s/react-hook-form-usefieldarray-insert-test-z8fyw?file=/src/index.js

@bluebill1049 bluebill1049 removed feature request request a feature to be added waiting-up-vote Waiting for votes from the community. labels Oct 18, 2020
@bluebill1049 bluebill1049 removed this from Pending Feature Requests in React Hook Form Oct 18, 2020
@dwiyatci
Copy link
Author

@bluebill1049 Ohh... WOW. So you're sayin' this turns out to have been implemented all along? :D

@AlanKrygowski
Copy link

Also, if anyone came here looking for a way to append/prepend multiple sets of fields at once, then the prepend function does accept an array, where you can normally place in objects as can be seen in this fork:
https://codesandbox.io/s/react-hook-form-usefieldarray-insert-test-forked-gqqnf?file=/src/index.js
(click prepend)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants