ScrollBar.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React from 'react';
  2. import baron from 'baron';
  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 scrollbar: baron;
  10. constructor(props) {
  11. super(props);
  12. }
  13. componentDidMount() {
  14. this.scrollbar = baron({
  15. root: this.container.parentElement,
  16. scroller: this.container,
  17. bar: '.baron__bar',
  18. barOnCls: '_scrollbar',
  19. scrollingCls: '_scrolling',
  20. track: '.baron__track',
  21. });
  22. }
  23. componentDidUpdate() {
  24. this.scrollbar.update();
  25. }
  26. componentWillUnmount() {
  27. this.scrollbar.dispose();
  28. }
  29. // methods can be invoked by outside
  30. setScrollTop(top) {
  31. if (this.container) {
  32. this.container.scrollTop = top;
  33. this.scrollbar.update();
  34. return true;
  35. }
  36. return false;
  37. }
  38. setScrollLeft(left) {
  39. if (this.container) {
  40. this.container.scrollLeft = left;
  41. this.scrollbar.update();
  42. return true;
  43. }
  44. return false;
  45. }
  46. handleRef = ref => {
  47. this.container = ref;
  48. };
  49. render() {
  50. return (
  51. <div className="baron baron__root baron__clipper">
  52. <div className={this.props.className + ' baron__scroller'} ref={this.handleRef}>
  53. {this.props.children}
  54. </div>
  55. <div className="baron__track">
  56. <div className="baron__bar" />
  57. </div>
  58. </div>
  59. );
  60. }
  61. }