Skip to content

Commit

Permalink
fix: fix picklist still opened after scroll in drawer content area
Browse files Browse the repository at this point in the history
  • Loading branch information
wildergd committed Oct 9, 2020
1 parent 248ed20 commit c76680b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/components/Picklist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import RenderIf from '../RenderIf';
import { ESCAPE_KEY, TAB_KEY } from '../../libs/constants';
import { uniqueId } from '../../libs/utils';
import OutsideClick from '../../libs/outsideClick';
import { WindowScrolling } from '../../libs/scrollController';
import Label from '../Input/label';
import getNormalizeValue from './helpers/getNormalizeValue';
import shouldOpenMenu from './helpers/shouldOpenMenu';
Expand Down Expand Up @@ -54,6 +55,7 @@ class Picklist extends Component {
this.handleChange = this.handleChange.bind(this);
this.closeAndFocusInput = this.closeAndFocusInput.bind(this);
this.outsideClick = new OutsideClick();
this.windowScrolling = new WindowScrolling();
this.activeChildren = [];
this.state = {
isOpen: false,
Expand All @@ -69,11 +71,15 @@ class Picklist extends Component {
const { isOpen } = this.state;
if (!wasOpen && isOpen) {
this.outsideClick.startListening(this.containerRef.current, () => this.closeMenu());
this.windowScrolling.startListening(() => this.closeMenu());
} else if (wasOpen && !isOpen) {
this.windowScrolling.stopListening();
}
}

componentWillUnmount() {
this.outsideClick.stopListening();
this.windowScrolling.stopListening();
}

getErrorMessageId() {
Expand Down
2 changes: 2 additions & 0 deletions src/libs/scrollController/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,5 @@ export function enableBodyScroll(targetElement) {
}
}
}

export { default as WindowScrolling } from './windowScrolling';
38 changes: 38 additions & 0 deletions src/libs/scrollController/windowScrolling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const privateHandleScroll = Symbol('handleScroll');

class WindowScrolling {
constructor() {
this.callback = null;
this.listening = false;
this.isTicking = false;
this[privateHandleScroll] = this[privateHandleScroll].bind(this);
}

[privateHandleScroll](event) {
if (!this.isTicking) {
window.requestAnimationFrame(() => {
this.callback(this.callback(this, event));
this.isTicking = false;
});
this.isTicking = true;
}
}

startListening(callback) {
this.callback = callback;
window.addEventListener('scroll', this[privateHandleScroll]);
window.addEventListener('wheel', this[privateHandleScroll]);
this.listening = true;
}

stopListening() {
if (this.listening) {
this.listening = false;
window.removeEventListener('scroll', this[privateHandleScroll]);
window.removeEventListener('wheel', this[privateHandleScroll]);
this.callback = null;
}
}
}

export default WindowScrolling;

0 comments on commit c76680b

Please sign in to comment.