Skip to content

Commit

Permalink
Merge pull request #16 from fundlg/add-on-item-press-event
Browse files Browse the repository at this point in the history
Add onItemPress event for Select component
  • Loading branch information
Retiznyk authored Oct 17, 2019
2 parents 781d4e1 + 18f8a9d commit 218b416
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Select based on modal
| **`items`** | array | + | | Select options |
| **`selected`** | any | | _null_ | Selected items |
| **`onSelect`** | function | | | Called on selection change with selected items as a param |
| **`onItemPress`** | function | | | Called on item component press event with item as a param |
| **`keyExtractor`** | function | | | Should return uniq item identifier |
| **`labelExtractor`** | function | | | Should return item label to show |
| **`renderHeader`** | function | | | Render custom select header. Input params: { `onCancel`: _function_, `onSubmit`: _function_, `onSearch`: _function_, `onClear`: _function_ } |
Expand Down
12 changes: 11 additions & 1 deletion lib/Select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class Select extends PureComponent {
items: PropTypes.array.isRequired,
selected: PropTypes.any,
onSelect: PropTypes.func,
onItemPress: PropTypes.func,
keyExtractor: PropTypes.func,
labelExtractor: PropTypes.func,
renderHeader: PropTypes.func,
Expand Down Expand Up @@ -51,6 +52,7 @@ export default class Select extends PureComponent {
renderDivider: () => null,
renderSelectedMarker: () => null,
renderSelectControls: () => null,
onItemPress: () => null,
ItemsContainer: FlatList,
disabled: false,
autoClose: false,
Expand Down Expand Up @@ -138,6 +140,14 @@ export default class Select extends PureComponent {
}
};

_generateOnItemPressHandler = item => () => {
const { onItemPress } = this.props;
this.toggleSelection(item);
if (onItemPress) {
onItemPress(item);
}
};

_renderItem = ({ item }) => {
const {
labelExtractor,
Expand All @@ -150,7 +160,7 @@ export default class Select extends PureComponent {

return (
<TouchableOpacity
onPress={() => this.toggleSelection(item)}
onPress={this._generateOnItemPressHandler(item)}
style={[style.item, itemStyle]}
>
<Text style={textStyle}>{label}</Text>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-controls",
"version": "0.6.1",
"version": "0.7.0",
"main": "index.js",
"keywords": [
"react",
Expand Down

0 comments on commit 218b416

Please sign in to comment.