This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

FilterBox

Search box that supports key value filtering

Model

type alias Model = FilterBox.Model.Model

Holds the state of the filter search box

{ filters : List ( String, String )
...
}

filters is where all of the added filters are stored as a list of (key, value).

type alias InputMatch = FilterBox.Model.InputMatch

InputMatches are displayed underneath the search box as suggestions when available.

{ match : List ( String, Bool )
, description : Maybe String
}

When all the strings in match are joined together they form the suggestion. If the boolean with each string is True then that section of the string will be bolded indicating the matching section of the string. See FilterBox.Helpers for an example.

description is an optional description of the suggestion. If provided it will be displayed beside the suggestion.

type alias Config = FilterBox.Model.Config

All configuration options for the filter box

{ getFilterKeys : Maybe (String -> Task String (List InputMatch))
, getFilterValues : Maybe (String -> String -> Task String (List InputMatch))
}

If getFilterKeys is specified, it will be passed the current text to provide a list of matches.

If getFilterValues is specified, it will be passed the selected key and current text to provide a list of matches.

init : Config -> ( Model, Cmd Msg )

Initializes the filter box

View

view : Model -> Html Msg

The view takes the model and displays a filter box

State

type alias Msg = FilterBox.Update.Msg

Filter box messages

update : Msg -> Model -> ( Model, Cmd Msg )

Update the filter box

detectFiltersChanged : Msg -> Model -> Bool

Use alongside update and .filters in order to trigger events when a filter is added

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        FilterBoxMsg subMsg ->
            let
                filtersChanged =
                    detectFiltersChanged subMsg model.filterBox

                ( nextFilterBox, filterBoxCmd ) =
                    FilterBox.update subMsg model.filterBox

                doSomethingCmd =
                    if filtersChanged then
                        doSomething nextFilterBox.filters
                    else
                        Cmd.none

                cmd =
                    Cmd.batch
                        [ doSomethingCmd
                        , Cmd.map FilterBoxMsg filterBoxCmd
                        ]
            in
                ( { model | filterBox = nextFilterBox }, cmd )
module FilterBox exposing (Model, InputMatch, Config, init, view, Msg, update, detectFiltersChanged)

{-| Search box that supports key value filtering


# Model

@docs Model, InputMatch, Config, init


# View

@docs view


# State

@docs Msg, update, detectFiltersChanged

-}

import FilterBox.Model
import FilterBox.Update
import FilterBox.View
import Html.Styled exposing (Html)


-- MODEL --


{-| Holds the state of the filter search box

    { filters : List ( String, String )
    ...
    }

`filters` is where all of the added filters are stored as a list of `(key, value)`.

-}
type alias Model =
    FilterBox.Model.Model


{-| InputMatches are displayed underneath the search box as suggestions when available.

    { match : List ( String, Bool )
    , description : Maybe String
    }

When all the strings in `match` are joined together they form the suggestion. If the boolean with each string is `True` then that section of the string will be bolded indicating the matching section of the string. See FilterBox.Helpers for an example.

`description` is an optional description of the suggestion. If provided it will be displayed beside the suggestion.

-}
type alias InputMatch =
    FilterBox.Model.InputMatch


{-| All configuration options for the filter box

    { getFilterKeys : Maybe (String -> Task String (List InputMatch))
    , getFilterValues : Maybe (String -> String -> Task String (List InputMatch))
    }

If `getFilterKeys` is specified, it will be passed the current text to provide a list of matches.

If `getFilterValues` is specified, it will be passed the selected key and current text to provide a list of matches.

-}
type alias Config =
    FilterBox.Model.Config


{-| Initializes the filter box
-}
init : Config -> ( Model, Cmd Msg )
init config =
    ( FilterBox.Model.initModel config, FilterBox.Update.initCmd config )



-- VIEW --


{-| The view takes the model and displays a filter box
-}
view : Model -> Html Msg
view model =
    FilterBox.View.view model



-- UPDATE --


{-| Filter box messages
-}
type alias Msg =
    FilterBox.Update.Msg


{-| Update the filter box
-}
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    FilterBox.Update.update msg model


{-| Use alongside `update` and `.filters` in order to trigger events when a filter is added

    update : Msg -> Model -> ( Model, Cmd Msg )
    update msg model =
        case msg of
            FilterBoxMsg subMsg ->
                let
                    filtersChanged =
                        detectFiltersChanged subMsg model.filterBox

                    ( nextFilterBox, filterBoxCmd ) =
                        FilterBox.update subMsg model.filterBox

                    doSomethingCmd =
                        if filtersChanged then
                            doSomething nextFilterBox.filters
                        else
                            Cmd.none

                    cmd =
                        Cmd.batch
                            [ doSomethingCmd
                            , Cmd.map FilterBoxMsg filterBoxCmd
                            ]
                in
                    ( { model | filterBox = nextFilterBox }, cmd )

-}
detectFiltersChanged : Msg -> Model -> Bool
detectFiltersChanged msg model =
    FilterBox.Update.detectFiltersChanged msg model