TyOverby / bonsai.red

4 stars 2 forks source link

Form examples won't compile #2

Open slipalong opened 1 year ago

slipalong commented 1 year ago

Hi Ty,

am trying to learn Bonsai (very challenging LOL) and been trying to follow your examples on bonsai.red. Ive hit an issue trying to work through the forms examples due to the labels_for_variants not being rendered (unused-value-declaration) any chance you can modify your examples so that can be compiled?

open! Core
open! Bonsai
open! Bonsai_web
open! Bonsai_web_ui_form
open Bonsai.Let_syntax

module Form = Bonsai_web_ui_form

type t =
  { some_string : string
  ; an_int : int
  ; on_or_off : bool
  }
[@@deriving typed_fields, sexp_of]

let form_of_t : t Form.t Computation.t =
  Form.Typed.Record.make
    (module struct
      (* reimport the module that typed_fields just derived *)
      module Typed_field = Typed_field

      let label_for_field = `Inferred

      (* provide a form computation for each field in the record *)
      let form_for_field : type a. a Typed_field.t -> a Form.t Computation.t = function
        | Some_string -> Form.Elements.Textbox.string [%here]
        | An_int -> Form.Elements.Number.int ~default:0 ~step:1 [%here] ()
        | On_or_off -> Form.Elements.Checkbox.bool ~default:false [%here]

    end)

type v =
  | A
  | B of int
  | C of string
[@@deriving typed_variants, sexp_of]

let form_of_v : v Form.t Computation.t =
  Form.Typed.Variant.make
    (module struct
      (* reimport the module that typed_fields just derived *)
      module Typed_variant = Typed_variant_of_v

      let label_for_variant = `Inferred

      (* provide a form computation for constructor in the variant *)
      let form_for_variant : type a. a Typed_variant.t -> a Form.t Computation.t
        = function
          | A -> Bonsai.const (Form.return ())
          | B -> Form.Elements.Textbox.int [%here]
          | C -> Form.Elements.Textbox.string [%here]

    end)

let view_for_form : Vdom.Node.t Computation.t =
  let%sub form_t = form_of_t in
  let%sub form_v = form_of_v in
  let%arr form_t = form_t
  and form_v = form_v in
  let form = Form.both form_t form_v in
  let value = Form.value form in
  Vdom.Node.div
    [ Form.view_as_vdom form
    ; Vdom.Node.sexp_for_debugging ([%sexp_of: (t * v) Or_error.t] value)
    ]

  let (_ : _ Start.Handle.t) =
  Start.start Start.Result_spec.just_the_view ~bind_to_element_with_id:"app" view_for_form
TyOverby commented 1 year ago

Can you provide the error message that you're seeing?

TyOverby commented 1 year ago

Oh, and could you list the versions for all the opam packages? The examples on bonsai.red only build against the tip of tree (https://github.com/janestreet/bonsai) and not any of the versions on opam