toystars / react-native-multiple-select

Simple multi-select component for react-native
MIT License
565 stars 313 forks source link

First element is never selected #45

Closed CarlosJJR closed 6 years ago

CarlosJJR commented 6 years ago

Issue summary

When you select the first item of the array it isn't marked as selected and is added multiple times to 'selectedItems'

Library versions

react-native: 0.48.0 react-native-multiple-select: ^0.2.7

Steps to Reproduce

  1. Init the component with the example
  2. Select the first option
  3. Option is not marked as selected

Expected Behavior

The first option should be selected or unselected according with selectedItems

Actual Behavior

The first option is never marked as selected and index 0 is added multiple times to selectedItems

Item 0 is selected when the list is collapsed

simulator screen shot - iphone 8 - 2017-12-06 at 16 08 20

But when the list is shown, the item is nor marked as selected

simulator screen shot - iphone 8 - 2017-12-06 at 16 15 05

After clicking multiple times the first item it is repeated on selectedItems

simulator screen shot - iphone 8 - 2017-12-06 at 16 15 35

CarlosJJR commented 6 years ago

the problem seems to be in the function _itemSelected

_itemSelected = item => { const { uniqueKey, selectedItems } = this.props; return !!find(selectedItems, singleItem => item[uniqueKey] === singleItem); };

Replacing the lodash function find seems to fix the issue: _itemSelected = item => { const { uniqueKey, selectedItems } = this.props; return selectedItems.indexOf(item[uniqueKey]) !== -1; };