Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(DatePicker): fix programmatic set of date in range mode #4814

Merged
merged 4 commits into from
Jan 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions packages/react/src/components/DatePicker/plugins/rangePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,38 @@ import rangePlugin from 'flatpickr/dist/plugins/rangePlugin';
* @returns {Plugin} An extension of Flatpickr `rangePlugin` that does the following:
* * Better ensures the calendar dropdown is always aligned to the `<input>` for the starting date.
* Workaround for: https://github.com/flatpickr/flatpickr/issues/1944
* * A logic to ensure `fp.setDate()` call won't end up with "startDate to endDate" set to the first `<input>`
*/
export default config => {
const factory = rangePlugin(Object.assign({ position: 'left' }, config));
return fp =>
Object.assign(factory(fp), {
return fp => {
const origSetDate = fp.setDate;

const init = () => {
fp.setDate = function setDate(dates, triggerChange, format) {
origSetDate.call(this, dates, triggerChange, format);
// If `triggerChange` is `true`, `onValueUpdate` Flatpickr event is fired
// where Flatpickr's range plugin takes care of fixing the first `<input>`
if (!triggerChange) {
const { _input: inputFrom } = fp;
const { input: inputTo } = config;
[inputFrom, inputTo].forEach((input, i) => {
if (input) {
input.value = !dates[i]
? ''
: fp.formatDate(new Date(dates[i]), fp.config.dateFormat);
}
});
}
};
};

const origRangePlugin = factory(fp);
const { onReady: origOnReady } = origRangePlugin;

return Object.assign(origRangePlugin, {
onReady: [init, origOnReady],
onPreCalendarPosition() {},
});
};
};