Made scrollbar have scrollTop and setScrollTop props so we can control scroll position

This commit is contained in:
Torkel Ödegaard
2019-01-17 11:25:44 +01:00
parent bc956057c3
commit 9cb49d14cd
3 changed files with 68 additions and 25 deletions

View File

@@ -1,4 +1,5 @@
import React, { PureComponent } from 'react';
import _ from 'lodash';
import Scrollbars from 'react-custom-scrollbars';
interface Props {
@@ -8,6 +9,8 @@ interface Props {
autoHideDuration?: number;
autoMaxHeight?: string;
hideTracksWhenNotNeeded?: boolean;
scrollTop?: number;
setScrollTop: (value: React.MouseEvent<HTMLElement>) => void;
}
/**
@@ -21,13 +24,43 @@ export class CustomScrollbar extends PureComponent<Props> {
autoHideDuration: 200,
autoMaxHeight: '100%',
hideTracksWhenNotNeeded: false,
scrollTop: 0,
setScrollTop: () => {},
};
private ref: React.RefObject<Scrollbars>;
constructor(props: Props) {
super(props);
this.ref = React.createRef<Scrollbars>();
}
updateScroll() {
const ref = this.ref.current;
if (ref && !_.isNil(this.props.scrollTop)) {
if (this.props.scrollTop > 10000) {
ref.scrollToBottom();
} else {
ref.scrollTop(this.props.scrollTop);
}
}
}
componentDidMount() {
this.updateScroll();
}
componentDidUpdate() {
this.updateScroll();
}
render() {
const { customClassName, children, autoMaxHeight } = this.props;
return (
<Scrollbars
ref={this.ref}
className={customClassName}
autoHeight={true}
// These autoHeightMin & autoHeightMax options affect firefox and chrome differently.