Skip to content

Commit

Permalink
Merge pull request #45 from marekrozmus/fix_for_issue_43
Browse files Browse the repository at this point in the history
feat(43): Add max swipe prop
  • Loading branch information
marekrozmus authored Aug 13, 2023
2 parents 16e680f + bbb26be commit 0b5e3f6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ Type: `LeadingActions component`

Container component that sets up correct props in `SwipeAction`. See examples for usage.

### maxSwipe

Type: 'number' (optional, default: `1.0`)

Limit the swipe to percent of width, e.g.: `0.5` will make swipe possible only for 50% of elements's width

### onClick

Type: `function` (optional)
Expand Down
1 change: 1 addition & 0 deletions examples/src/android/WithOneAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const WithOneAction = ({
onSwipeProgress={setSwipeProgress}
onSwipeStart={handleSwipeStart}
onClick={handleOnClick(id)}
maxSwipe={0.5}
>
<ItemContent>
<ItemRow>
Expand Down
25 changes: 23 additions & 2 deletions src/SwipeableListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ class SwipeableListItem extends PureComponent {
if (this.props.resetState) {
this.props.resetState(this.playReturnAnimation);
}

if (this.props.maxSwipe) {
this.maxSwipeThreshold =
this.props.maxSwipe * this.listElement.offsetWidth;
} else {
this.maxSwipeThreshold = this.listElement.offsetWidth;
}
}

componentWillUnmount() {
Expand Down Expand Up @@ -229,7 +236,13 @@ class SwipeableListItem extends PureComponent {
event.stopPropagation();
event.preventDefault();

this.left = clientX - this.dragStartPoint.x;
const newLeft = clientX - this.dragStartPoint.x;

if (newLeft > 0) {
this.left = Math.min(newLeft, this.maxSwipeThreshold);
} else {
this.left = Math.max(newLeft, -this.maxSwipeThreshold);
}
this.scheduleUpdatePosition();
}
}
Expand All @@ -249,7 +262,14 @@ class SwipeableListItem extends PureComponent {
event.stopPropagation();
event.preventDefault();

this.left = clientX - this.dragStartPoint.x;
const newLeft = clientX - this.dragStartPoint.x;

if (newLeft > 0) {
this.left = Math.min(newLeft, this.maxSwipeThreshold);
} else {
this.left = Math.max(newLeft, -this.maxSwipeThreshold);
}

this.scheduleUpdatePosition();
}
}
Expand Down Expand Up @@ -828,6 +848,7 @@ SwipeableListItem.propTypes = {
fullSwipe: PropTypes.bool,
leadingActions: PropTypes.node,
listType: PropTypes.oneOf(Object.values(ListType)),
maxSwipe: PropTypes.number,
onClick: PropTypes.func,
onSwipeEnd: PropTypes.func,
onSwipeProgress: PropTypes.func,
Expand Down
6 changes: 6 additions & 0 deletions src/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ interface SwipeableListItemProps {
* It can be set for the whole list or for single item. See `type` for `SwipeableList`. Value from the `SwipeableListItem` takes precedence.
*/
listType?: Type;
/**
* default: 1.0
*
* Limit the swipe to percent of width, e.g.: 0.5 will make swipe possible only for 50% of elements's width
*/
maxSwipe: number;
/**
* Fired when item is clicked.
*/
Expand Down

0 comments on commit 0b5e3f6

Please sign in to comment.