Skip to content

Commit 909fa9c

Browse files
author
Jeff Hanson
committed
pass instance to all callbacks; add item closing to example (jshanson7#7)
1 parent 62905d4 commit 909fa9c

File tree

5 files changed

+80
-41
lines changed

5 files changed

+80
-41
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
BIN = ./node_modules/.bin
22
SRC = $(shell find ./src -name '*.js')
33
LIB = $(SRC:./src/%=lib/%)
4+
EXAMPLE = $(shell find ./example/*.js)
45

56
build:: $(LIB)
67

78
lint::
8-
@$(BIN)/eslint $(SRC)
9+
@$(BIN)/eslint $(SRC) $(EXAMPLE)
910

1011
release-patch: build lint
1112
@$(call release,patch)

example/.eslintrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"rules": {
3+
"react/no-multi-comp": 0,
4+
"react/prop-types": 0
5+
}
6+
}

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "0.0.1",
44
"private": true,
55
"scripts": {
6-
"start": "node node_modules/react-native/local-cli/cli.js start"
6+
"start": "node node_modules/react-native/local-cli/cli.js start",
7+
"update-lib": "rm -r node_modules/react-native-swipeable && cd ../ && npm run build && cd example && npm install"
78
},
89
"dependencies": {
910
"react": "15.4.1",

example/swipeable-example.js

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,44 @@ import React, {Component} from 'react';
22
import {ScrollView, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
33
import Swipeable from 'react-native-swipeable';
44

5-
export default function SwipeableExample() {
6-
return (
7-
<ScrollView style={styles.container}>
8-
<Example1/>
9-
<Example2/>
10-
<Example3/>
11-
</ScrollView>
12-
);
5+
export default class SwipeableExample extends Component {
6+
7+
state = {
8+
currentlyOpenSwipeable: null
9+
};
10+
11+
handleScroll = () => {
12+
const {currentlyOpenSwipeable} = this.state;
13+
14+
if (currentlyOpenSwipeable) {
15+
currentlyOpenSwipeable.recenter();
16+
}
17+
};
18+
19+
render() {
20+
const {currentlyOpenSwipeable} = this.state;
21+
const itemProps = {
22+
onOpen: (event, gestureState, swipeable) => {
23+
if (currentlyOpenSwipeable && currentlyOpenSwipeable !== swipeable) {
24+
currentlyOpenSwipeable.recenter();
25+
}
26+
27+
this.setState({currentlyOpenSwipeable: swipeable});
28+
},
29+
onClose: () => this.setState({currentlyOpenSwipeable: null})
30+
};
31+
32+
return (
33+
<ScrollView onScroll={this.handleScroll} style={styles.container}>
34+
<Example1 {...itemProps}/>
35+
<Example2 {...itemProps}/>
36+
<Example3 {...itemProps}/>
37+
</ScrollView>
38+
);
39+
}
1340
}
1441

15-
function Example1() {
42+
function Example1({onOpen, onClose}) {
1643
return (
1744
<Swipeable
1845
leftContent={(
@@ -28,6 +55,8 @@ function Example1() {
2855
<Text>2</Text>
2956
</TouchableOpacity>
3057
]}
58+
onRightButtonsOpenRelease={onOpen}
59+
onRightButtonsCloseRelease={onClose}
3160
>
3261
<View style={[styles.listItem, {backgroundColor: 'salmon'}]}>
3362
<Text>Example 1</Text>
@@ -36,7 +65,7 @@ function Example1() {
3665
);
3766
}
3867

39-
function Example2() {
68+
function Example2({onOpen, onClose}) {
4069
return (
4170
<Swipeable
4271
leftButtonWidth={45}
@@ -65,6 +94,8 @@ function Example2() {
6594
<Text>Pull action</Text>
6695
</View>
6796
)}
97+
onLeftButtonsOpenRelease={onOpen}
98+
onLeftButtonsCloseRelease={onClose}
6899
>
69100
<View style={[styles.listItem, {backgroundColor: 'paleturquoise'}]}>
70101
<Text>Example 2</Text>
@@ -85,9 +116,6 @@ class Example3 extends Component {
85116

86117
return (
87118
<Swipeable
88-
onLeftActionActivate={() => this.setState({leftActionActivated: true})}
89-
onLeftActionDeactivate={() => this.setState({leftActionActivated: false})}
90-
onLeftActionComplete={() => this.setState({toggle: !toggle})}
91119
leftActionActivationDistance={200}
92120
leftContent={(
93121
<View style={[styles.leftSwipeItem, {backgroundColor: leftActionActivated ? 'lightgoldenrodyellow' : 'steelblue'}]}>
@@ -96,6 +124,9 @@ class Example3 extends Component {
96124
<Text>keep pulling!</Text>}
97125
</View>
98126
)}
127+
onLeftActionActivate={() => this.setState({leftActionActivated: true})}
128+
onLeftActionDeactivate={() => this.setState({leftActionActivated: false})}
129+
onLeftActionComplete={() => this.setState({toggle: !toggle})}
99130
>
100131
<View style={[styles.listItem, {backgroundColor: toggle ? 'thistle' : 'darkseagreen'}]}>
101132
<Text>Example 3</Text>

src/index.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export default class Swipeable extends PureComponent {
217217
const {lastOffset, pan} = this.state;
218218

219219
pan.setOffset(lastOffset);
220-
this.props.onSwipeStart(event, gestureState);
220+
this.props.onSwipeStart(event, gestureState, this);
221221
};
222222

223223
_handlePanResponderMove = (event, gestureState) => {
@@ -257,46 +257,46 @@ export default class Swipeable extends PureComponent {
257257
let nextRightButtonsActivated = rightButtonsActivated;
258258

259259
this._handlePan(event, gestureState);
260-
onSwipeMove(event, gestureState);
260+
onSwipeMove(event, gestureState, this);
261261

262262
if (!leftActionActivated && canSwipeRight && x >= leftActionActivationDistance) {
263263
nextLeftActionActivated = true;
264-
onLeftActionActivate(event, gestureState);
264+
onLeftActionActivate(event, gestureState, this);
265265
}
266266

267267
if (leftActionActivated && canSwipeRight && x < leftActionActivationDistance) {
268268
nextLeftActionActivated = false;
269-
onLeftActionDeactivate(event, gestureState);
269+
onLeftActionDeactivate(event, gestureState, this);
270270
}
271271

272272
if (!rightActionActivated && canSwipeLeft && x <= -rightActionActivationDistance) {
273273
nextRightActionActivated = true;
274-
onRightActionActivate(event, gestureState);
274+
onRightActionActivate(event, gestureState, this);
275275
}
276276

277277
if (rightActionActivated && canSwipeLeft && x > -rightActionActivationDistance) {
278278
nextRightActionActivated = false;
279-
onRightActionDeactivate(event, gestureState);
279+
onRightActionDeactivate(event, gestureState, this);
280280
}
281281

282282
if (!leftButtonsActivated && hasLeftButtons && !isSwipingLeft && x >= leftButtonsActivationDistance) {
283283
nextLeftButtonsActivated = true;
284-
onLeftButtonsActivate(event, gestureState);
284+
onLeftButtonsActivate(event, gestureState, this);
285285
}
286286

287287
if (leftButtonsActivated && hasLeftButtons && isSwipingLeft) {
288288
nextLeftButtonsActivated = false;
289-
onLeftButtonsDeactivate(event, gestureState);
289+
onLeftButtonsDeactivate(event, gestureState, this);
290290
}
291291

292292
if (!rightButtonsActivated && hasRightButtons && !isSwipingRight && x <= -rightButtonsActivationDistance) {
293293
nextRightButtonsActivated = true;
294-
onRightButtonsActivate(event, gestureState);
294+
onRightButtonsActivate(event, gestureState, this);
295295
}
296296

297297
if (rightButtonsActivated && hasRightButtons && isSwipingRight) {
298298
nextRightButtonsActivated = false;
299-
onRightButtonsDeactivate(event, gestureState);
299+
onRightButtonsDeactivate(event, gestureState, this);
300300
}
301301

302302
const needsUpdate =
@@ -339,30 +339,30 @@ export default class Swipeable extends PureComponent {
339339
const animationFn = this._getReleaseAnimationFn();
340340
const animationConfig = this._getReleaseAnimationConfig();
341341

342-
onSwipeRelease(event, gestureState);
342+
onSwipeRelease(event, gestureState, this);
343343

344344
if (leftActionActivated) {
345-
onLeftActionRelease(event, gestureState);
345+
onLeftActionRelease(event, gestureState, this);
346346
}
347347

348348
if (rightActionActivated) {
349-
onRightActionRelease(event, gestureState);
349+
onRightActionRelease(event, gestureState, this);
350350
}
351351

352352
if (leftButtonsActivated && !leftButtonsOpen) {
353-
onLeftButtonsOpenRelease(event, gestureState);
353+
onLeftButtonsOpenRelease(event, gestureState, this);
354354
}
355355

356356
if (!leftButtonsActivated && leftButtonsOpen) {
357-
onLeftButtonsCloseRelease(event, gestureState);
357+
onLeftButtonsCloseRelease(event, gestureState, this);
358358
}
359359

360360
if (rightButtonsActivated && !rightButtonsOpen) {
361-
onRightButtonsOpenRelease(event, gestureState);
361+
onRightButtonsOpenRelease(event, gestureState, this);
362362
}
363363

364364
if (!rightButtonsActivated && rightButtonsOpen) {
365-
onRightButtonsCloseRelease(event, gestureState);
365+
onRightButtonsCloseRelease(event, gestureState, this);
366366
}
367367

368368
this.setState({
@@ -390,32 +390,32 @@ export default class Swipeable extends PureComponent {
390390
onSwipeComplete
391391
} = this.props;
392392

393-
onSwipeComplete(event, gestureState);
393+
onSwipeComplete(event, gestureState, this);
394394

395395
if (leftActionActivated) {
396-
onLeftActionComplete(event, gestureState);
397-
onLeftActionDeactivate(event, gestureState);
396+
onLeftActionComplete(event, gestureState, this);
397+
onLeftActionDeactivate(event, gestureState, this);
398398
}
399399

400400
if (rightActionActivated) {
401-
onRightActionComplete(event, gestureState);
402-
onRightActionDeactivate(event, gestureState);
401+
onRightActionComplete(event, gestureState, this);
402+
onRightActionDeactivate(event, gestureState, this);
403403
}
404404

405405
if (leftButtonsActivated && !leftButtonsOpen) {
406-
onLeftButtonsOpenComplete(event, gestureState);
406+
onLeftButtonsOpenComplete(event, gestureState, this);
407407
}
408408

409409
if (!leftButtonsActivated && leftButtonsOpen) {
410-
onLeftButtonsCloseComplete(event, gestureState);
410+
onLeftButtonsCloseComplete(event, gestureState, this);
411411
}
412412

413413
if (rightButtonsActivated && !rightButtonsOpen) {
414-
onRightButtonsOpenComplete(event, gestureState);
414+
onRightButtonsOpenComplete(event, gestureState, this);
415415
}
416416

417417
if (!rightButtonsActivated && rightButtonsOpen) {
418-
onRightButtonsCloseComplete(event, gestureState);
418+
onRightButtonsCloseComplete(event, gestureState, this);
419419
}
420420
});
421421
};

0 commit comments

Comments
 (0)