diff options
Diffstat (limited to 'test/examples/components.gleam')
-rw-r--r-- | test/examples/components.gleam | 102 |
1 files changed, 0 insertions, 102 deletions
diff --git a/test/examples/components.gleam b/test/examples/components.gleam deleted file mode 100644 index 722b796..0000000 --- a/test/examples/components.gleam +++ /dev/null @@ -1,102 +0,0 @@ -// IMPORTS --------------------------------------------------------------------- - -import gleam/dynamic -import gleam/int -import gleam/list -import gleam/map -import gleam/option.{Some} -import gleam/result -import lustre -import lustre/attribute -import lustre/effect -import lustre/element.{element, text} -import lustre/element/html.{button, div, li, ol, p, slot} -import lustre/event - -// MAIN ------------------------------------------------------------------------ - -pub fn main() { - let assert Ok(_) = - lustre.component( - "custom-counter", - counter_init, - counter_update, - counter_render, - map.from_list([ - #( - "count", - fn(attr) { - dynamic.int(attr) - |> result.map(GotCount) - }, - ), - ]), - ) - - // A `simple` lustre application doesn't produce `Effect`s. These are best to - // start with if you're just getting started with lustre or you know you don't - // need the runtime to manage any side effects. - let app = lustre.simple(init, update, render) - let assert Ok(_) = lustre.start(app, "[data-lustre-app]") - - Nil -} - -fn init() { - [] -} - -fn update(history, msg) { - case msg { - "reset" -> [] - _ -> [msg, ..history] - } -} - -fn render(history) { - let on_custom_click = { - use _ <- event.on("custom-click") - Some("click") - } - div( - [], - [ - button([event.on_click("reset")], [text("Reset")]), - ol([], list.map(history, fn(msg) { li([], [text(msg)]) })), - element( - "custom-counter", - [on_custom_click, attribute.property("count", list.length(history))], - [ol([], list.map(history, fn(msg) { li([], [text(msg)]) }))], - ), - ], - ) -} - -// COUNTER --------------------------------------------------------------------- - -fn counter_init() { - #(0, effect.none()) -} - -type CounterMsg { - GotCount(Int) - Clicked -} - -fn counter_update(count, msg) { - case msg { - GotCount(count) -> #(count, effect.none()) - Clicked -> #(count, event.emit("custom-click", Nil)) - } -} - -fn counter_render(count) { - div( - [], - [ - button([event.on_click(Clicked)], [text("Click me!")]), - p([], [text("Count: "), text(int.to_string(count))]), - slot([]), - ], - ) -} |