A select widget for Elm
Go to file
2023-04-14 10:39:35 +01:00
.github/workflows Create static.yml 2023-04-14 10:39:35 +01:00
examples Prevent more unnecessary filtering 2023-03-11 21:28:17 +00:00
review FEATURE: Add withSelectOnTab 2022-11-23 16:00:30 +00:00
src Rename a function 2023-03-13 09:14:21 +00:00
.gitignore Refactor and add aria attributes 2022-06-29 20:08:55 +02:00
CHANGELOG.md Bump version 3.1.3 2023-03-13 09:19:53 +00:00
elm.json Bump version 3.1.3 2023-03-13 09:19:53 +00:00
LICENSE Rename license 2022-07-21 20:33:21 +02:00
package-lock.json v3.1.0 2023-01-19 13:35:18 +00:00
package.json Bump version 3.1.3 2023-03-13 09:19:53 +00:00
README.md Bump version 3.1.3 2023-03-13 09:19:53 +00:00

Elm-Ui Select

A select widget for elm-ui with keyboard input, filtering, menu scrolling and requests.

Features:

  • Filter menu items based on user input.
  • Keyboard selection with up/down/PgUp/PgDn keys. Menu scrolls with selection.
  • Automatically attempts to size and place the menu based on the position of the input in the viewport.
  • Customisable: Supply your own attributes for the input, menu or your own option Element.
  • Make HTTP requests to retrieve matching options (The package does not make the requests, but you supply the Cmd/Effect - no elm/http dependency)
  • Can be used with the Effect pattern and elm-program-test to simulate input to the select.

Example


-- MODEL

type alias Model =
    { countrySelect : Select Country
    }


init : () -> ( Model, Cmd Msg )
init _ =
    ( { countrySelect =
            Select.init "country-select"
                |> Select.setItems Countries.all
      }
    , Cmd.none
    )



-- UPDATE

type Msg
    = CountrySelectMsg (Select.Msg Country)


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        CountrySelectMsg subMsg ->
            Select.update CountrySelectMsg subMsg model.countrySelect
                |> Tuple.mapFirst (\select -> { model | countrySelect = select })

-- VIEW

view : Model -> Element Msg
view model =
    Select.view []
        { onChange = CountrySelectMsg
        , label = Input.labelAbove [] (text "Choose a country")
        , placeholder = Just (Input.placeholder [] (text "Type to search"))
        , itemToString = \c -> c.flag ++ " " ++ c.name
        }
        |> Select.toElement model.countrySelect

Note that if you have the list of items in your model already, you can also pass them directly in the view instead of setting the items in init and it will work just as well:



init : ( PageModel, Cmd Msg )
init =
    ( { countrySelect = Select.init "country-select" }
    , Cmd.none
    )


view : List Country -> PageModel -> Element Msg
view countries model =
    Select.view []
        { onChange = CountrySelectMsg
        , label = Input.labelAbove [] (text "Choose a country")
        , placeholder = Just (Input.placeholder [] (text "Type to search"))
        , itemToString = \c -> c.flag ++ " " ++ c.name
        }
        |> Select.toElement (Select.setItems countries model.countrySelect)


Limitations

There are issues when the input is placed within a parent element that has overflow scroll or auto: the menu may be clipped by the parent. This can be overcome by using Select.withMenuPositionFixed, but if the parent also has a transform applied, it gets clipped again. This means any parent with e.g. Element.scrollBarY + Element.moveDown/moveLeft etc. can cause issues. This is due to a feature of the current CSS spec.