nunntom/elm-ui-select/3.1.3

NOTE: the latest version is 4.0.0

Select.Effect

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.

Type

type alias Effect effect msg =
Effect effect msg

The Effect type

Update Effect

update : (Msg a -> msg) -> Msg a -> Select a -> ( Select a, Effect Never msg )

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 selectEffect
List (UpdateOption effect a msg)
-> (Msg a -> msg)
-> Msg a
-> Select a
-> ( Select a, Effect effect msg )

Update 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"

Update Options

type alias UpdateOption effect a msg =
UpdateOption effect a msg

Options for use with updateWith.

request : (String -> effect) -> UpdateOption effect a msg

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)
        }
requestMinInputLength : Int -> UpdateOption effect a msg

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.

requestDebounceDelay : Float -> UpdateOption effect a msg

Configure debouncing for the request. How long should we wait in milliseconds after the user stops typing to send the request? Default is 300.

onSelectedChange : (Maybe a -> msg) -> UpdateOption effect a msg

If provided this msg will be sent whenever the selected item changes.

onInput : (String -> msg) -> UpdateOption effect a msg

If provided this msg will be sent whenever the input value changes.

onFocus : msg -> UpdateOption effect a msg

If provided this msg will be sent whenever the input gets focus.

onLoseFocus : msg -> UpdateOption effect a msg

If provided this msg will be sent whenever the input loses focus.

Perform Effect

perform : Effect Never msg -> Cmd msg

Turn an Effect into a Cmd

performEffect : MyEffect -> Cmd Msg
performEffect effect =
    case effect of
        SelectEffect selectEffect ->
            Select.Effect.perform selectEffect
performWithRequest : (effect -> Cmd msg) -> Effect effect msg -> Cmd msg

Perform 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) query

Simulating Effects

{ perform : (() -> msg) -> simulatedTask -> simulatedEffect
, batch : List simulatedEffect -> simulatedEffect
, sleep : Float -> simulatedTask
}
-> Effect Never msg
-> simulatedEffect

Simulate 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
                }
                selectEffect
{ perform : (() -> msg) -> simulatedTask -> simulatedEffect
, batch : List simulatedEffect -> simulatedEffect
, sleep : Float -> simulatedTask
}
-> (effect -> simulatedEffect)
-> Effect effect msg
-> simulatedEffect

Simulate 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)
                }

Simulating Input

SimulateInputConfig single selector programTest
-> String
-> String
-> programTest
-> programTest

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" ]
type alias SimulateInputConfig single selector programTest =
{ simulateDomEvent :
(single -> single) -> ( String, Value ) -> programTest -> programTest
, find : List selector -> single -> single
, attribute : Attribute Never -> selector
}

Config type alias used by simulateClickOption

Mapping

map : (msg -> msg2) -> Effect effect msg -> Effect effect msg2

Map Effect from one msg type to another

mapEffect : (effect -> effect2) -> Effect effect msg -> Effect effect2 msg

Map Effect from one effect type to another