package bonsai
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=1d68aab713659951eba5b85f21d6f9382e0efa8579a02c3be65d9071c6e86303
doc/bonsai.web_ui_form/Bonsai_web_ui_form/index.html
Module Bonsai_web_ui_form
Source
module Form_view := View
return
produces a bonsai form that will always produce the same value. set
and normalize
will do nothing to the form provided by this.
val return_settable :
(module Bonsai_web.Bonsai.Model with type t = 'a) ->
'a ->
'a t Bonsai_web.Computation.t
return_settable
is identical to return
, but set
and normalize
will update the value of the form.
return_error
produces a form that always fails validation.
val view_as_vdom :
?theme:Bonsai_web.View.Theme.t ->
?on_submit:'a Submit.t ->
?editable:Form_view.editable ->
'a t ->
Bonsai_web.Vdom.Node.t
view_as_vdom
produces the vdom representation of the form.
editable
defaults to `Yes_always
, which should be used when form input can't be disabled. `Currently_yes
allows editing, but generates less diff when toggled with `Currently_no
. When editable
is `Currently_no
, the view is wrapped in a fieldset that disables all of the inputs in the form.
Regardless of the value of editable
, scheduling the Form.set
effect will still change the values in the form.
Known bugs: While setting editable to `Currently_no prevents modification of most browser-builtin input elements, some custom form elements like the drag-and-drop, multiselect, and removing items using the pills in typeahead-multi for don't currently respect this and can be modified anyway. Work is underway to fix these.
set
fills the form with the provided value, setting the contents of form-elements if possible
normalize
sets the contents of a form to its current value. This only impacts values that have a "normalized" form. For example, a float-producing textbox being normalized might go from displaying "1.000" to "1."
Combines two forms into another one that produces both values from the inputs in tupled form.
Combines a list of forms into another that produces all values from the inputs in list form.
Combines a map of forms into another that produces all values from the inputs in map form.
project
is the powerhouse of the library; Using this function, you can change the type produced. Think of it like map
.
parse_exn
is a function that converts "forwards". As its name implies, you're free (and encouraged to) throw exceptions when the type conversion would fail.unparse
goes in the opposite direction. This one must not throw.
Example:
let _ : int Form.t =
project
(a: string Form.t)
~parse_exn:Int.of_string
~unparse:Int.to_string
The same as project
except that the parse
function is Or_error
returning.
validate
can provide additional validation of a value, but unlike project
or project'
, it doesn't change the type of the resulting form
Same as label
, but it lets you use an arbitrary vdom node instead of just a string.
Same as tooltip
, but it lets you use an arbitrary vdom node instead of just a string.
optional
takes a 'a t
and produces a 'a option t
when given a "some detector" and a token "none" value. is_some none
must be false.
Example:
let _ : string option t =
optional
(a: string t)
~is_some:(Fn.non String.is_empty)
~none:""
val optional' :
'a t ->
parse:('a -> 'b option Core.Or_error.t) ->
unparse:('b -> 'a) ->
none:'a ->
'b option t
An alternative "optional form" construction function; optional'
gives you the ability to produce the full set of parse options:
- Ok (Some b)
- Ok None
- Error error] while also converting to another type (
'a -> 'b option
) at the same time.
fallback_to
modifies the given form so that Ok default
is used as the form's value when it is in an Error
state.
Record_builder
is the primary way to compose form values using this library.
Unlike the rest of the API which operates on values of type Form.t
value values, they operate on Form.t Value.t
, and typically return Computation.t
.