mercurymedia / elm-smart-select / SingleSelect

A select component for a single selection with local data.

Architecture


type SmartSelect msg a

The opaque type representing a particular smart select instance.


type Msg a

Opaque type representing cases to be passed to SingleSelect.update

init : { selectionMsg : ( a, Msg a ) -> msg, internalMsg : Msg a -> msg, idPrefix : String } -> SmartSelect msg a

Instantiates and returns a smart select.

view : { selected : Maybe a, options : List a, optionLabelFn : a -> String } -> SmartSelect msg a -> Html msg

The smart select view for selecting one option at a time with local data.

viewCustom : { isDisabled : Basics.Bool, selected : Maybe a, options : List a, optionLabelFn : a -> String, optionDescriptionFn : a -> String, optionsContainerMaxHeight : Basics.Float, searchFn : String -> List a -> List a, selectTitle : String, searchPrompt : String, noResultsForMsg : String -> String, noOptionsMsg : String } -> SmartSelect msg a -> Html msg

The customizable smart select view for selecting one option at a time with local data.

import Html exposing (Html)
import SingleSelect

type Msg
    = HandleSelectUpdate (SingleSelect.Msg Product)
    | HandleSelection ( Product, SingleSelect.Msg Product )

type alias Product =
    { name : String
    , description : String
    , price : Float
    }

init : () -> ( Model, Cmd Msg )
init _ =
    ( { products = exampleProducts
      , select =
            SingleSelect.init
                { selectionMsg = HandleSelection
                , internalMsg = HandleSelectUpdate
                }
      , selectedProduct = Nothing
      }
    , Cmd.none
    )

type alias Model =
    { products : List Product
    , select : SingleSelect.SmartSelect Msg Product
    , selectedProduct : Maybe Product
    }

viewCustomProductSelect : Model -> Html Msg
viewCustomProductSelect model =
    SingleSelect.viewCustom
        { isDisabled = False
        , selected = model.selectedProduct
        , options = model.products
        , optionLabelFn = .name
        , optionDescriptionFn = \option -> "$" ++ String.fromFloat option.price
        , optionsContainerMaxHeight = 500
        , searchFn =
            \searchText allOptions ->
                List.filter
                    (\option ->
                        String.contains (String.toLower searchText) (String.toLower option.name)
                            || String.contains (String.toLower searchText) (String.toLower option.description)
                    )
                    allOptions
        , selectTitle = "Select a Product"
        , searchPrompt = "Search for a Product"
        , noResultsForMsg = \searchText -> "No results found for: " ++ searchText
        , noOptionsMsg = "There are no options to select"
        }
        model.select

subscriptions : SmartSelect msg a -> Platform.Sub.Sub msg

Events external to the smart select to which it is subscribed.

update : Msg a -> SmartSelect msg a -> ( SmartSelect msg a, Platform.Cmd.Cmd msg )

Update the provided smart select and receive the updated select instance and a cmd to run.