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

feat: add onSync callback to resolve #87 #91

Merged
merged 4 commits into from
Jan 30, 2020
Merged
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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,
};