Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

large multi selects #20

Merged
merged 1 commit into from
Feb 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/MultiSelect.elm
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,36 @@ selectedOptionsDecoder =
|> Decode.map filterSelected



{- |
To allow for large multi selects the length is first retrieved in a IE safe way.
Then a method that compiles to a JS loop is used to retrieve the options.
-}


optionsDecoder : Decode.Decoder (List Option)
optionsDecoder =
let
loop idx xs =
Decode.maybe (Decode.field (String.fromInt idx) optionDecoder)
|> Decode.andThen
(Maybe.map (\x -> loop (idx + 1) (x :: xs))
>> Maybe.withDefault (Decode.succeed xs)
)
in
(Decode.field "options" <| loop 0 [])
|> Decode.map List.reverse
Decode.field "options"
(Decode.keyValuePairs (Decode.maybe optionDecoder)
|> Decode.andThen
(\list ->
let
onlySuccess =
List.foldr
(\( _, next ) sofar ->
case next of
Nothing ->
sofar

Just x ->
x :: sofar
)
[]
list
in
Decode.succeed onlySuccess
)
)


type alias Option =
Expand Down