ScrollBar.tsx 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import React from 'react';
  2. import PerfectScrollbar from 'perfect-scrollbar';
  3. export interface Props {
  4. children: any;
  5. className: string;
  6. }
  7. export default class ScrollBar extends React.Component<Props, any> {
  8. private container: any;
  9. private ps: PerfectScrollbar;
  10. constructor(props) {
  11. super(props);
  12. }
  13. componentDidMount() {
  14. this.ps = new PerfectScrollbar(this.container);
  15. }
  16. componentDidUpdate() {
  17. this.ps.update();
  18. }
  19. componentWillUnmount() {
  20. this.ps.destroy();
  21. }
  22. // methods can be invoked by outside
  23. setScrollTop(top) {
  24. if (this.container) {
  25. this.container.scrollTop = top;
  26. this.ps.update();
  27. return true;
  28. }
  29. return false;
  30. }
  31. setScrollLeft(left) {
  32. if (this.container) {
  33. this.container.scrollLeft = left;
  34. this.ps.update();
  35. return true;
  36. }
  37. return false;
  38. }
  39. handleRef = ref => {
  40. this.container = ref;
  41. };
  42. render() {
  43. return (
  44. <div className={this.props.className} ref={this.handleRef}>
  45. {this.props.children}
  46. </div>
  47. );
  48. }
  49. }