Closed andrewn closed 4 years ago
I got around this issue by moving the remove element with the click handler to remove outside of the element with getSelectedItemProps
See below:
{selectedItems.map((selectedItem, index) => (
<>
<span
style={selectedItemStyles}
key={`selected-item-${index}`}
{...getSelectedItemProps({ selectedItem, index })}
>
{selectedItem}
</span>
<span
style={selectedItemIconStyles}
onClick={() => removeSelectedItem(selectedItem)}
>
✕
</span>
</>
))}
Hi! I updated the example to include a stop propagation on the click event, as it was caught by the handler in the item otherwise. Can you update your use case to reflect this?
Hi @silviuaavram, I don't see your updated example?
@andrewn Add onClick={e => { e.stopPropagation() }}
to your span element with getSelectedItemProps
on it.
https://codesandbox.io/s/usemultipleselection-combobox-usage-8enbh?file=/src/index.js
downshift
version: 5.4.2node
version: 12.16.1 (but can reproduce on Code Sandbox)npm
(oryarn
) version: 6.13.4Relevant code or config
What you did:
I'm using
useMultipleSelection
withselectedItems
in controlled mode. This is so I can have external forms drive the Combobox state.What happened:
Calling
removeSelectedItem(item)
to remove an item in the selection doesn't callonSelectedItemsChange
and so the controlledselectedItems
is never updated. The result is that the item cannot be removed.Reproduction repository:
I forked the
useMultipleSelection
demo.Problem description:
I added a
stateReducer
to log what happens. Clicking on the item that callsremoveSelectedItem(item)
causes the following reducer changes, in order:Suggested solution:
It seems that the click action is overwriting the result of removing the selected item.