form-atoms / form-atoms

Atomic form primitives for Jotai

Home Page:https://codesandbox.io/s/getting-started-with-form-atoms-v2-ddhgq2?file=/src/App.tsx

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`FormAtomValues` should accept `FormAtom` instead of `FormAtomFields`

MiroslavPetrik opened this issue · comments

Describe the bug
The naming of FormAtomValues indicates that given form atom, it will return the form values.
The problem is it accepts form fields, not form object thus making it confusing.

To Reproduce

const userForm = formAtom({
  first: fieldAtom({
    value: "",
  }),
  last: fieldAtom({
    value: "",
  }),
});

type Values = FormAtomValues<typeof userForm> // ERR!
// The helper type in reality accepts `FormAtomFields`
// and thus should be named `FormAtomFieldsValues`


// Currently custom helper is needed!
type FormValues<Form extends FormAtom<any>> = Form extends FormAtom<
  infer Fields
>
  ? FormAtomValues<Fields>
  : never;

type Values = FormValues<typeof userForm>

Expected behavior

  1. the call to FormAtomValues should work when given FormAtom.
  2. The existent FormAtomValues can be renamed to FormFieldsValues or FieldsValues or FieldAtomsValues ...

Why this makes sense

  1. Clear definition of form & fields
    1. fields is object containing the fieldAtoms
    2. form is return value of formAtom which accepts the fields
  2. Simple forms can be constructed without having a separate variable for the fields. Currently I often move the argument back and forth which is not good.
  3. This would be consistent with the hook useFormAtomValues which takes formAtom as argument, not form fields.

Additional context

  • docs must be updated

Here's the documentation on the API updates:
https://github.com/jaredLunde/form-atoms/blob/acbee934097d90e27a69825dd907e9428a80fce0/README.md#formvalues

Would love your feedback! You've been super insightful and instrumental in improving this library.

I've updated my calls to the new & improved ones and I love it 😍

Thank you for your work.