Skip to content

Commit

Permalink
Adding selection callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
akoprow committed Mar 12, 2012
1 parent d00622b commit a28c6d7
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions select.opa
Original file line number Original file line Diff line number Diff line change
@@ -1,24 +1,38 @@
import stdlib.themes.bootstrap import stdlib.themes.bootstrap


function selectOptions(list('a) opts) { function select(({string id} or {autogen}) id, list('a) options, ('a -> void) callback) {
function render_option(o) { id =
<option>{o}</> match (id) {
case {id: user_id}: user_id;
case {autogen}: Dom.fresh_id()
}
function render_option(i, o) {
<option value={i}>{o}</>
} }
<>{List.map(render_option, opts)}</> function changed(_evt) {
sel_val = List.unsafe_get(String.to_int(Dom.get_value(#{id})), options);
callback(sel_val);
}
<select id={id} onchange={changed}>
{List.mapi(render_option, options)}
</>
}

function selected(int number) {
#selectMsg = <>You selected {number}</>
} }


function page() { function page() {
list(int) numberRange = [2, 3, 4, 5, 6, 7, 8, 9] list(int) numberRange = [2, 3, 4, 5, 6, 7, 8, 9];
<div class=container> <div class=container>
<form class=form-horizontal> <form class=form-horizontal>
<legend>Simple selection demo with Opa</> <legend>Simple selection demo with Opa</>
<fieldset> <fieldset>
<div class=control-group> <div class=control-group>
<label class=control-label for=select01>Make your choice</> <label class=control-label for=select01>Make your choice</>
<div class=controls> <div class=controls>
<select id=select01> {select({id: "select01"}, numberRange, selected)}
{selectOptions(numberRange)} <p id=#selectMsg class=help-block>You did not make any selection</>
</>
</> </>
</> </>
</> </>
Expand All @@ -27,4 +41,4 @@ function page() {
} }


// simple one-page server // simple one-page server
Server.start(Server.http, {title: "Form selection in Opa", ~page}) Server.start(Server.http, {title: "Form selection in Opa", ~page});

0 comments on commit a28c6d7

Please sign in to comment.