Skip to content

Commit 8313356

Browse files
Brad Bumbaloughgitim
Brad Bumbalough
authored andcommitted
Expose toggleRowActive to renderItem component
- Use `manuallyActivateRows={true}` to have the `toggleRowActive` prop exposed to rendered rows. - The out of box timer with pan responder will not activate rows.
1 parent ebbf4e5 commit 8313356

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ npm i react-native-sortable-list --save
2929
- **horizontal?** (boolean) when true, the SortableList's children are arranged horizontally in a row instead of vertically in a column. The default value is false.
3030
- **sortingEnabled?** (boolean) when false, rows are not sortable. The default value is true.
3131
- **scrollEnabled?** (boolean) when false, the content does not scrollable. The default value is true.
32+
- **manuallyActivateRows?** (bool) whether you intend to use the `toggleRowActive` method to activate a row or use the out of box solution.
3233
- **autoscrollAreaSize?** (number) determines the height for vertical list and the width for horizontal list of the area at the begining and the end of the list that will trigger autoscrolling. Defaults to 60.<br />
3334
- **rowActivationTime?** (number) determines time delay in ms before pressed row becomes active. Defaults to 200 ms.<br />
3435
- **refreshControl?** (element)<br />
3536
A RefreshControl that works the same way as a ScrollView's refreshControl.
3637
- **renderRow** (function)<br />
3738
`({key, index, data, disabled, active}) => renderable`<br />
38-
Takes a row key, row index, data entry from the data source and its statuses disabled, active and should return a renderable component to be rendered as the row.<br />
39+
Takes a row key, row index, data entry from the data source and its statuses disabled, active and should return a renderable component to be rendered as the row. The child component will receive a method called `toggleRowActive` (only if `manuallyActivateRows={true}`) to manually activate the row. Useful if you have multiple touch responders in your view.<br />
3940
- **renderFooter?** (function)<br />
4041
`() => renderable`<br />
4142
Renders returned component at the bottom of the list.

src/Row.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {Component} from 'react';
1+
import React, {Component, cloneElement} from 'react';
22
import PropTypes from 'prop-types';
33
import {Animated, PanResponder, StyleSheet} from 'react-native';
44
import {shallowEqual} from './utils';
@@ -14,6 +14,7 @@ export default class Row extends Component {
1414
x: PropTypes.number,
1515
y: PropTypes.number,
1616
}),
17+
manuallyActivateRows: PropTypes.bool,
1718
activationTime: PropTypes.number,
1819

1920
// Will be called on long press.
@@ -55,19 +56,27 @@ export default class Row extends Component {
5556
},
5657

5758
onPanResponderGrant: (e, gestureState) => {
58-
e.persist();
59-
60-
this._longPressTimer = setTimeout(() => {
59+
e.persist();
60+
if (!this.props.manuallyActivateRows) {
61+
this._longPressTimer = setTimeout(() => {
62+
this._target = e.nativeEvent.target;
63+
this._prevGestureState = {
64+
...gestureState,
65+
moveX: gestureState.x0,
66+
moveY: gestureState.y0,
67+
};
68+
if (!this._active) {
69+
this._toggleActive(e, gestureState);
70+
}
71+
}, this.props.activationTime);
72+
} else {
6173
this._target = e.nativeEvent.target;
6274
this._prevGestureState = {
6375
...gestureState,
6476
moveX: gestureState.x0,
6577
moveY: gestureState.y0,
6678
};
67-
if (!this._active) {
68-
this._toggleActive(e, gestureState);
69-
}
70-
}, this.props.activationTime);
79+
}
7180
},
7281

7382
onPanResponderMove: (e, gestureState) => {
@@ -164,7 +173,7 @@ export default class Row extends Component {
164173
{...this._panResponder.panHandlers}
165174
style={rowStyle}
166175
onLayout={this._onLayout}>
167-
{children}
176+
{this.props.manuallyActivateRows && children ? cloneElement(children, { toggleRowActive: this._toggleActive.bind(this) }) : children}
168177
</Animated.View>
169178
);
170179
}

src/SortableList.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default class SortableList extends Component {
2525
refreshControl: PropTypes.element,
2626
autoscrollAreaSize: PropTypes.number,
2727
rowActivationTime: PropTypes.number,
28+
manuallyActivateRows: PropTypes.bool,
2829

2930
renderRow: PropTypes.func.isRequired,
3031
renderFooter: PropTypes.func,
@@ -38,6 +39,7 @@ export default class SortableList extends Component {
3839
sortingEnabled: true,
3940
scrollEnabled: true,
4041
autoscrollAreaSize: 60,
42+
manuallyActivateRows: false
4143
}
4244

4345
/**
@@ -248,7 +250,8 @@ export default class SortableList extends Component {
248250
onActivate={this._onActivateRow.bind(this, key, index)}
249251
onPress={this._onPressRow.bind(this, key)}
250252
onRelease={this._onReleaseRow.bind(this, key)}
251-
onMove={this._onMoveRow}>
253+
onMove={this._onMoveRow}
254+
manuallyActivateRows={this.props.manuallyActivateRows}>
252255
{renderRow({
253256
key,
254257
data: data[key],

0 commit comments

Comments
 (0)