-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.elm
More file actions
107 lines (76 loc) · 2.15 KB
/
Main.elm
File metadata and controls
107 lines (76 loc) · 2.15 KB
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
module Main exposing (..)
import Html exposing (Html, text)
import Html.Events as E
import Html.Attributes as A
import Task
-- Main.elm
type alias Model =
{ value : Int, subModel : IntModel }
type Msg
= ConvertToKiB Int
| SubMsg (IntMsg Msg)
main : Program Never Model Msg
main =
Html.program
{ init = Model 0 (IntModel "") ! []
, update = update
, view = view
, subscriptions = \_ -> Sub.none
}
view : Model -> Html Msg
view model =
Html.div []
[ text "Value:"
, text <| toString model.value
, Html.br [] []
, Html.map SubMsg <| intView model.subModel
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
ConvertToKiB value ->
let
newModel =
{ model | value = value * 1024 }
in
newModel ! []
SubMsg sub ->
case sub of
UpMsg m ->
update m model
_ ->
let
( subModel, subCmd ) =
intUpdate ConvertToKiB sub model.subModel
in
{ model | subModel = subModel } ! [ Cmd.map SubMsg subCmd ]
-- IntField.elm
-- "component" source goes here
do : msg -> Cmd msg
do msg =
Task.perform identity <| Task.succeed msg
type alias IntModel =
{ string : String }
type IntMsg msg
= ValidateInput
| Input String
| UpMsg msg
intView : IntModel -> Html (IntMsg msg)
intView model =
Html.div []
[ Html.input [ E.onInput Input, A.value model.string ] []
, Html.button [ E.onClick ValidateInput ] [ text "OK" ]
]
intUpdate : (Int -> msg) -> IntMsg msg -> IntModel -> ( IntModel, Cmd (IntMsg msg) )
intUpdate upMsg msg model =
case msg of
Input s ->
{ model | string = s } ! []
ValidateInput ->
case String.toInt model.string of
Ok v ->
model ! [ do <| UpMsg <| upMsg v ]
Err e ->
model ! []
UpMsg _ ->
model ! []