Skip to content
Closed
Show file tree
Hide file tree
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
75 changes: 43 additions & 32 deletions src/GroupedDropdownOptions.elm
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,50 @@ optionGroupsToHtml dropdownItemEventListeners selectionConfig groupedDropdownOpt
optionGroupToHtml : DropdownItemEventListeners msg -> SelectionConfig -> DropdownOptionsGroup -> List (Html msg)
optionGroupToHtml dropdownItemEventListeners selectionMode dropdownOptionsGroup =
let
optionGroupHtml =
case dropdownOptionsGroup |> getOptions |> DropdownOptions.maybeFirstOptionSearchFilter of
Just optionSearchFilter ->
case dropdownOptionsGroup |> getOptionsGroup |> OptionGroup.toString of
"" ->
text ""

_ ->
div
[ class "optgroup"
, Html.Attributes.attribute "part" "dropdown-optgroup"
]
[ span [ class "optgroup-header" ]
(tokensToHtml optionSearchFilter.groupTokens)
]

Nothing ->
case dropdownOptionsGroup |> getOptionsGroup |> OptionGroup.toString of
"" ->
text ""

optionGroupAsString ->
div
[ class "optgroup"
, Html.Attributes.attribute "part" "dropdown-optgroup"
]
[ span [ class "optgroup-header" ]
[ text
optionGroupAsString
]
]
options =
getOptions dropdownOptionsGroup

optionsHtml =
optionsToCustomHtml dropdownItemEventListeners selectionMode options
in
optionGroupHtml :: optionsToCustomHtml dropdownItemEventListeners selectionMode (getOptions dropdownOptionsGroup)
if DropdownOptions.isEmpty options then
[]

else
let
optionGroupHtml =
case options |> DropdownOptions.maybeFirstOptionSearchFilter of
Just optionSearchFilter ->
case dropdownOptionsGroup |> getOptionsGroup |> OptionGroup.toString of
"" ->
text ""

_ ->
div
[ class "optgroup"
, Html.Attributes.attribute "part" "dropdown-optgroup"
]
[ span [ class "optgroup-header" ]
(tokensToHtml optionSearchFilter.groupTokens)
]

Nothing ->
case dropdownOptionsGroup |> getOptionsGroup |> OptionGroup.toString of
"" ->
text ""

optionGroupAsString ->
div
[ class "optgroup"
, Html.Attributes.attribute "part" "dropdown-optgroup"
]
[ span [ class "optgroup-header" ]
[ text
optionGroupAsString
]
]
in
optionGroupHtml :: optionsHtml


dropdownOptionsToDatalistHtml : DropdownOptions -> Html msg
Expand Down
64 changes: 63 additions & 1 deletion tests/Option/OrderingInGroups.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
module Option.OrderingInGroups exposing (suite)

import DropdownItemEventListeners exposing (DropdownItemEventListeners)
import DropdownOptions
import Expect
import GroupedDropdownOptions
import Option exposing (Option(..), setGroupWithString, test_newFancyOptionWithMaybeCleanString)
import Html exposing (Html)
import Option exposing (Option(..), newSelectedOption, setGroupWithString, test_newFancyOptionWithMaybeCleanString)
import OptionGroup exposing (OptionGroup)
import OptionList
import OptionValue
import SelectionMode exposing (defaultSelectionConfig)
import Test exposing (Test, describe, test)


Expand Down Expand Up @@ -67,6 +71,20 @@ optionGroupToDebuggingString optionGroup =
OptionGroup.toString optionGroup



-- Mock event listeners for testing HTML rendering


mockEventListeners : DropdownItemEventListeners String
mockEventListeners =
{ mouseOverMsgConstructor = \_ -> "mouseOver"
, mouseOutMsgConstructor = \_ -> "mouseOut"
, mouseDownMsgConstructor = \_ -> "mouseDown"
, mouseUpMsgConstructor = \_ -> "mouseUp"
, noOpMsgConstructor = "noOp"
}


suite : Test
suite =
describe "When we have a sorted list of options"
Expand All @@ -90,4 +108,48 @@ suite =
)
)
)
, test "hide optgroup headers when all options in the group are selected" <|
\_ ->
let
selectedScrewDriver =
newSelectedOption 0 "Screw Driver" Nothing |> setGroupWithString "Hand Tool"

selectedWrench =
newSelectedOption 1 "Wrench" Nothing |> setGroupWithString "Hand Tool"

selectedHammer =
newSelectedOption 2 "Hammer" Nothing |> setGroupWithString "Hand Tool"

selectedChisel =
newSelectedOption 3 "Chisel" Nothing |> setGroupWithString "Hand Tool"

unselectedDrill =
drill

unselectedSawZaw =
sawZaw

allOptions =
OptionList.FancyOptionList
[ selectedScrewDriver
, unselectedDrill
, selectedWrench
, unselectedSawZaw
, selectedHammer
, selectedChisel
]

unselectedOnlyOptions =
DropdownOptions.figureOutWhichOptionsToShowInTheDropdownThatAreNotSelected
defaultSelectionConfig
allOptions

renderedHtml =
GroupedDropdownOptions.groupOptionsInOrder unselectedOnlyOptions
|> GroupedDropdownOptions.optionGroupsToHtml mockEventListeners defaultSelectionConfig

elementCount =
List.length renderedHtml
in
Expect.equal 3 elementCount
]