Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add refresh controls #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/AutoDragSortableView.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {Component} from 'react'
import {Animated, Dimensions, Easing, PanResponder, StyleSheet, TouchableOpacity, View, ScrollView,Platform} from 'react-native'
import {Animated, Dimensions, Easing, PanResponder, StyleSheet, RefreshControl, TouchableOpacity, View, ScrollView,Platform} from 'react-native'

const PropTypes = require('prop-types')
const {width,height} = Dimensions.get('window')
Expand Down Expand Up @@ -42,6 +42,7 @@ export default class AutoDragSortableView extends Component{
height: Math.ceil(dataSource.length / rowNum) * itemHeight,
itemWidth,
itemHeight,
scrollEnabled: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default this to true so refresh control is enabled before an item is dragged

};

this._panResponder = PanResponder.create({
Expand All @@ -57,6 +58,7 @@ export default class AutoDragSortableView extends Component{
onPanResponderMove: (evt, gestureState) => this.moveTouch(evt, gestureState),
onPanResponderRelease: (evt, gestureState) => this.endTouch(evt),

onPanResponderTerminate: (evt) => this.endTouch(evt),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes a problem with some gesture handling when the ScrollView captures the gesture

onPanResponderTerminationRequest: (evt, gestureState) => false,
onShouldBlockNativeResponder: (evt, gestureState) => false,
})
Expand Down Expand Up @@ -106,6 +108,7 @@ export default class AutoDragSortableView extends Component{
componentDidMount() {
this.initTag()
this.autoMeasureHeight()
this.isHasMeasure = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prevent autoMeasureHeight from running again due to componentDidUpdate(). If we don't do this then before the first reorder (which sets isHasMeasure to true) pull to refresh acts very janky because we are pulling to refresh so the refreshing prop updates but then autoMeasureHeight causes the ScrollView to scroll.

}

componentDidUpdate() {
Expand Down Expand Up @@ -589,7 +592,10 @@ export default class AutoDragSortableView extends Component{
render() {
return (
<ScrollView
bounces={false}
bounces={this.props.onRefresh}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enable bounces in order for pull to refresh to work

refreshControl={this.props.onRefresh && (
<RefreshControl onRefresh={this.props.onRefresh} refreshing={this.props.refreshing} enabled={this.state.scrollEnabled} />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disable RefreshControl when scrollEnabled is false because that means we are processing a drag of an item. The drag can conflict with the pull to refresh gesture if it is left enabled.

)}
scrollEventThrottle={1}
scrollIndicatorInsets={this.props.scrollIndicatorInsets}
ref={(scrollRef)=> {
Expand Down Expand Up @@ -682,6 +688,9 @@ AutoDragSortableView.propTypes = {

sortable: PropTypes.bool,

refreshing: PropTypes. bool,
onRefresh: PropTypes.func,

onClickItem: PropTypes.func,
onDragStart: PropTypes.func,
onDragEnd: PropTypes.func,
Expand Down
3 changes: 3 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ interface IProps{

sortable?: boolean;

refreshing?: boolean;
onRefresh?: (() => void) | null;

onClickItem?: (data: any[],item: any,index: number) => void;
onDragStart?: (fromIndex: number) => void;
onDragEnd?: (fromIndex: number,toIndex: number) => void;
Expand Down