Confidenceman02/elm-select/1.0.2

NOTE: the latest version is 13.0.0

Select

Select items from a drop-down list.

Set up

type State
type Action item
= InputChange String
| Select item
| DeselectMulti item
| ClearSingleSelectItem

The select actions that your program can react to.

InputChange -> The value of the input has changed

Select -> There has been a selection of a MenuItem

DeselectMulti -> A multi variant selected tag has been deselected

ClearSingleSelectItem -> A single variant selected menu item has been deselected

An initial state for the State type.

type Msg item
placeholder : String -> Config item -> Config item

The text that will appear as an input placeholder.

    yourView model =
        view
            (single Nothing |> placeholder "some placeholder")
            (selectIdentifier "1234")
selectIdentifier : String -> SelectId

The ID for the rendered Select input

NOTE: It is important that the ID's of all selects that exist on a page remain unique.

yourView model =
    view
        (single Nothing)
        (selectIdentifier "someUniqueId")
state : State -> Config item -> Config item

Sets the state value.

Should be a persisted value that lives in your model.

    model : Model
    model =
        { selectState = initState }


    yourView : Model
    yourView model =
        view
            (single Nothing |> state model.selectState)
            (selectIdentifier "1234")
update : Msg item -> State -> ( Maybe (Action item), State, Cmd (Msg item) )

Handle all Msg events

yourUpdate msg model =
    case msg of
        SelectMsg selectMsg ->
            update selectMsg model.selectState
view : Config item -> SelectId -> Html (Msg item)

Renders the select

    yourView model =
        view
            (single Nothing)
            (selectIdentifier "SingleSelectExample")

Single select

single : Maybe (MenuItem item) -> Config item

Select a single item.

clearable : Bool -> Config item -> Config item

Allows a 'single' variant selected menu item to be deselected.

    yourView model =
        view
            (single Nothing |> clearable True)
            (selectIdentifier "SingleSelectExample")

Multi select

multi : MultiSelectConfig -> List (MenuItem item) -> Config item

Select multiple items.

yourView model =
    view
        (multi initMultiConfig [])
        (selectIdentifier "1234")
truncateMultiTag : Float -> MultiSelectConfig -> MultiSelectConfig

Limit the width of a multi select tag.

Handy for when the selected item text is excessively long. Text that breaches the set width will display as an ellipses.

Width will be in px values.

    yourView model =
        view
            (multi (initMultiConfig |> truncateMultitag 30) [])
            (selectIdentifier "1234")
multiTagColor : Color -> MultiSelectConfig -> MultiSelectConfig

Set the color for the multi select tag.

    yourView =
        view
            (multi (initMultiConfig |> multiTagColor (Css.hex "#E1E2EA") [])
            (selectIdentifier "1234")
initMultiConfig : MultiSelectConfig

Starting value for the 'multi' variant.

    yourView model =
        view
            (multi initMultiConfig)
            (selectIdentifier "1234")

Common

disabled : Bool -> Config item -> Config item

Disable the the select input

    yourView model =
        view
            (single Nothing |> disabled True)
            (selectIdentifier "SingleSelectExample")
jsOptimize : Bool -> State -> State

Opt in to JS optimizations for the best performance.

    model : Model model =
        { selectState = initState |> jsOptimize True }

Install the JS package:

npm

npm install @confidenceman02/elm-select

github packages

add to projects .npmrc

@confidenceman02:registry=<https://npm.pkg.github.com/>

install

npm install @confidenceman02/elm-select

Import script

In the Elm init file

import { Elm } from "./src/Main";
import "@confidenceman02/elm-select/dynamic"


Elm.Main.init({node, flags})

Alternatively, you can import the minified JS in the index.html file

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Viewer</title>


        <script src="/node_modules/@confidenceman02/elm-select/dist/dynamic.min.js"></script>
      </head>
      <body>
        <main></main>
        <script src="index.js"></script>
      </body>
    </html>
labelledBy : String -> Config item -> Config item

The ID for the label that describes the select. It is best practice to render the select with a label.

    yourView model =
        view
            (single Nothing |> labelledBy "selectLabelId")
            (selectIdentifier "SingleSelectExample")
loading : Bool -> Config item -> Config item

Displays the animated loading icon. This would be useful if you are loading menu options asynchronously, like from a server.

    yourView model =
        view
            (single Nothing |> loading True)
            (selectIdentifier "SingleSelectExample")