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

Question: How to know if a field/zodField atom inside listAtom is at index (say first or last) #6

Open
pgangwani opened this issue May 13, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@pgangwani
Copy link

Is your feature request related to a problem? Please describe.
This is question. I would like to know if a field/zodField atom inside listAtom is at index (say first or last).
Lets take an example
List> { A: fieldAtom, B: Atom}[]
If change of B atom is triggered, how do I know if its at n index of ListAtom

Describe the solution you'd like
I need to see the example if I am missing something.

Describe alternatives you've considered
I am using always listAtom validate to rely fully but that doest give inline error experience. In my case I would like to skip validation or make it optional.

Additional context
Add any other context or screenshots about the feature request here.

@MiroslavPetrik
Copy link
Member

Currently there is information about the item index in the list while you are rendering the list. The docs are full of this.
But I suspect that this question is related to the item position being unknown while you validate or change the data.

First of all, I would focus on the validation - previously I've added demo on the listAtom validation, where the ascending order of items is validated. I assume, that you would like to have this error attached to the specific items violating the ASC order, instead of having one error on the whole listAtom.

That looks like a valid feature request. But implementation wise, the validate function would need to be aware of the self fieldAtom, so it could retrieve its index from the list atom. The list atom is accessible, as the validate callback can read any atoms with the jotai get.

I can see this could be valuable, but implementation wise, to make the fields aware of context, that is more work. It would be similar to what I've implemented with the name atoms, which are recursive and support nested namespacing like users.0.accounts.2.iban.

That being said, if you really need this, you still can traverse your atoms in the validate config function of the listAtom and you can "forcibly" set the errorAtom of the respective fields. That would be imperative code.

@MiroslavPetrik MiroslavPetrik added the enhancement New feature or request label Sep 13, 2024
@pgangwani
Copy link
Author

pgangwani commented Sep 15, 2024

Hello @MiroslavPetrik, Hope you are doing good !
Thanks alot for the reasoning and future forward to this library. I would like to contribute to this library in better way including future of whole form-atom org. Earlier I was not in state of understanding the granular details. Now I think I spent enough time with this and extend for advance usecases.
Any guidance for me would really appreciated!

Now, Coming back to this. I tried below snippet and it worked , see:

const discountValuesAtomList = get(get(discountValues)._formFields);
const discountValueIndex = discountValuesAtomList.findIndex(
  (field) => field.val === valField,
);

strucutre of the field:

[{
val: "val-at-1"
}]

But I am really not sure if this is cleaner way or not.

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

No branches or pull requests

2 participants