From ed63ed1b34df30544a5a2b977568937998a54d3c Mon Sep 17 00:00:00 2001 From: Medson de Oliveira Junior Date: Mon, 19 Mar 2018 17:20:57 -0300 Subject: [PATCH] Add reset form state helper. Closes #41 (#43) --- re/ReForm.re | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/re/ReForm.re b/re/ReForm.re index 32011bb7..64a21f50 100644 --- a/re/ReForm.re +++ b/re/ReForm.re @@ -30,7 +30,8 @@ module Create = (Config: Config) => { | HandleFieldValidation((Config.fields, value)) | HandleError(option(string)) | HandleChange((Config.fields, value)) - | HandleSubmit; + | HandleSubmit + | ResetFormState; type values = Config.state; type schema = list((Config.fields, Validation.validation(values))); module Field = { @@ -65,7 +66,8 @@ module Create = (Config: Config) => { type onSubmit = { values, setSubmitting: (bool) => unit, - setError: (option(string)) => unit + setError: (option(string)) => unit, + resetFormState: unit => unit }; type state = { @@ -104,6 +106,7 @@ module Create = (Config: Config) => { }, reducer: (action, state) => switch action { + | ResetFormState => ReasonReact.UpdateWithSideEffects({...state, values: initialState, errors: [], isSubmitting: false}, (self => onFormStateChange(self.state))) | HandleSubmitting(isSubmitting) => ReasonReact.UpdateWithSideEffects( {...state, isSubmitting}, @@ -155,6 +158,7 @@ module Create = (Config: Config) => { ( self => { onSubmit({ + resetFormState: () => self.send(ResetFormState), values: state.values, setSubmitting: isSubmitting => self.send(HandleSubmitting(isSubmitting)),