Skip to content

Commit

Permalink
fix: delay handle change on one tick
Browse files Browse the repository at this point in the history
  • Loading branch information
arfedulov committed Aug 24, 2018
1 parent d82c7a4 commit 4e012f4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ nbbuild/
dist/
nbdist/
.nb-gradle/

.vscode
6 changes: 5 additions & 1 deletion src/inputs/DateTimeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
getInitializer,
TIME_FORMAT,
} from './parse';
import { getUnhandledProps } from '../lib';
import { getUnhandledProps, tick } from '../lib';

function getNextMode(currentMode) {
if (currentMode === 'year') return 'month';
Expand Down Expand Up @@ -130,6 +130,10 @@ class DateTimeInput extends BaseInput {
}

handleSelect = (e, { value }) => {
tick(this.handleSelectUndelayed, e, { value });
}

handleSelectUndelayed = (e, { value }) => {
if (this.props.closable && this.state.mode === 'minute') {
this.closePopup();
}
Expand Down
6 changes: 5 additions & 1 deletion src/inputs/TimeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getInitializer,
TIME_FORMAT,
} from './parse';
import { getUnhandledProps } from '../lib';
import { getUnhandledProps, tick } from '../lib';

function getNextMode(currentMode) {
if (currentMode === 'hour') return 'minute';
Expand All @@ -35,6 +35,10 @@ class TimeInput extends BaseInput {
}

handleSelect = (e, { value }) => {
tick(this.handleSelectUndelayed, e, { value });
}

handleSelectUndelayed = (e, { value }) => {
const {
hour,
minute,
Expand Down
1 change: 1 addition & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as getUnhandledProps } from './getUnhandledProps';
export { default as tick } from './tick';
15 changes: 15 additions & 0 deletions src/lib/tick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** Set zero timeout.
*
* Sometimes we need to delay rerendering components
* on one tick (if they are inside `Popup` and rerendering could
* change `Popup`'s content sizes).
* Becouse it races with Popup's onclick handler.
* `Popup` relies on it's content sizes when computing
* should popup stay open or be closed. So we need
* to wait until `Popup`'s onclick handler done its job.
*/
const tick = (leadToRerendering, ...args) => {
setTimeout(leadToRerendering, 0, ...args);
};

export default tick;

0 comments on commit 4e012f4

Please sign in to comment.