form-atoms / list-atom

A listAtom extension for the Jotai form-atoms.
https://form-atoms.github.io/list-atom/
MIT License
4 stars 1 forks source link

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

Open pgangwani opened 4 months ago

pgangwani commented 4 months ago

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 commented 1 week ago

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.

pgangwani commented 1 week ago

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.