final-form / final-form

🏁 Framework agnostic, high performance, subscription-based form state management

Home Page:https://final-form.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Include data in resulting object from form.getState()

msageryd opened this issue · comments

feature request

I've just discovered mutators. They are awesome!
I'm adding data to my fields with final-form-set-field-data. I also learned that I can initialise the data property when registering the fields. (#240 (comment))

Since data seems to be the decided property name for arbitrary field data it would be great to get the data property for all fields from form.getState().

Proposed solution

This feature would be simple to implement by piggy-backing on the existing reduce function in calcNextFormState()

const { modified, touched, visited } = safeFieldKeys.reduce(

My user-land solution is to reduce over getRegisteredFields at submit, like this:

const data = form.getRegisteredFields().reduce(function(result, key) {
    result[key] = form.getFieldState(key).data;
    return result;
}, {});

I would really like form.getState() to output a data-object. I also think others would benefit from this.

I could make a PR if this is interesting.