Skip to content

Commit

Permalink
fields: return var when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
voodoos committed Oct 18, 2024
1 parent 279f6a4 commit 3205177
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
5 changes: 3 additions & 2 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ let app =
in
make { name = "view-order"; default = "desc"; label = [] } options
in
let f_sort_order = Lwd.pair f_sort.value f_order.value in
let f_sort_order = Lwd.pair (Lwd.get f_sort.value) (Lwd.get f_order.value) in
let filters, f_value =
let f_libraries =
let open Field_checkboxes in
Expand Down Expand Up @@ -110,7 +110,8 @@ let app =
let main_view =
let previous_value = ref None in
let request =
Ui_utils.map3 f_value f_search.value f_sort_order ~f:(fun l t (s, _o) ->
Ui_utils.map3 f_value (Lwd.get f_search.value) f_sort_order
~f:(fun l t (s, _o) ->
let filters = Option.map (fun s -> [ Db.View.Search s ]) t in
Console.log
[
Expand Down
2 changes: 1 addition & 1 deletion vendor/brr_lwd_ui/lib/forms/field_checkboxes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type label = Elwd.t Elwd.col
type 'value desc = Check of 'value * label * checked
(* TODO | Group of label * 'value desc list *)

type 'value t = { name : string; desc : 'value desc Lwd_seq.t Lwd.t }
type 'value group = { name : string; desc : 'value desc Lwd_seq.t Lwd.t }
type 'a reactive_field = { field : Elwd.t Lwd.t; value : 'a Lwd.t }

let name ~g ~n ~id base_name =
Expand Down
18 changes: 9 additions & 9 deletions vendor/brr_lwd_ui/lib/forms/field_select.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open! Brr_lwd
type 'a reactive_field = {
field : Elwd.t Lwd.t;
label : Elwd.t Lwd.t;
value : 'a Lwd.t;
value : 'a Lwd.var;
}

let name ~id base_name =
Expand All @@ -15,7 +15,7 @@ let name ~id base_name =
let make ?(at = []) ?(ev = []) (desc : string Field.desc) options =
let id = name ~id:true desc.name in
let name = name ~id:false desc.name in
let var = Persistent.var ~key:id desc.default in
let value = Persistent.var ~key:id desc.default in
let label = Elwd.label ~at:[ `P (At.for' (Jstr.v id)) ] desc.label in
let field =
let at =
Expand All @@ -27,23 +27,23 @@ let make ?(at = []) ?(ev = []) (desc : string Field.desc) options =
let on_change =
Elwd.handler Ev.change (fun ev ->
let t = Ev.target ev |> Ev.target_to_jv in
let value = Jv.get t "value" in
Lwd.set var (Jv.to_string value))
let value' = Jv.get t "value" in
Lwd.set value (Jv.to_string value'))
in
let ev = `P on_change :: ev in
let options =
Lwd_seq.map
(fun (value, name) ->
(fun (value', name) ->
let open Attrs.O in
let at = v (`P (A (At.value @@ Jstr.v value))) in
let at = v (`P (A (At.value @@ Jstr.v value'))) in
let selected =
Lwd.map (Lwd.get var) ~f:(fun selected ->
A (At.if' (Equal.poly selected value) At.selected))
Lwd.map (Lwd.get value) ~f:(fun selected ->
A (At.if' (Equal.poly selected value') At.selected))
in
let at = `R selected @:: at in
Elwd.option ~at [ `P (El.txt' name) ])
options
in
Elwd.select ~at ~ev [ `S (Lwd_seq.lift options) ]
in
{ field; label; value = Lwd.get var }
{ field; label; value }
12 changes: 6 additions & 6 deletions vendor/brr_lwd_ui/lib/forms/field_textinput.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open! Brr_lwd
type 'a reactive_field = {
field : Elwd.t Lwd.t;
label : Elwd.t Lwd.t;
value : 'a Lwd.t;
value : 'a Lwd.var;
}

let name ~id base_name =
Expand All @@ -16,7 +16,7 @@ let name ~id base_name =
let make ?(at = []) ?(ev = []) (desc : string option Field.desc) =
let id = name ~id:true desc.name in
let name = name ~id:false desc.name in
let var = Persistent.var ~key:id desc.default in
let value = Persistent.var ~key:id desc.default in
let label = Elwd.label ~at:[ `P (At.for' (Jstr.v id)) ] desc.label in
let field =
let at =
Expand All @@ -26,17 +26,17 @@ let make ?(at = []) ?(ev = []) (desc : string option Field.desc) =
|> add At.Name.type' (`P "text")
in
let at =
match Lwd.peek var with
match Lwd.peek value with
| Some v -> `P (At.value @@ Jstr.v v) :: at
| None -> at
in
let on_change =
Elwd.handler Ev.keyup (fun ev ->
let t = Ev.target ev |> Ev.target_to_jv in
let value = Jv.get t "value" in
Lwd.set var (Some (Jv.to_string value)))
let value' = Jv.get t "value" in
Lwd.set value (Some (Jv.to_string value')))
in
let ev = `P on_change :: ev in
Elwd.input ~at ~ev ()
in
{ field; label; value = Lwd.get var }
{ field; label; value }

0 comments on commit 3205177

Please sign in to comment.