-
Notifications
You must be signed in to change notification settings - Fork 22
/
Main.elm
90 lines (70 loc) · 2.16 KB
/
Main.elm
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
module Main exposing (Model, Msg(..), init, main, subscriptions, update, view)
import Browser
import Helper
import Html exposing (Html, div, text)
import Input
main : Program () Model Msg
main =
Browser.element
{ view = view
, init = \() -> init
, update = update
, subscriptions = subscriptions
}
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
type alias Model =
{ name : Input.Model
, helper : Helper.Model
}
init : ( Model, Cmd Msg )
init =
let
-- Omit the Cmd from child's init, because we know that it's Cmd.none
( nameModel, _ ) =
Input.init "John"
( helperModel, _ ) =
Helper.init "Please enter your name"
in
( Model nameModel helperModel, Cmd.none )
type Msg
= NameMsg Input.Msg
| HelperMsg Helper.Msg
view : Model -> Html Msg
view model =
div []
[ text model.name
, Html.map NameMsg (Input.view model.name)
, Html.map HelperMsg (Helper.view model.helper)
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NameMsg childMsg ->
case childMsg of
{- We have intercepted a message from child module.
This part of the update function might be moved
to a separate function for better readability.
-}
Input.Focus ->
update (HelperMsg Helper.Show) model
Input.Blur ->
update (HelperMsg Helper.Hide) model
-- The default message passing routine.
_ ->
let
( nameModel, nameCmd ) =
Input.update childMsg model.name
in
( { model | name = nameModel }
, Cmd.map NameMsg nameCmd
)
HelperMsg childMsg ->
let
( helperModel, helperCmd ) =
Helper.update childMsg model.helper
in
( { model | helper = helperModel }
, Cmd.map HelperMsg helperCmd
)