This module provides a component that allows you to get autocomplete suggestions. These can be either hardcoded or you can use remote suggestions obtained from API call. It uses the builder pattern:
new to create a base input.withPlaceholder or withIsRequired.view to render it.Initialize the SuggestionInput model.
init : ContactInfoModel
init =
{ addressSuggestionInputModel = AvardaUi.SuggestionInput.init "address" }Used after init to use hardcoded list of suggestions.
In some cases it's nice to have the option to re-init the component with no need to pass id again.
Msg type for the SuggestionInput component. Use this for the wrap msg in your own Msg type.
type ContactInfoMsg
= HandleAddressSuggestionInput (AvardaUi.SuggestionInput.Msg Address)
| NoUpdateupdate : Model -> Msg -> ( Model, Cmd Msg )
update model msg =
case msg of
HandleAddressSuggestionsInput suggestionInputMsg ->
let
( HandleAddressSuggestionInput, suggestionInputCmd ) =
SuggestionInput.update
HandleAddressSuggestionsInput
suggestionInputMsg
model.addressSuggestionInputModel
in
( { model | addressSuggestionInputModel = HandleAddressSuggestionInput }, suggestionInputCmd )Create a new SuggestionInput component.
AvardaUi.SuggestionInput.new
{ label = "Address"
, suggestionInputModel = addressSuggestionInputModel
, suggestionToInputValue = .addressLine1
}Render the Select as HTML.
Always call this after you've built up the input with new and chained settings.
AvardaUi.SuggestionInput.new "address"
|> AvardaUi.SuggestionInput.withIsRequired True
|> AvardaUi.SuggestionInput.viewTo use it with the hardcoded list of suggestions, just setSuggestions after init.
SuggestionInput.init "address"
|> SuggestionInput.setSuggestions suggestionListIf you wish to use it with remote suggestions, add a remoteSuggestions update option in your updateWith function.
SuggestionInput.updateWith
[ SuggestionInput.remoteSuggestions { requestCmd = fetchSuggestions, requestMinInputLength = 3 } ]Set the input value.
Get the currently selected suggestion.
If none is selected, returns Nothing.
Note that if you select a suggestion and then write anything to the input, the suggestion is no longer selected in the model
You can pass extra options / callbacks to this component using updateWith
update : Model -> Msg -> ( Model, Cmd Msg )
update model msg =
case msg of
HandleAddressSuggestionsInput suggestionInputMsg ->
let
( HandleAddressSuggestionInput, suggestionInputCmd ) =
SuggestionInput.updateWith
[ SuggestionInput.onInput InsertedAddress
, SuggestionInput.onSelect ClickedAddressSuggestion
]
HandleAddressSuggestionsInput
suggestionInputMsg
model.addressSuggestionInputModel
in
( { model | addressSuggestionInputModel = HandleAddressSuggestionInput }, suggestionInputCmd )Used for triggering a message when suggestion is selected.
Used for triggering a message when the input is typed into.
Used in updateWith in order to use suggestions obtain from an API call instead of hardcoded ones set with setSuggestions.
The RemoteSuggestionsPayload looks like this:
type alias RemoteSuggestionsPayload a msg =
{ requestCmd : String -> (RemoteData.WebData (List a) -> msg) -> Cmd msg
, requestMinInputLength : Int
}where String is the request's query.
Example of the requestCmd:
getAddressSuggestions : String -> (RemoteData.WebData (List Address) -> msg) -> Cmd msg
getAddressSuggestions query msg =
{ method = "GET"
, headers = []
, url = "https://api.example.com/suggestions?query=" ++ query
, body = Http.emptyBody
, expect = Http.expectJson (RemoteData.fromResult >> msg) addressSuggestionDecoder
, timeout = Nothing
, tracker = Nothing
}
|> Http.requestUsed to fix issue with scrolling option into view when used in ShadowDOM. elm/browser cannot find the element ID when this component is used by an app which uses ShadowDOM
type alias ScrollOptionIntoViewCmd msg =
String -> Cmd msgAdjust the border radius of the input box.
Add a hint text below the input.
Add a placeholder to the input.
Disable the input. Visually dims and prevents user actions.
Mark the input as required.
This adds a * to the label and sets required=true.
Show an error message and mark the input invalid. Replaces any active hint text.
Set a maximum height for the dropdown menu.
Set a custom ARIA label.
Enable autocomplete with a given string like "address1". It is set to "off" by default.
Provide a custom view function for rendering suggestions.
Add a left-side child element.
Add a right-side child element.
Provide additional styles for the selected suggestion.
Adjust the gap between the Select and the dropdown menu.
Model type for the SuggestionInput component. Use this in your own model.