Skip to content

Commit 836a4e3

Browse files
author
Madeline Trotter
authored
Fix select bugs: filter selected options, avoid re-render on focus change (#182)
1 parent aab8784 commit 836a4e3

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/Lumi/Components/Select.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ styles = jss
430430

431431
, "&:hover lumi-select-input": lumiInputHoverStyles
432432

433-
, "&:focus lumi-select-input, & lumi-select-inner[data-focus=\"true\"] lumi-select-input":
433+
, "&:focus-within lumi-select-input, & lumi-select-inner[data-focus=\"true\"] lumi-select-input":
434434
{ extend: lumiInputFocusStyles
435435
, "& lumi-select-input-placeholder":
436436
{ display: "none"
@@ -541,7 +541,7 @@ styles = jss
541541
}
542542

543543
-- hide the blank input line when not in focus
544-
, "& input.select-native-input[value=\"\"]:not(:focus)":
544+
, "& lumi-select-inner:not([data-focus=\"true\"]) input.select-native-input[value=\"\"]":
545545
{ width: 0
546546
, minWidth: 0
547547
, padding: 0

src/Lumi/Components/Select/SelectBackend.purs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ selectBackend = make component
145145
{ isOpen = true
146146
, isActive = true
147147
, focusedIndex = s.focusedIndex <|>
148-
if (getOptions s # maybe 0 optionsLength) > 0 then
148+
if (getOptions props s # maybe 0 optionsLength) > 0 then
149149
Just 0
150150
else
151151
Nothing
@@ -169,13 +169,13 @@ selectBackend = make component
169169
}
170170

171171
Blur -> do
172-
when state.isActive do
172+
when (state.isActive) do
173173
self.setState _ { isActive = false }
174174

175175
FocusIndexUp -> do
176176
self.setState \s -> s
177177
{ focusedIndex = do
178-
options <- getOptions s
178+
options <- getOptions props s
179179
let iMax = optionsLength options - 1
180180
pure
181181
case fromMaybe 0 s.focusedIndex of
@@ -186,7 +186,7 @@ selectBackend = make component
186186
FocusIndexDown -> do
187187
self.setState \s -> s
188188
{ focusedIndex = do
189-
options <- getOptions s
189+
options <- getOptions props s
190190
let iMax = optionsLength options - 1
191191
pure
192192
case fromMaybe (-1) s.focusedIndex of
@@ -237,7 +237,7 @@ selectBackend = make component
237237
send self $ LoadOptions searchTerm
238238

239239
LoadOptions searchTerm -> do
240-
when (isNothing $ getOptions { searchTerm, optionCache: state.optionCache }) do
240+
when (isNothing $ getOptions props { searchTerm, optionCache: state.optionCache }) do
241241
let
242242
tidyUp =
243243
map
@@ -272,7 +272,7 @@ selectBackend = make component
272272
, isOpen: self.state.isOpen
273273
, isActive: self.state.isActive
274274
, keydownEventHandler
275-
, options: map _.external $ fromMaybe Loading $ getOptions self.state
275+
, options: map _.external $ fromMaybe Loading $ getOptions self.props self.state
276276
, removeAllSelectedOptions: send self RemoveAllSelectedOptions
277277
, removeSelectedOption: send self <<< RemoveSelectedOption
278278
, searchTerm: self.state.searchTerm
@@ -366,7 +366,7 @@ selectBackend = make component
366366
E.preventDefault e
367367
E.stopPropagation e
368368
traverse_ (send self <<< AddSelectedOption) do
369-
options <- getOptions state
369+
options <- getOptions props state
370370
focusedIndex <- state.focusedIndex
371371
option <- getOption focusedIndex options
372372
pure option.external
@@ -392,13 +392,24 @@ optionsLength = case _ of
392392
_ -> 0
393393

394394
getOptions ::
395-
forall a r.
395+
forall a p r.
396+
{ value :: Array a
397+
, toSelectOption :: a -> SelectOption
398+
| p
399+
} ->
396400
{ searchTerm :: String
397-
, optionCache :: Map.Map String (SelectOptions a)
401+
, optionCache :: Map.Map String (SelectOptions (SelectOptionInternal a))
398402
| r
399403
} ->
400-
Maybe (SelectOptions a)
401-
getOptions { searchTerm, optionCache } = Map.lookup searchTerm optionCache
404+
Maybe (SelectOptions (SelectOptionInternal a))
405+
getOptions { toSelectOption, value } { searchTerm, optionCache } =
406+
Map.lookup searchTerm optionCache <#> case _ of
407+
Ready os -> Ready $ isNotSelected os
408+
so -> so
409+
where
410+
selected = map (_.value <<< toSelectOption) value
411+
412+
isNotSelected = Array.filter \v -> Array.notElem v.value selected
402413

403414
getOption :: forall a. Int -> SelectOptions a -> Maybe a
404415
getOption i = case _ of

0 commit comments

Comments
 (0)