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

added tap / click elements example to readme #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,41 @@ var HorizontalScroll = React.createClass({
})
```

## Tap / Click Elements inside IScroll

To override the native scrolling iScroll has to inhibit some default browser behaviors, such as mouse clicks. See [iScroll manual](http://iscrolljs.com/) options.click and options.tap. The following is an example of including a clickable element.

```js
constructor(props) {
super(props);
this.refDict = [];
}

componentDidMount() {
this.refDict[0].addEventListener("tap", () => this.handleTap(this.refDict[0]));
Copy link
Owner

Choose a reason for hiding this comment

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

With comlicated code it would be better to check presence of element

if (this.myElementRef) {
  // add listener
}

Copy link
Owner

Choose a reason for hiding this comment

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

With binded handler to class you can do simple

.addEventListener("tap", this.handleTap)

}

Copy link
Owner

Choose a reason for hiding this comment

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

And when you add listener, you have to remove it too

componentWillUnmount() {
  if(this.myElementRef) {
    this.myElementRef.removeEventListener("tap", this.handleTap)
  }
}

handleTap(e) {
Copy link
Owner

Choose a reason for hiding this comment

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

Bind tap handler in constructor:

  this.handleTap = this.handleTap.bind(this)

or use class props syntax:

handleTap = (ev) => {
 ....
}

Then you dont need to call it () => this.handleTap()

console.log(e.id);
}

render(){
return(
<ReactIScroll iScroll={iScroll}
options={{
tap: true
}}
>
<div>
<div id={0} ref={el => this.refDict[0] = el}>
Copy link
Owner

Choose a reason for hiding this comment

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

With simple example like this you can save ref directly.

ref={el => this.myElementRef = el}

Click me in react-iscroll
</div>
</div>
</ReactIScroll>
)
}
```

## Example

There is example application. You can run it with this commands:
Expand Down