@@ -72,16 +72,20 @@ import Web.Event.Event (Event)
7272import Web.Event.Event as Event
7373import Web.UIEvent.FocusEvent (FocusEvent )
7474
75+ -- | A type synonym which picks the `input` type from a form field.
7576type FieldInput :: Type -> Type -> Type -> Type
7677type FieldInput input error output = input
7778
79+ -- | A type synonym which represents the current state of a form field.
7880type FieldState :: Type -> Type -> Type -> Type
7981type FieldState input error output =
8082 { initialValue :: input
8183 , value :: input
8284 , result :: Maybe (Either error output )
8385 }
8486
87+ -- | A type synonym which represents the available actions that can be called
88+ -- | on a form field.
8589type FieldAction :: Type -> Type -> Type -> Type -> Type
8690type FieldAction action input error output =
8791 { key :: String
@@ -92,9 +96,12 @@ type FieldAction action input error output =
9296 , handleBlur :: FocusEvent -> action
9397 }
9498
99+ -- | A type synonm which represents a pure validation function for a form field.
95100type FieldValidation :: Type -> Type -> Type -> Type
96101type FieldValidation input error output = input -> Either error output
97102
103+ -- | Validate a variant of form field inputs by providing a record of validation
104+ -- | functions, one per possible case in the variant. Used for pure validation.
98105validate
99106 :: forall validators xs r1 r2 r3 inputs results
100107 . RL.RowToList validators xs
@@ -106,9 +113,15 @@ validate
106113 -> Variant results
107114validate = flip Variant .over
108115
116+ -- | A type synonm which represents an effectful validation function for a form
117+ -- | field.
109118type FieldValidationM :: (Type -> Type ) -> Type -> Type -> Type -> Type
110119type FieldValidationM m input error output = input -> m (Either error output )
111120
121+ -- | Validate a variant of form field inputs by providing a record of validation
122+ -- | functions, one per possible case in the variant. Used for effectful
123+ -- | validation. It is possible to use `HalogenM` as your validation monad,
124+ -- | which gives you full access to your component state (including form fields).
112125validateM
113126 :: forall xs r1 r2 r3 inputs validators results m
114127 . RL.RowToList validators xs
@@ -121,12 +134,16 @@ validateM
121134 -> m (Variant results )
122135validateM = flip Variant .traverse
123136
137+ -- | A type synonym which represents the result of validating the `input` type
138+ -- | of a form field to produce either its `error` or `output`.
124139type FieldResult :: Type -> Type -> Type -> Type
125140type FieldResult input error output = Either error output
126141
142+ -- | A type synonym which picks the `output` type from a form field.
127143type FieldOutput :: Type -> Type -> Type -> Type
128144type FieldOutput input error output = output
129145
146+ -- | Available settings for controlling the form's behavior.
130147type FormConfig =
131148 { validateOnBlur :: Boolean
132149 , validateOnChange :: Boolean
@@ -140,6 +157,32 @@ type InitialFormConfig fields action =
140157 | OptionalFormConfig
141158 }
142159
160+ -- | Formless uses queries to notify you when important events happen in your
161+ -- | form. You are expected to handle these queries in your `handleQuery`
162+ -- | function so that Formless works correctly.
163+ -- |
164+ -- | You can use `handleSubmitValidate` or `handleSubmitValidateM` if you only
165+ -- | need to handle form submission and validation events.
166+ -- |
167+ -- | - **Query**
168+ -- | Formless has proxied a query from the parent component to you. You are
169+ -- | expected to handle the query.
170+ -- |
171+ -- | - **Validate**
172+ -- | A field needs to be validated. You are expected to validate the field and
173+ -- | return its validated result. You can use the `validate` and `validateM`
174+ -- | functions provided for Formless to perform validation.
175+ -- |
176+ -- | - **SubmitAttempt**
177+ -- | The form was submitted, but not all fields passed validation. You are
178+ -- | given a record of all form fields along with their validation result.
179+ -- |
180+ -- | - **Submit**
181+ -- | The form was submitted and all fields passed validation. You are given
182+ -- | a record of the validated output types for every field in the form.
183+ -- |
184+ -- | - **Reset**
185+ -- | The form was reset to its initial state.
143186data FormQuery :: (Type -> Type ) -> Row Type -> Row Type -> Row Type -> Type -> Type
144187data FormQuery query inputs results outputs a
145188 = Query (query a )
@@ -149,7 +192,8 @@ data FormQuery query inputs results outputs a
149192 | Reset a
150193
151194-- | A default implementation for `handleQuery` which only handles successful
152- -- | submission and validation events.
195+ -- | submission and validation events, used when you only need non-monadic
196+ -- | validation. See `FormQuery` for all available events in Formless.
153197handleSubmitValidate
154198 :: forall inputs fields validators results outputs query state action slots output m a
155199 . ({ | outputs } -> H.HalogenM state action slots (FormOutput fields output ) m Unit )
@@ -167,7 +211,8 @@ handleSubmitValidate onSubmit validate' validators = case _ of
167211 pure Nothing
168212
169213-- | A default implementation for `handleQuery` which only handles successful
170- -- | submission and validation events.
214+ -- | submission and validation events, used when you need monadic validation.
215+ -- | See `FormQuery` for all available events in Formless.
171216handleSubmitValidateM
172217 :: forall inputs fields validators results outputs query state action slots output m a
173218 . ({ | outputs } -> H.HalogenM state action slots (FormOutput fields output ) m Unit )
@@ -185,6 +230,7 @@ handleSubmitValidateM onSubmit validateM' validators = case _ of
185230 _ ->
186231 pure Nothing
187232
233+ -- | Available form-wide actions you can tell Formless to do.
188234type FormAction fields action =
189235 { setFields :: { | fields } -> action
190236 , reset :: action
@@ -193,12 +239,16 @@ type FormAction fields action =
193239 , handleSubmit :: Event -> action
194240 }
195241
242+ -- | The summary state of the entire form.
196243type FormState =
197244 { errorCount :: Int
198245 , submitCount :: Int
199246 , allTouched :: Boolean
200247 }
201248
249+ -- | The full form context which is provided to you. It includes any component
250+ -- | input you wish to take, along with the current state of the form fields and
251+ -- | the form and a set of actions you can use on the form fields or the form.
202252type FormContext :: Row Type -> Row Type -> Type -> Type -> Type
203253type FormContext fields actions input action =
204254 { input :: input
@@ -208,23 +258,39 @@ type FormContext fields actions input action =
208258 , formActions :: FormAction fields action
209259 }
210260
261+ -- | Formless uses `FormOutput` to let you notify it of events it should handle.
262+ -- |
263+ -- | - **Raise**
264+ -- | Tell Formless to proxy the provided output to the parent component.
265+ -- |
266+ -- | - **Eval**
267+ -- | Tell Formless to evaluate an action on the form. Actions are provided to
268+ -- | you via the `FormContext`, which gives you actions you can call on
269+ -- | individual form fields or on the form as a whole.
211270data FormOutput :: Row Type -> Type -> Type
212271data FormOutput fields output
213272 = Raise output
214273 | Eval (FormlessAction fields )
215274
275+ -- | A drop-in replacement for Halogen's `raise` function, which you can use to
276+ -- | proxy output through Formless up to the parent component.
216277raise
217278 :: forall fields state action slots output m
218279 . output
219280 -> H.HalogenM state action slots (FormOutput fields output ) m Unit
220281raise = H .raise <<< Raise
221282
283+ -- | Tell Formless to evaluate a Formless action.
222284eval
223285 :: forall fields state action slots output m
224286 . FormlessAction fields
225287 -> H.HalogenM state action slots (FormOutput fields output ) m Unit
226288eval = H .raise <<< Eval
227289
290+ -- | Internal actions that Formless evaluates to modify the state of the form.
291+ --
292+ -- These constructors should never be exported. These actions are only provided
293+ -- to the user via the form context after being constructed by Formless.
228294data FormlessAction :: Row Type -> Type
229295data FormlessAction fields
230296 = SubmitForm (Maybe Event )
@@ -253,6 +319,12 @@ type InternalFormState fields actions input action =
253319 , formConfig :: FormConfig
254320 }
255321
322+ -- | The Formless higher-order component. Expects a form configuration, the
323+ -- | initial input values for each field in the form, and the component that
324+ -- | you want to provide `FormContext` to (ie. your form component).
325+ -- |
326+ -- | Please see the Formless README.md and examples directory for a thorough
327+ -- | description of this component.
256328formless
257329 :: forall config inputs fields actions results outputs query action input output m
258330 . MonadEffect m
0 commit comments