aboutsummaryrefslogtreecommitdiff
path: root/js/elements.ml
blob: 783f791175addfff6493ebd139c4f6424c865ae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
open StdLabels

module Form = struct
  let label_for_field : ?id':Jstr.t -> Jstr.t -> Brr.El.t =
   fun ?id' label ->
    Brr.El.label
      ~at:
        Brr.At.
          [
            if_some (Option.map Brr.At.for' id');
            class' (Jstr.v "field-label is-normal");
          ]
      [ Brr.El.txt label ]

  let input_field :
      ?name:Jstr.t ->
      ?id':Jstr.t ->
      ?value':Jstr.t ->
      label:Jstr.t ->
      unit ->
      Brr.El.t =
   fun ?name ?id' ?(value' = Jstr.empty) ~label () ->
    let name' = name in
    let input =
      Brr.El.input
        ~at:
          Brr.At.
            [
              if_some (Option.map Brr.At.id id');
              if_some (Option.map Brr.At.name name');
              class' (Jstr.v "input");
              type' (Jstr.v "text");
              value value';
            ]
        ()
    and label = label_for_field ?id' label in

    Brr.El.div
      ~at:Brr.At.[ class' (Jstr.v "field is-horizontal") ]
      [
        label; Brr.El.div ~at:Brr.At.[ class' (Jstr.v "field-body") ] [ input ];
      ]

  type choice_value = {
    id' : Jstr.t option;
    label : Jstr.t;
    value : Jstr.t;
    checked : bool;
  }

  let radio :
      ?name:Jstr.t ->
      ?values:choice_value list ->
      label:Jstr.t ->
      unit ->
      Brr.El.t =
   fun ?name ?(values = []) ~label () ->
    let name' = name in
    let label = label_for_field label in

    let radios =
      List.map values ~f:(fun entry ->
          (* This is the element receiving the events *)
          let input =
            Brr.El.input
              ~at:
                Brr.At.
                  [
                    if_some (Option.map Brr.At.name name');
                    if_some (Option.map Brr.At.id entry.id');
                    if' entry.checked Brr.At.checked;
                    type' (Jstr.v "radio");
                    value entry.value;
                  ]
              ()
          in

          Brr.El.label
            ~at:Brr.At.[ class' (Jstr.v "radio") ]
            [
              input;
              Brr.El.span
                ~at:Brr.At.[ class' (Jstr.v "control-label") ]
                [ Brr.El.nbsp (); Brr.El.txt entry.label ];
            ])
    in

    let form_entry =
      Brr.El.div
        ~at:Brr.At.[ class' (Jstr.v "field has-check is-horizontal") ]
        [
          label;
          Brr.El.div
            ~at:Brr.At.[ class' (Jstr.v "field-body") ]
            [
              Brr.El.div
                ~at:Brr.At.[ class' (Jstr.v "field") ]
                [
                  Brr.El.div
                    ~at:
                      Brr.At.
                        [
                          class' (Jstr.v "field is-grouped-multine is-grouped");
                        ]
                    [
                      Brr.El.div ~at:Brr.At.[ class' (Jstr.v "radios") ] radios;
                    ];
                ];
            ];
        ]
    in
    (* Report each change_event to the main html element  *)
    form_entry

  let submit : ?value':Jstr.t -> unit -> Brr.El.t =
   fun ?(value' = Jstr.empty) () ->
    Brr.El.div
      ~at:Brr.At.[ class' (Jstr.v "field is-horizontal") ]
      [
        Brr.El.div ~at:Brr.At.[ class' (Jstr.v "field-label") ] [];
        Brr.El.div
          ~at:Brr.At.[ class' (Jstr.v "field-body") ]
          [
            Brr.El.input
              ~at:
                Brr.At.
                  [
                    class' (Jstr.v "button is-primary");
                    type' (Jstr.v "submit");
                    value value';
                  ]
              ();
          ];
      ]
end