Update the Select by returning Effects instead of Cmds. This module is designed to help testing with elm-program-test, allowing you to simulate the effects produced by the select and simulate input. If you are not doing this kind of testing, you don't need this module.
Update the Select
type MyEffect
= SelectEffect (Select.Effect Never Msg)
update : Msg -> Model -> ( Model, MyEffect )
update msg model =
case msg of
SelectMsg subMsg ->
Select.Effect.update SelectMsg subMsg model.select
|> Tuple.mapFirst (\select -> { model | select = select })
|> Tuple.mapSecond SelectEffect
performEffect : MyEffect -> Cmd Msg
performEffect effect =
case effect of
SelectEffect selectEffect ->
Select.Effect.perform selectEffectUpdate with options.
update : Msg -> Model -> ( Model, MyEffect )
update msg model =
case msg of
SelectMsg subMsg ->
Select.Effect.updateWith [ Select.Effect.onSelectedChanged ThingSelected ] SelectMsg subMsg model.select
|> Tuple.mapFirst (\select -> { model | select = select })
|> Tuple.mapSecond SelectEffect
ThingSelected maybeThing ->
Debug.todo "Do something when the thing is selected/deselected"Options for use with updateWith.
Update with an HTTP request to retrieve matching remote results. Note that in order to avoid an elm/http dependency in this package, you will need to provide the request Effect yourself.
Provide a function that takes the input value and returns an Effect (your app's own Effect type) that can be used to perform an HTTP request. Update will use this Effect when the user types into the input.
When the effect is performed you must use Select.Effect.performWithRequest instead of Select.Effect.perform.
type MyEffect
= SelectEffect (Select.Effect MyEffect Msg)
| FetchThings String
update : Msg -> Model -> ( Model, MyEffect )
update msg model =
case msg of
SelectMsg subMsg ->
Select.Effect.updateWith [ Select.Effect.request fetchThings ] SelectMsg subMsg model.select
|> Tuple.mapFirst (\select -> { model | select = select })
|> Tuple.mapSecond SelectEffect
performEffect : MyEffect -> Cmd Msg
performEffect effect =
case effect of
SelectEffect selectEffect ->
Select.Effect.performWithRequest performEffect selectEffect
FetchThings query ->
fetchThings (Select.gotRequestResponse query >> SelectMsg) query
fetchThings : (Result Http.Error (List thing) -> msg) -> String -> Cmd msg
fetchThings tagger query =
Http.get
{ url = "https://awesome-thing.api/things?search=" ++ query
, expect = Http.expectJson tagger (Decode.list thingDecoder)
}How many characters does a user need to type before a request is sent? If this is too low you may get an unmanagable number of results! Default is 3 characters.
Configure debouncing for the request. How long should we wait in milliseconds after the user stops typing to send the request? Default is 300.
If provided this msg will be sent whenever the selected item changes.
If provided this msg will be sent whenever the input value changes.
If provided this msg will be sent whenever the input gets focus.
If provided this msg will be sent whenever the input loses focus.
Turn an Effect into a Cmd
performEffect : MyEffect -> Cmd Msg
performEffect effect =
case effect of
SelectEffect selectEffect ->
Select.Effect.perform selectEffectPerform the Effect with a request. You need to provide your own perform function to perform the provided request effect.
performEffect : MyEffect -> Cmd Msg
performEffect effect =
case effect of
SelectEffect selectEffect ->
Select.Effect.performWithRequest performEffect selectEffect
FetchThings query ->
fetchThings (Select.gotRequestResponse >> SelectMsg) querySimulate the select effects. This is designed to work with elm-program-test, but since this package doesn't have it as a dependency, you need to provide some of the functions to help with the simulation.
simulateEffect : MyEffect -> SimulatedEffect Msg
simulateEffect effect =
case effect of
SelectEffect selectEffect ->
Select.Effect.simulate
Example.SelectMsg
{ perform = SimulatedTask.perform
, batch = SimulatedCmd.batch
, sleep = SimulatedProcess.sleep
}
selectEffectSimulate the select effects with a request. This is designed to work with elm-program-test, but since this package doesn't have it as a dependency, you need to provide some of the functions to help with the simulation.
simulateEffect : MyEffect -> SimulatedEffect Msg
simulateEffect effect =
case effect of
SelectEffect selectEffect ->
Select.Effect.simulateWithRequest
Example.SelectMsg
{ perform = SimulatedTask.perform
, batch = SimulatedCmd.batch
, sleep = SimulatedProcess.sleep
}
simulateEffect
selectEffect
FetchThings query ->
SimulateHttp.get
{ url = "https://awesome-thing.api/things?search=" ++ query
, expect = SimulateHttp.expectJson tagger (Decode.list thingDecoder)
}Simulate clicking an option by the text label of the option. This is designed to help simulate input with elm-program-test. Since this package doesn't have elm-test or elm-program-test as dependencies, you need to provide some of the functions from those packages here.
simulateConfig : Select.Effect.SimulateInputConfig (Single msg) Selector (ProgramTest model msg effect)
simulateConfig =
{ simulateDomEvent = ProgramTest.simulateDomEvent
, find = Query.find
, attribute = Selector.attribute
}
selectTest : Test
selectTest =
Test.test "Typing United and clicking United Kingdom option selects United Kingdom" <|
\() ->
ProgramTest.createElement
{ init = Example.init
, update = Example.update
, view = Example.view
}
|> ProgramTest.withSimulatedEffects simulateEffect
|> ProgramTest.start ()
|> ProgramTest.fillIn "" "Choose a country" "United Kingdom"
|> Select.Effect.simulateClickOption simulateConfig model.select "United Kingdom"
|> ProgramTest.expectViewHas [ Selector.text "You chose United Kingdom" ]Config type alias used by simulateClickOption
Map Effect from one msg type to another
Map Effect from one effect type to another
The Effect type