-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPorts.elm
86 lines (63 loc) · 1.85 KB
/
Ports.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
port module Ports exposing (main)
import Html exposing (Html, program, text, div, input, h1)
import Html.Attributes exposing (placeholder, style, id)
import Html.Events exposing (onFocus)
import View exposing (container)
import Json.Decode
type alias Model =
{ date : Maybe String
}
type Msg
= OpenDatePicker
| DatePicked String
type alias HtmlId =
String
port openDatePicker : HtmlId -> Cmd msg
port datePicked : (String -> msg) -> Sub msg
main : Program Never Model Msg
main =
program
{ init = { date = Nothing } ! []
, update = update
, subscriptions = (\_ -> datePicked DatePicked)
, view = view
}
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
OpenDatePicker ->
model ! [ openDatePicker dateInputId ]
DatePicked date ->
{ model | date = Just date } ! []
dateInputId : HtmlId
dateInputId =
"date"
view : Model -> Html Msg
view model =
let
picked =
case model.date of
Nothing ->
"No date picked yet"
Just date ->
"Date picked: " ++ date
in
container
(Just ( "/Speakers.elm", "Native" ))
[ h1 [] [ text "Set sail!" ]
, div
[]
[ text picked ]
, input
[ placeholder "Select a date"
, style inputStyles
, onFocus OpenDatePicker
, id dateInputId
]
[]
]
[ ( "https://elmtown.github.io/2017/05/09/history-in-elm-town-ports-episode-13.html", "History in Elm Town, Ports! - Episode 13" )
]
inputStyles : List ( String, String )
inputStyles =
[ ( "display", "block" ), ( "padding", ".5em" ), ( "margin-top", "1em" ) ]