inputlogic / elements

Dependable UI Components for (P)React Apps
0 stars 0 forks source link

useForm: Add a `setFormData` to update form data at any time. #132

Closed coryschadt closed 4 years ago

staydecent commented 4 years ago

@coryschadt Could you explain the use case for this? For edit forms, you can load the existing data and then set it via initialData. You can also use preProcess to alter the data before sending it to the server.

So I'm left guessing this is to programmatically set a value on a form field that the end user should see?

If you can clarify this, then it will be easier to determine the best solution. 🙌

adriaanwm commented 4 years ago

@staydecent I can clarify this.

In alpha we had an edit form for user settings, on success of edit we wanted to clear one of the fields, but keep the rest as they were (the updated values)

If we used the clear function then the form would go back to initial data which was no longer correct for most fields. Preprocess also doesn't solve this problem.

staydecent commented 4 years ago

Ah yeah makes sense. The old clear let you pass in a new initialData would that be sufficient in this case?

clear(true, { fieldValueToKeep: 'Cool value' })

If so, would you expect it to extend from the initialData or replace it? ie.

    // Reset form data to initialData *plus* provided object when calling clear
    dataRef.current = Object.assign(
      {},
      initialData,
      valuesFromCallingClear
    )

or

    // reset form data to provided object *or* initialData
    dataRef.current = Object.assign(
      {},
      valuesFromCallingClear || initialData
    )

@adriaanwm

adriaanwm commented 4 years ago

I like option 2, replace initialData since I think it's less likely to cause any unexpected behavior.