Make presidents searchable by name
This commit is contained in:
parent
a0ac286ac3
commit
0ea9f2c904
|
@ -1,5 +1,8 @@
|
||||||
import Html exposing (Html, div, h1, text)
|
import Html exposing (Html, div, h1, input, text)
|
||||||
import Html.App as App
|
import Html.App as App
|
||||||
|
import Html.Attributes exposing (placeholder, value)
|
||||||
|
import Html.Events exposing (onInput)
|
||||||
|
import String
|
||||||
import Table
|
import Table
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,14 +23,20 @@ main =
|
||||||
type alias Model =
|
type alias Model =
|
||||||
{ people : List Person
|
{ people : List Person
|
||||||
, tableState : Table.State
|
, tableState : Table.State
|
||||||
|
, query : String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
init : List Person -> ( Model, Cmd Msg )
|
init : List Person -> ( Model, Cmd Msg )
|
||||||
init people =
|
init people =
|
||||||
( Model people (Table.initialSort "Year")
|
let
|
||||||
, Cmd.none
|
model =
|
||||||
)
|
{ people = people
|
||||||
|
, tableState = Table.initialSort "Year"
|
||||||
|
, query = ""
|
||||||
|
}
|
||||||
|
in
|
||||||
|
( model, Cmd.none )
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,14 +44,22 @@ init people =
|
||||||
|
|
||||||
|
|
||||||
type Msg
|
type Msg
|
||||||
= UpdateTableState Table.State
|
= UpdateQuery String
|
||||||
|
| UpdateTableState Table.State
|
||||||
|
|
||||||
|
|
||||||
update : Msg -> Model -> ( Model, Cmd Msg )
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||||
update msg model =
|
update msg model =
|
||||||
case msg of
|
case msg of
|
||||||
|
UpdateQuery newQuery ->
|
||||||
|
( { model | query = String.toLower newQuery }
|
||||||
|
, Cmd.none
|
||||||
|
)
|
||||||
|
|
||||||
UpdateTableState newState ->
|
UpdateTableState newState ->
|
||||||
( Model model.people newState, Cmd.none )
|
( { model | tableState = newState }
|
||||||
|
, Cmd.none
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,11 +67,21 @@ update msg model =
|
||||||
|
|
||||||
|
|
||||||
view : Model -> Html Msg
|
view : Model -> Html Msg
|
||||||
view {people, tableState} =
|
view { people, tableState, query } =
|
||||||
div []
|
let
|
||||||
[ h1 [] [ text "Birthplaces of U.S. Presidents" ]
|
acceptablePeople =
|
||||||
, Table.view config tableState people
|
List.filter (String.contains query << String.toLower << .name) people
|
||||||
]
|
in
|
||||||
|
div []
|
||||||
|
[ h1 [] [ text "Birthplaces of U.S. Presidents" ]
|
||||||
|
, input
|
||||||
|
[ value query
|
||||||
|
, placeholder "Search by Name"
|
||||||
|
, onInput UpdateQuery
|
||||||
|
]
|
||||||
|
[]
|
||||||
|
, Table.view config tableState acceptablePeople
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
config : Table.Config Person Msg
|
config : Table.Config Person Msg
|
Loading…
Reference in New Issue