noble-ai / rescript-fields

Elaborate form validation for Rescript
https://noble-ai.github.io/rescript-fields
MIT License
1 stars 1 forks source link

Supplying an `~init=None` to a `Form.use(.)` results in no field updates to the form. #2

Open KateChanthakaew opened 2 weeks ago

KateChanthakaew commented 2 weeks ago

Description

When a user supplies an ~init=None, subsequent updates made to the form via field.actions.set(value) do not result in form Store updates or changes.

Reproduction

Example File: rescript-fields/examples/login/Login.res

Using the example files and documentation, calling a field.actions.set() from an onChange is not setting values to the form.

Form Instantiation:

 let form = Form.use(. 
    ~context=contextValidate,
    ~init=None,
    ~validateInit=false,
  )

Js.log2("form.field: ", form.field->Field.show)

Handler Verification:

        onChange={e => {
          let target = e->ReactEvent.Form.target
          Js.log2("target.value: ", target["value"])
          target["value"]->form.actions.set
        }}

When typing in the form input fields, there is a target["value"] for every key stroke, but as seen by the form's show method logs, form.field->Field.show no updates are being stored to the form.

VDO:

https://github.com/user-attachments/assets/928b7379-eb26-48bc-8d3a-cb9b72c48ddd

Expected Behavior

form.field should always update when input field.action.set(value) is called.

Working Example

If we provide Form.use(.~init=Some(Field.input), ...) and the FieldType is not FieldArray, the form is working as expected. FieldArray's bugs will be noted in a different git-hub issue.

let init: Field.input = {
  username: "",
  password: "",
}

let form = `Form.use(.`
    ~context=contextValidate,
    ~init=some(init),
    ~validateInit=false,
 )

VDO:

https://github.com/user-attachments/assets/09c234fc-b8bb-4ed0-b3d1-6cee6891a260