shakacode / re-formality

Form validation tool for reason-react
https://re-formality.now.sh
MIT License
244 stars 35 forks source link

Passing form to child component #47

Closed lukashambsch closed 5 years ago

lukashambsch commented 5 years ago

I may be missing something easy here, but I want to trigger a save from outside the form component. So, I'm creating an event that my form will listen for to trigger the save. In order to do that I want to have access to the form state in the didMount function. So I'm creating a wrapper component passing the form as an attribute so I can access form.state inside didMount. Please let me know if there is a better approach I'm missing.

My problem with this approach is that reason isn't detecting the type correctly and I can't seem to figure out how to access the type from the Formality namespace. To be clear, I have a component like this:

<FormContainer initialState={name: person.name}>
  ...{form => <FormFieldsWrapper form />}
</FormContainer>

The form attr on FormFieldsWrapper is what is not being typed correctly. This could be wrong, but it looks to me like the type is Formality__Form.Form. I don't see that type exposed in Formality.re.

Thanks!

alex35mil commented 5 years ago

To get this type, you need to create Form module first:

module Form = Formality.Make(FormConfig);

// and then you can access type of `form` argument
type form = Form.interface;

I could make it generic to make it directly accessible like this:

Formality.interface(FormConfig.field, FormConfig.state, FormConfig.message)

But FormConfig is still required to construct this type and it's more verbose this way.

lukashambsch commented 5 years ago

Sorry for the delayed response. That helps a ton. Thanks!!