Remove request from UpdateConfig and remove exposed UpdateConfig module

This commit is contained in:
Tom Nunn 2022-07-13 09:19:15 +02:00
parent 9d8ce7f3d5
commit b72fc3d71e
10 changed files with 73 additions and 155 deletions

View File

@ -8,8 +8,7 @@
"Select",
"Select.Filter",
"Select.Effect",
"Select.Request",
"Select.UpdateConfig"
"Select.Request"
],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {

View File

@ -64,10 +64,12 @@ update msg model =
case msg of
SelectMsg subMsg ->
Select.Effect.updateWith
{ request = Just (Select.Effect.request FetchCocktails)
, clearInputValueOnBlur = True
, selectExactMatchOnBlur = False
}
(Just
{ clearInputValueOnBlur = True
, selectExactMatchOnBlur = False
}
)
(Just (Select.Effect.request FetchCocktails))
SelectMsg
subMsg
model.select

View File

@ -11,7 +11,6 @@ import Http
import Json.Decode as Decode exposing (Decoder)
import Resources.ClearButton
import Select exposing (OptionState(..), Select)
import Select.UpdateConfig as SelectConfig
main : Program () Model Msg
@ -63,13 +62,7 @@ update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SelectMsg subMsg ->
Select.updateWith
(SelectConfig.default
|> SelectConfig.withRequest (Select.request fetchCocktails)
)
SelectMsg
subMsg
model.select
Select.updateWith Nothing (Just (Select.request fetchCocktails)) SelectMsg subMsg model.select
|> Tuple.mapFirst (\select -> { model | select = select })

View File

@ -40,8 +40,9 @@ import Internal.Filter as Filter exposing (Filter)
import Internal.Option as Option exposing (Option)
import Internal.OptionState exposing (OptionState(..))
import Internal.Placement exposing (Placement(..))
import Internal.Request exposing (Request)
import Internal.RequestState exposing (RequestState(..))
import Select.UpdateConfig exposing (UpdateConfig)
import Internal.UpdateConfig exposing (UpdateConfig)
@ -119,7 +120,7 @@ toHighlighted (Model { highlighted }) =
toFilteredOptions : (a -> String) -> Maybe (Filter a) -> Model a -> List (Option a)
toFilteredOptions itemToString filter (Model model) =
List.indexedMap (Option.init itemToString) model.items
List.map (Option.init itemToString) model.items
|> (if model.applyFilter then
Filter.filterOptions model.inputValue filter
@ -296,8 +297,8 @@ setFocused v (Model model) =
Model { model | focused = v }
blur : UpdateConfig effect -> List (Option a) -> Model a -> Model a
blur { clearInputValueOnBlur, selectExactMatchOnBlur, request } filteredOptions (Model model) =
blur : UpdateConfig -> Maybe (Request effect) -> List (Option a) -> Model a -> Model a
blur { clearInputValueOnBlur, selectExactMatchOnBlur } request filteredOptions (Model model) =
(if model.selected == Nothing then
case ( selectExactMatchOnBlur, Option.findByString filteredOptions model.inputValue ) of
( True, Just option ) ->

View File

@ -2,21 +2,21 @@ module Internal.Option exposing (Option, findByString, findByValue, init, toItem
type alias Option a =
( a, String, Int )
( a, String )
init : (a -> String) -> Int -> a -> Option a
init itemToString idx item =
( item, itemToString item, idx )
init : (a -> String) -> a -> Option a
init itemToString item =
( item, itemToString item )
toItem : Option a -> a
toItem ( a, _, _ ) =
toItem ( a, _ ) =
a
toString : Option a -> String
toString ( _, s, _ ) =
toString ( _, s ) =
s

View File

@ -6,11 +6,11 @@ import Internal.Msg exposing (Msg(..))
import Internal.Option exposing (Option)
import Internal.Request as Request exposing (Request)
import Internal.RequestState exposing (RequestState(..))
import Select.UpdateConfig exposing (UpdateConfig)
import Internal.UpdateConfig as UpdateConfig exposing (UpdateConfig)
update : UpdateConfig effect -> (Msg a -> msg) -> Msg a -> Model a -> ( Model a, Effect effect msg )
update ({ request } as config) tagger msg model =
update : Maybe UpdateConfig -> Maybe (Request effect) -> (Msg a -> msg) -> Msg a -> Model a -> ( Model a, Effect effect msg )
update config request tagger msg model =
case msg of
InputChanged val ->
( model
@ -56,7 +56,7 @@ update ({ request } as config) tagger msg model =
onFocusMenu tagger request model
InputLostFocus filteredOptions ->
( Model.blur config filteredOptions model
( Model.blur (Maybe.withDefault UpdateConfig.default config) request filteredOptions model
, Effect.none
)

View File

@ -0,0 +1,14 @@
module Internal.UpdateConfig exposing (UpdateConfig, default)
type alias UpdateConfig =
{ clearInputValueOnBlur : Bool
, selectExactMatchOnBlur : Bool
}
default : UpdateConfig
default =
{ clearInputValueOnBlur = False
, selectExactMatchOnBlur = False
}

View File

@ -3,7 +3,7 @@ module Select exposing
, setItems, setSelected, setInputValue, closeMenu
, toValue, toInputValue, toInputElementId, toMenuElementId
, isMenuOpen, isLoading, isRequestFailed
, Msg, update, updateWith, Request, request, gotRequestResponse
, Msg, update, updateWith, UpdateConfig, Request, request, gotRequestResponse
, ViewConfig, view, withMenuAttributes, MenuPlacement(..), withMenuMaxHeight, withMenuMaxWidth, withNoMatchElement, withOptionElement, OptionState, withClearButton, ClearButton, clearButton, withFilter, withMenuAlwaysAbove, withMenuAlwaysBelow, withMenuPositionFixed
, toElement
, Effect
@ -34,7 +34,7 @@ module Select exposing
# Update and Requests
@docs Msg, update, updateWith, Request, request, gotRequestResponse
@docs Msg, update, updateWith, UpdateConfig, Request, request, gotRequestResponse
# Configure View
@ -64,7 +64,6 @@ import Internal.Request as Request
import Internal.Update as Update
import Internal.View as View exposing (ViewConfigInternal)
import Select.Filter exposing (Filter)
import Select.UpdateConfig as UpdateConfig exposing (UpdateConfig)
@ -191,23 +190,24 @@ type alias Msg a =
-}
update : (Msg a -> msg) -> Msg a -> Select a -> ( Select a, Cmd msg )
update tagger msg select =
Update.update UpdateConfig.default tagger msg select
Update.update Nothing Nothing tagger msg select
|> Tuple.mapSecond (Effect.perform (\_ -> Cmd.none))
{-| Update with configuration options, including using an HTTP request to retrieve matching remote results.
{-| Update with optional configuration options, and optionally use 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 Cmd yourself.
See [Select.UpdateConfig](Select-UpdateConfig) for configuration options.
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Request SelectMsg subMsg ->
Select.updateWith
{ request = Select.request fetchThings
, clearInputValueOnBlur = False
, selectExactMatchOnBlur = True
}
(Just
{ clearInputValueOnBlur = False
, selectExactMatchOnBlur = True
}
)
(Just (Select.request fetchThings))
SelectMsg
subMsg
model.select
@ -220,22 +220,25 @@ See [Select.UpdateConfig](Select-UpdateConfig) for configuration options.
, expect = Http.expectJson (Select.gotRequestResponse query) (Decode.list thingDecoder)
}
You can also use [Select.UpdateConfig](Select-UpdateConfig) to build up a config:
-}
updateWith : Maybe UpdateConfig -> Maybe (Request a) -> (Msg a -> msg) -> Msg a -> Select a -> ( Select a, Cmd msg )
updateWith config req tagger msg select =
Update.update config req identity msg select
|> Tuple.mapSecond (Effect.perform identity >> Cmd.map tagger)
Select.updateWith
(Select.UpdateConfig.default
|> Select.UpdateConfig.withRequest (Select.request fetchThings)
)
SelectMsg
subMsg
model.select
|> Tuple.mapFirst (\select -> { model | select = select })
{-| Configuration options for updateWith.
- Should the input be cleared if on blur if nothing is selected?
- Should an exact string match (case insensitive) be selected automatically on blur if nothing is already selected?
If you do not provide a config, both default to False.
-}
updateWith : UpdateConfig (Cmd (Msg a)) -> (Msg a -> msg) -> Msg a -> Select a -> ( Select a, Cmd msg )
updateWith config tagger msg select =
Update.update config identity msg select
|> Tuple.mapSecond (Effect.perform identity >> Cmd.map tagger)
type alias UpdateConfig =
{ clearInputValueOnBlur : Bool
, selectExactMatchOnBlur : Bool
}
{-| A Request. See [Select.Request](Select-Request) for configuration options.

View File

@ -51,8 +51,8 @@ import Internal.Model as Model exposing (Model)
import Internal.Msg exposing (Msg)
import Internal.Request as Request
import Internal.Update as Update
import Internal.UpdateConfig exposing (UpdateConfig)
import Json.Encode as Encode
import Select.UpdateConfig as UpdateConfig exposing (UpdateConfig)
{-| The Effect type
@ -87,12 +87,11 @@ type alias Effect effect msg =
-}
update : (Msg a -> msg) -> Msg a -> Select a -> ( Select a, Effect Never msg )
update =
Update.update UpdateConfig.default
Update.update Nothing Nothing
{-| Update with configuration options, including using an HTTP request to retrieve matching remote results.
{-| Update with configuration options, and optionally use 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.
See [Select.UpdateConfig](Select-UpdateConfig) for configuration options.
type MyEffect
= SelectEffect (Select.Effect MyEffect Msg)
@ -103,10 +102,12 @@ See [Select.UpdateConfig](Select-UpdateConfig) for configuration options.
case msg of
SelectMsg subMsg ->
Select.Effect.updateWith
{ request = Select.request fetchThings
, clearInputValueOnBlur = False
, selectExactMatchOnBlur = True
}
(Just
{ clearInputValueOnBlur = False
, selectExactMatchOnBlur = True
}
)
(Just (Select.request fetchThings))
SelectMsg
subMsg
model.select
@ -129,18 +130,8 @@ See [Select.UpdateConfig](Select-UpdateConfig) for configuration options.
, expect = Http.expectJson tagger (Decode.list thingDecoder)
}
You can also use [Select.UpdateConfig](Select-UpdateConfig) to build up a config:
Select.Effect.updateWith
(Select.UpdateConfig.default
|> Select.UpdateConfig.withRequest (Select.request fetchThings)
)
subMsg
model.select
|> Tuple.mapFirst (\select -> { model | select = select })
-}
updateWith : UpdateConfig effect -> (Msg a -> msg) -> Msg a -> Select a -> ( Select a, Effect effect msg )
updateWith : Maybe UpdateConfig -> Maybe (Request effect) -> (Msg a -> msg) -> Msg a -> Select a -> ( Select a, Effect effect msg )
updateWith =
Update.update

View File

@ -1,85 +0,0 @@
module Select.UpdateConfig exposing
( UpdateConfig, default
, withRequest, withClearInputValueOnBlur, withSelectExactMatchOnBlur
)
{-| Build a config for updating the select
# Type
@docs UpdateConfig, default
# Configure
@docs withRequest, withClearInputValueOnBlur, withSelectExactMatchOnBlur
-}
import Internal.Request exposing (Request)
{-| The UpdateConfig type
-}
type alias UpdateConfig effect =
{ request : Maybe (Request effect)
, clearInputValueOnBlur : Bool
, selectExactMatchOnBlur : Bool
}
{-| The default config. No request, clears the input on blur if no selection and does not select an exact string match on blur.
-}
default : UpdateConfig Never
default =
{ request = Nothing
, clearInputValueOnBlur = False
, selectExactMatchOnBlur = False
}
{-| 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 Cmd yourself.
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Request SelectMsg subMsg ->
Select.updateWith
(Select.UpdateConfig.default
|> Select.UpdateConfig.withRequest (Select.request fetchThings)
)
SelectMsg
subMsg
model.select
|> Tuple.mapFirst (\select -> { model | select = select })
fetchThings : String -> Cmd (Select.Msg Thing)
fetchThings query =
Http.get
{ url = "https://awesome-thing.api/things?search=" ++ query
, expect = Http.expectJson (Select.gotRequestResponse query) (Decode.list thingDecoder)
}
-}
withRequest : Request effect -> UpdateConfig eff1 -> UpdateConfig effect
withRequest request config =
{ request = Just request
, clearInputValueOnBlur = config.clearInputValueOnBlur
, selectExactMatchOnBlur = config.selectExactMatchOnBlur
}
{-| Should the input value be cleared when the input loses focus? Setting this to False can be useful to make the select more like an autocomplete
-}
withClearInputValueOnBlur : Bool -> UpdateConfig effect -> UpdateConfig effect
withClearInputValueOnBlur v config =
{ config | clearInputValueOnBlur = v }
{-| If the input value exactly matches the valueToString value of one of the options, set is as selected when the input loses focus.
-}
withSelectExactMatchOnBlur : Bool -> UpdateConfig effect -> UpdateConfig effect
withSelectExactMatchOnBlur v config =
{ config | selectExactMatchOnBlur = v }