Skip to content

Commit d78d024

Browse files
committed
Switch to examples that match the guide
1 parent c0094ab commit d78d024

File tree

2 files changed

+97
-87
lines changed

2 files changed

+97
-87
lines changed

src/examples/checkboxes.elm

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
1-
import Html exposing (..)
2-
import Html.App exposing (beginnerProgram)
3-
import Html.Attributes exposing (..)
4-
import Html.Events exposing (onCheck)
1+
import Html exposing (Html, fieldset, input, label, text)
2+
import Html.App as App
3+
import Html.Attributes exposing (style, type')
4+
import Html.Events exposing (onClick)
5+
56

67

78
main =
8-
beginnerProgram { model = model, view = view, update = update }
9+
App.beginnerProgram { model = optOut, update = update, view = view }
910

1011

1112

1213
-- MODEL
1314

1415

1516
type alias Model =
16-
{ red : Bool
17-
, underline : Bool
18-
, bold : Bool
17+
{ notifications : Bool
18+
, autoplay : Bool
19+
, location : Bool
1920
}
2021

2122

22-
model : Model
23-
model =
24-
Model False False True
23+
optOut : Model
24+
optOut =
25+
Model True True True
2526

2627

2728

2829
-- UPDATE
2930

3031

3132
type Msg
32-
= Red Bool
33-
| Underline Bool
34-
| Bold Bool
33+
= ToggleNotifications
34+
| ToggleAutoplay
35+
| ToggleLocation
3536

3637

3738
update : Msg -> Model -> Model
3839
update msg model =
3940
case msg of
40-
Red bool ->
41-
{ model | red = bool }
41+
ToggleNotifications ->
42+
{ model | notifications = not model.notifications }
4243

43-
Underline bool ->
44-
{ model | underline = bool }
44+
ToggleAutoplay ->
45+
{ model | autoplay = not model.autoplay }
4546

46-
Bold bool ->
47-
{ model | bold = bool }
47+
ToggleLocation ->
48+
{ model | location = not model.location }
4849

4950

5051

@@ -53,34 +54,18 @@ update msg model =
5354

5455
view : Model -> Html Msg
5556
view model =
56-
div []
57-
[ span [toStyle model] [text "Hello, how are you?!"]
58-
, label []
59-
[ br [] []
60-
, input [ type' "checkbox", checked model.red, onCheck Red ] []
61-
, text "red"
62-
]
63-
, label []
64-
[ br [] []
65-
, input [ type' "checkbox", checked model.underline, onCheck Underline ] []
66-
, text "underline"
67-
]
68-
, label []
69-
[ br [] []
70-
, input [ type' "checkbox", checked model.bold, onCheck Bold ] []
71-
, text "bold"
72-
]
57+
fieldset []
58+
[ checkbox ToggleNotifications "Email Notifications"
59+
, checkbox ToggleAutoplay "Video Autoplay"
60+
, checkbox ToggleLocation "Use Location"
7361
]
7462

7563

76-
toStyle : Model -> Attribute msg
77-
toStyle model =
78-
style
79-
[ ("color", if model.red then "red" else "black")
80-
, ("text-decoration", if model.underline then "underline" else "none")
81-
, ("font-weight", if model.bold then "bold" else "normal")
64+
checkbox : msg -> String -> Html msg
65+
checkbox msg name =
66+
label
67+
[ style [("padding", "20px")]
68+
]
69+
[ input [ type' "checkbox", onClick msg ] []
70+
, text name
8271
]
83-
84-
85-
-- Exercise: move the repetative code in `view` into a separate function
86-
-- and use it three times.

src/examples/radio-buttons.elm

Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,66 @@
1-
import Html exposing (Html, Attribute, br, div, input, label, span, text)
2-
import Html.App exposing (beginnerProgram)
3-
import Html.Attributes exposing (..)
4-
import Html.Events exposing (onCheck)
1+
import Html exposing (Html, Attribute, div, fieldset, input, label, text)
2+
import Html.App as App
3+
import Html.Attributes exposing (name, style, type')
4+
import Html.Events exposing (onClick)
5+
import Markdown
6+
57

68

79
main =
8-
beginnerProgram { model = model, view = view, update = update }
10+
App.beginnerProgram { model = chapter1, update = update, view = view }
911

1012

1113

1214
-- MODEL
1315

1416

1517
type alias Model =
16-
{ style : Style
18+
{ fontSize : FontSize
19+
, content : String
1720
}
1821

1922

20-
type Style
21-
= Red
22-
| Underline
23-
| Bold
23+
type FontSize
24+
= Small
25+
| Medium
26+
| Large
27+
28+
29+
chapter1 : Model
30+
chapter1 =
31+
Model Medium intro
32+
33+
34+
intro : String
35+
intro = """
36+
37+
# Anna Karenina
2438
39+
## Chapter 1
2540
26-
model =
27-
{ style = Bold }
41+
Happy families are all alike; every unhappy family is unhappy in its own way.
42+
43+
Everything was in confusion in the Oblonskys’ house. The wife had discovered
44+
that the husband was carrying on an intrigue with a French girl, who had been
45+
a governess in their family, and she had announced to her husband that she
46+
could not go on living in the same house with him...
47+
48+
"""
2849

2950

3051

3152
-- UPDATE
3253

3354

34-
type Msg =
35-
Switch Style
55+
type Msg
56+
= SwitchTo FontSize
3657

3758

3859
update : Msg -> Model -> Model
39-
update (Switch newStyle) model =
40-
{ model | style = newStyle }
60+
update msg model =
61+
case msg of
62+
SwitchTo newFontSize ->
63+
{ model | fontSize = newFontSize }
4164

4265

4366

@@ -47,35 +70,37 @@ update (Switch newStyle) model =
4770
view : Model -> Html Msg
4871
view model =
4972
div []
50-
[ span [toStyle model] [text "Hello, how are you?!"]
51-
, radio Red "red" model
52-
, radio Underline "underline" model
53-
, radio Bold "bold" model
73+
[ fieldset []
74+
[ radio "Small" (SwitchTo Small)
75+
, radio "Medium" (SwitchTo Medium)
76+
, radio "Large" (SwitchTo Large)
77+
]
78+
, Markdown.toHtml [ sizeToStyle model.fontSize ] model.content
5479
]
5580

5681

57-
toStyle : Model -> Attribute msg
58-
toStyle model =
59-
style <|
60-
case model.style of
61-
Red ->
62-
[ ("color", "red") ]
82+
radio : String -> msg -> Html msg
83+
radio value msg =
84+
label
85+
[ style [("padding", "20px")]
86+
]
87+
[ input [ type' "radio", name "font-size", onClick msg ] []
88+
, text value
89+
]
6390

64-
Underline ->
65-
[ ("text-decoration", "underline") ]
6691

67-
Bold ->
68-
[ ("font-weight", "bold") ]
92+
sizeToStyle : FontSize -> Attribute msg
93+
sizeToStyle fontSize =
94+
let
95+
size =
96+
case fontSize of
97+
Small ->
98+
"0.8em"
6999

100+
Medium ->
101+
"1em"
70102

71-
radio : Style -> String -> Model -> Html Msg
72-
radio style name model =
73-
let
74-
isSelected =
75-
model.style == style
103+
Large ->
104+
"1.2em"
76105
in
77-
label []
78-
[ br [] []
79-
, input [ type' "radio", checked isSelected, onCheck (\_ -> Switch style) ] []
80-
, text name
81-
]
106+
style [("font-size", size)]

0 commit comments

Comments
 (0)