aboutsummaryrefslogtreecommitdiff
path: root/examples/03-controlled-inputs/src/app.gleam
blob: babef7a2b8db6ce12d1cbb89ec8d34579ea09760 (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
import gleam/int
import gleam/string
import lustre
import lustre/attribute
import lustre/element.{type Element}
import lustre/event
// These examples are written with lustre_ui in mind. They'll work regardless,
// but to see what lustre_ui can do make sure to run each of these examples with
// the `--include-styles` flag:
//
//   $ gleam run -m lustre/try -- --include-styles
//
// In your own apps, make sure to add the `lustre_ui` dependency and include the
// stylesheet somewhere.
import lustre/ui
import lustre/ui/aside

// MAIN ------------------------------------------------------------------------

pub fn main() {
  let app = lustre.simple(init, update, view)
  let assert Ok(_) = lustre.start(app, "#app", Nil)
}

// MODEL -----------------------------------------------------------------------

type Model {
  Model(value: String, length: Int, max: Int)
}

fn init(_) -> Model {
  Model(value: "", length: 0, max: 10)
}

// UPDATE ----------------------------------------------------------------------

pub opaque type Msg {
  GotInput(value: String)
  Reset
}

fn update(model: Model, msg: Msg) -> Model {
  case msg {
    GotInput(value) -> {
      let length = string.length(value)

      case length <= model.max {
        True -> Model(..model, value: value, length: length)
        False -> model
      }
    }
    Reset -> Model(..model, value: "", length: 0)
  }
}

// VIEW ------------------------------------------------------------------------

fn view(model: Model) -> Element(Msg) {
  let styles = [#("width", "100vw"), #("height", "100vh"), #("padding", "1rem")]
  let length = int.to_string(model.length)
  let max = int.to_string(model.max)

  ui.centre(
    [attribute.style(styles)],
    ui.aside(
      [aside.content_first(), aside.align_centre()],
      ui.field(
        [],
        [element.text("Write a message:")],
        ui.input([attribute.value(model.value), event.on_input(GotInput)]),
        [element.text(length <> "/" <> max)],
      ),
      ui.button([event.on_click(Reset)], [element.text("Reset")]),
    ),
  )
}