Orasund / elm-ui-widgets

Collection of reusable views for elm-ui.
https://orasund.github.io/elm-ui-widgets/
BSD 3-Clause "New" or "Revised" License
85 stars 11 forks source link

Option Story always defaults to the first value #67

Closed Orasund closed 3 years ago

Orasund commented 3 years ago

@cdevienne The Option Story Story.optionListStory will initiate with the first element of the list, instead of the given default value.

I've therefore decided that we actually see this as a feature and change the function to:

optionListStory : String -> ( String, a ) -> List ( String, a ) -> Story a
optionListStory label first options =
    { info = OptionListStory label <| List.map Tuple.first (first :: options)
    , toValue =
        \optLabel ->
            (first :: options)
                |> List.foldl
                    (\( key, optvalue ) res ->
                        case ( res, optLabel == key ) of
                            ( Just x, _ ) ->
                                Just x

                            ( Nothing, True ) ->
                                Just optvalue

                            ( Nothing, False ) ->
                                Nothing
                    )
                    Nothing
                |> Maybe.withDefault (first |> Tuple.second)
    }

Note how the default value is now the first element in the list.

cdevienne commented 3 years ago

I was thinking the exact same thing this morning :-)