Navigation Menu

Skip to content

Commit

Permalink
feat: add onSync callback to resolve #87 (#91)
Browse files Browse the repository at this point in the history
* feat: add onSync callback to resolve #87

* fix: should keep updateScroll as manual trigger api

* doc: update the README

* simplify code

Co-authored-by: Allen Yang <Goldenyz@hotmail.com>
  • Loading branch information
haoliangwu and goldenyz committed Jan 30, 2020
1 parent b208ee5 commit 755caea
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -79,6 +79,13 @@ All the callback 'onXXXX' can accept a parameter: the ref to the scrollbar conta
</PerfectScrollbar>
```

#### onSync
Invoked when `PerfectScrollbar` comp needs to sync the scrollbar container by invoking `ps.update()`(Basically, it is invoked in CDU lifecycle) and receive the internal `perfect-scroll` instance `ps` as parameter.

It is useful when you want to customize the sync logic in some scenarios, eg: debounce the invocation of `ps.update()`.

For more detail, please refer to [issue#87](https://github.com/goldenyz/react-perfect-scrollbar/issues/87) and the example directory.

#### React.HTMLAttributes
Any attributes defined in [React.HTMLAttributes](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L1689) can be used for the component.

Expand Down
65 changes: 47 additions & 18 deletions example/example.js
Expand Up @@ -10,13 +10,22 @@ function logEvent(type) {
console.log(`event '${type}' triggered!`);
}

const debounce = (fn, ms = 0) => {
let timeoutId;
return function wrapper(...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn.apply(this, args), ms);
};
};

class Example extends Component {
constructor(props) {
super(props);

this.state = {
className: undefined,
onXReachEnd: null,
items: Array.from(new Array(100).keys()),
};
}

Expand All @@ -33,28 +42,48 @@ class Example extends Component {
logEvent('onYReachEnd');
}

handleTrigger = () => {
this.setState({
items: Array.from(new Array(100).keys()),
});
}

handleSync = debounce((ps) => {
ps.update();
console.log('debounce sync ps container in 1000ms');
}, 1000)

render() {
const { className, onXReachEnd } = this.state;

return (
<div className="example">
<ScrollBar
className={className}
onScrollY={() => logEvent('onScrollY')}
onScrollX={() => logEvent('onScrollX')}
onScrollUp={() => logEvent('onScrollUp')}
onScrollDown={() => logEvent('onScrollDown')}
onScrollLeft={() => logEvent('onScrollLeft')}
onScrollRight={() => logEvent('onScrollRight')}
onYReachStart={() => logEvent('onYReachStart')}
onYReachEnd={this.handleYReachEnd}
onXReachStart={() => logEvent('onXReachStart')}
onXReachEnd={onXReachEnd}
component="div"
>
<div className="content" />
</ScrollBar>
</div>
<React.Fragment>

<div className="example">
<ScrollBar
className={className}
onScrollY={() => logEvent('onScrollY')}
onScrollX={() => logEvent('onScrollX')}
onScrollUp={() => logEvent('onScrollUp')}
onScrollDown={() => logEvent('onScrollDown')}
onScrollLeft={() => logEvent('onScrollLeft')}
onScrollRight={() => logEvent('onScrollRight')}
onYReachStart={() => logEvent('onYReachStart')}
onYReachEnd={this.handleYReachEnd}
onXReachStart={() => logEvent('onXReachStart')}
onXReachEnd={onXReachEnd}
component="div"
>
<div className="content" />
</ScrollBar>
</div>
<div className="example">
<button onClick={this.handleTrigger}>Trigger</button>
<ScrollBar onSync={this.handleSync}>
{this.state.items.map(e => (<div key={e}>{e}</div>))}
</ScrollBar>
</div>
</React.Fragment>
);
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/scrollbar.js
Expand Up @@ -37,7 +37,9 @@ export default class ScrollBar extends Component {

componentDidUpdate(prevProps) {
this._updateEventHook(prevProps);
this._ps.update();

this.updateScroll();

if (prevProps.className !== this.props.className) {
this._updateClassName();
}
Expand Down Expand Up @@ -90,7 +92,7 @@ export default class ScrollBar extends Component {
}

updateScroll() {
this._ps.update();
this.props.onSync(this._ps);
}

handleRef(ref) {
Expand All @@ -116,6 +118,7 @@ export default class ScrollBar extends Component {
onXReachStart,
onXReachEnd,
component,
onSync,
children,
...remainProps
} = this.props;
Expand Down Expand Up @@ -145,6 +148,7 @@ ScrollBar.defaultProps = {
onYReachEnd: undefined,
onXReachStart: undefined,
onXReachEnd: undefined,
onSync: ps => ps.update(),
component: 'div',
};

Expand All @@ -165,5 +169,6 @@ ScrollBar.propTypes = {
onYReachEnd: PropTypes.func,
onXReachStart: PropTypes.func,
onXReachEnd: PropTypes.func,
onSync: PropTypes.func,
component: PropTypes.string,
};

0 comments on commit 755caea

Please sign in to comment.