ScrollBar.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. wheelPropagation: true,
  16. });
  17. }
  18. componentDidUpdate() {
  19. this.ps.update();
  20. }
  21. componentWillUnmount() {
  22. this.ps.destroy();
  23. }
  24. // methods can be invoked by outside
  25. setScrollTop(top) {
  26. if (this.container) {
  27. this.container.scrollTop = top;
  28. this.ps.update();
  29. return true;
  30. }
  31. return false;
  32. }
  33. setScrollLeft(left) {
  34. if (this.container) {
  35. this.container.scrollLeft = left;
  36. this.ps.update();
  37. return true;
  38. }
  39. return false;
  40. }
  41. handleRef = ref => {
  42. this.container = ref;
  43. };
  44. render() {
  45. return (
  46. <div className={this.props.className} ref={this.handleRef}>
  47. {this.props.children}
  48. </div>
  49. );
  50. }
  51. }