ScrollBar.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. update() {
  47. this.scrollbar.update();
  48. }
  49. handleRef = ref => {
  50. this.container = ref;
  51. };
  52. render() {
  53. return (
  54. <div className="baron baron__root baron__clipper">
  55. <div className={this.props.className + ' baron__scroller'} ref={this.handleRef}>
  56. {this.props.children}
  57. </div>
  58. <div className="baron__track">
  59. <div className="baron__bar" />
  60. </div>
  61. </div>
  62. );
  63. }
  64. }