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

Date range picker inconsistency when single day range and time precis… #3557

Merged
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions packages/datetime/src/dateRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,11 @@ export class DateRangePicker extends AbstractPureComponent<IDateRangePickerProps
return null;
}

const { allowSingleDayRange, maxDate, minDate } = this.props;
const { allowSingleDayRange, maxDate, minDate, timePrecision } = this.props;
return [
<Shortcuts
key="shortcuts"
{...{ allowSingleDayRange, maxDate, minDate, shortcuts }}
{...{ allowSingleDayRange, maxDate, minDate, shortcuts, timePrecision }}
onShortcutClick={this.handleShortcutClick}
/>,
<Divider key="div" />,
Expand Down
12 changes: 9 additions & 3 deletions packages/datetime/src/shortcuts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Classes, Menu, MenuItem } from "@blueprintjs/core";
import * as React from "react";
import { DATERANGEPICKER_SHORTCUTS } from "./common/classes";
import { clone, DateRange, isDayRangeInRange } from "./common/dateUtils";
import { TimePrecision } from "./timePicker";

export interface IDateRangeShortcut {
/** Shortcut label that appears in the list. */
Expand All @@ -44,14 +45,15 @@ export interface IShortcutsProps {
minDate: Date;
maxDate: Date;
shortcuts: IDateRangeShortcut[] | true;
timePrecision: TimePrecision;
onShortcutClick: (shortcut: IDateRangeShortcut) => void;
}

export class Shortcuts extends React.PureComponent<IShortcutsProps> {
public render() {
const shortcuts =
this.props.shortcuts === true
? createDefaultShortcuts(this.props.allowSingleDayRange)
? createDefaultShortcuts(this.props.allowSingleDayRange, this.props.timePrecision !== undefined)
: this.props.shortcuts;

const shortcutElements = shortcuts.map((s, i) => (
Expand Down Expand Up @@ -80,7 +82,7 @@ function createShortcut(label: string, dateRange: DateRange): IDateRangeShortcut
return { dateRange, label };
}

function createDefaultShortcuts(allowSingleDayRange: boolean) {
function createDefaultShortcuts(allowSingleDayRange: boolean, hasTimePrecision: boolean) {
const today = new Date();
const makeDate = (action: (d: Date) => void) => {
const returnVal = clone(today);
Expand All @@ -89,6 +91,7 @@ function createDefaultShortcuts(allowSingleDayRange: boolean) {
return returnVal;
};

const tomorrow = makeDate(() => null);
const yesterday = makeDate(d => d.setDate(d.getDate() - 2));
const oneWeekAgo = makeDate(d => d.setDate(d.getDate() - 7));
const oneMonthAgo = makeDate(d => d.setMonth(d.getMonth() - 1));
Expand All @@ -98,7 +101,10 @@ function createDefaultShortcuts(allowSingleDayRange: boolean) {
const twoYearsAgo = makeDate(d => d.setFullYear(d.getFullYear() - 2));

const singleDayShortcuts = allowSingleDayRange
? [createShortcut("Today", [today, today]), createShortcut("Yesterday", [yesterday, yesterday])]
? [
createShortcut("Today", [today, hasTimePrecision ? tomorrow : today]),
createShortcut("Yesterday", [yesterday, hasTimePrecision ? today : yesterday]),
]
: [];

return [
Expand Down
13 changes: 13 additions & 0 deletions packages/datetime/test/dateRangePickerTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,19 @@ describe("<DateRangePicker>", () => {
assert.isTrue(DateUtils.areSameDay(today, value[1]));
});

it("shortcuts fire onChange with correct values when single day range and allowSingleDayRange enabled", () => {
render({ allowSingleDayRange: true, timePrecision: "minute" }).clickShortcut();

const today = new Date();
const tomorrow = DateUtils.clone(today);
tomorrow.setDate(today.getDate() + 1);

assert.isTrue(onChangeSpy.calledOnce);
const value = onChangeSpy.args[0][0];
assert.isTrue(DateUtils.areSameDay(today, value[0]));
assert.isTrue(DateUtils.areSameDay(tomorrow, value[1]));
});

it("custom shortcuts select the correct values", () => {
const dateRange = [new Date(2015, Months.JANUARY, 1), new Date(2015, Months.JANUARY, 5)] as DateRange;
render({
Expand Down