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

Feat/update at index #548

Closed
wants to merge 2 commits into from

Conversation

jansedlon
Copy link
Contributor

@jansedlon jansedlon commented Mar 28, 2024

Hey @edmundhung. I am in need to update a list item at a specific index so I tried to implement it. I welcome any suggestions. There's no contribution guide so I tried to scrap the code and came up with this :D

Use case:
I have a list of attachments in a community post. When a user wants to upload a file, I want to immediately insert it into a list with a loading state and when the upload is finished, I want to update the url and state at a specified index.

For now I have done it using remove and insert which works but is suboptimal.

@edmundhung
Copy link
Owner

I haven't look into the detail yet. But the update intent should work anywhere, you can point it to the list and update the whole list or point it to a specific index and update the value just like any other field.

@jansedlon
Copy link
Contributor Author

jansedlon commented Mar 28, 2024

From what I saw, it accepts a list of values. If I try to update a specific index, the field doesn't exist yet.

Example

form.insert({
  name: fields.attachments.name,
  value: {
    // ...value
  }
});

// code

fetch().then(() => {
  // I want to update here but the reference to the fieldlist does not have the inserted field
  // The length is still 0
})

@jansedlon
Copy link
Contributor Author

This works, but it's meh :D

form.update({
          name: `attachments[${someDeterminedIndex}]`,
          value: {
            // data
          },
        });

@edmundhung
Copy link
Owner

How about making form.update to accept an optional index? Then you could do this:

form.update({
  name: fields.attachments.name,
  index: someDeterminedIndex,
  value: {
    // data
  },
});

@jansedlon
Copy link
Contributor Author

jansedlon commented Mar 28, 2024

Absolutely, that could work. However the update logic is much more complex and I'm not sure I'd do it correctly

@edmundhung
Copy link
Owner

Here you go: #555

@jansedlon
Copy link
Contributor Author

Hey, thank you 🙏🙏 Sorry, haven't had time to try out, will tomorrow

@edmundhung
Copy link
Owner

It's all good :) You can try it out on v1.1.0-pre.0 now.

@jansedlon
Copy link
Contributor Author

jansedlon commented Apr 2, 2024

@edmundhung Works flawlessly 😍 Thank you for everything :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants