Skip to content

Commit

Permalink
Set search query as user types
Browse files Browse the repository at this point in the history
When the user types in the text box, we set the query value on the
model, thus filtering the dropdown.

Note that the new Msg value `SearchInput` takes a paremeter, we don't
give it one in `onInput`. While `onClick` takes a Msg as its argument,
`onInput` takes a _function_ `String -> Msg` as its argument.

`SearchInput` is such a function when not given a value.

`onInput` does this because we don't know the value ahead of time.
Instead, it will put the whatever value the user has typed inside the
Msg.
  • Loading branch information
JoelQ committed Jul 5, 2017
1 parent 0355ee6 commit cd42ae0
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Main.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Main exposing (main)

import Html exposing (..)
import Html.Events exposing (onClick)
import Html.Events exposing (onClick, onInput)


main =
Expand Down Expand Up @@ -58,6 +58,7 @@ viewOpen : Model -> Html Msg
viewOpen model =
div []
[ dropdownHead model.selected CloseSelect
, input [ onInput SearchInput ] []
, ul [] (List.map dropdownItem (filteredValues model))
]

Expand Down Expand Up @@ -98,6 +99,7 @@ type Msg
= OpenSelect
| CloseSelect
| ItemSelected String
| SearchInput String


update : Msg -> Model -> Model
Expand All @@ -111,3 +113,6 @@ update msg model =

ItemSelected value ->
{ model | selected = Just value, state = Closed }

SearchInput query ->
{ model | query = query }

0 comments on commit cd42ae0

Please sign in to comment.