-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Allow shortcuts to change time in Date Range Picker #3223
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,15 @@ import { TimePicker } from "./timePicker"; | |
| export interface IDateRangeShortcut { | ||
| label: string; | ||
| dateRange: DateRange; | ||
|
|
||
| /** | ||
| * By default, clicking a shortcut does not change the time of the date range picker, but instead | ||
| * takes the date components of the `dateRange` and combines it with the currently selected time. | ||
| * Setting `shouldChangeTime` to `true` will override this behavior and allow shortcuts to change | ||
| * the selected time as well. | ||
| * @default false | ||
| */ | ||
| shouldChangeTime?: boolean; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thoughts on calling this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though prop name is |
||
| } | ||
|
|
||
| export interface IDateRangePickerProps extends IDatePickerBaseProps, IProps { | ||
|
|
@@ -282,7 +291,7 @@ export class DateRangePicker extends AbstractPureComponent<IDateRangePickerProps | |
| <Shortcuts | ||
| key="shortcuts" | ||
| {...{ allowSingleDayRange, maxDate, minDate, shortcuts }} | ||
| onShortcutClick={this.handleNextState} | ||
| onShortcutClick={this.handleShortcutClick} | ||
| />, | ||
| <Divider key="div" />, | ||
| ]; | ||
|
|
@@ -495,6 +504,24 @@ export class DateRangePicker extends AbstractPureComponent<IDateRangePickerProps | |
| this.handleNextState(nextValue); | ||
| }; | ||
|
|
||
| private handleShortcutClick = (shortcut: IDateRangeShortcut) => { | ||
| const { dateRange, shouldChangeTime } = shortcut; | ||
| if (shouldChangeTime) { | ||
| const newDateRange: DateRange = [dateRange[0], dateRange[1]]; | ||
| const newTimeRange: DateRange = [dateRange[0], dateRange[1]]; | ||
| const nextState = getStateChange( | ||
| this.state.value, | ||
| dateRange, | ||
| this.state, | ||
| this.props.contiguousCalendarMonths, | ||
| ); | ||
| this.setState({ ...nextState, time: newTimeRange }); | ||
| Utils.safeInvoke(this.props.onChange, newDateRange); | ||
| } else { | ||
| this.handleNextState(dateRange); | ||
| } | ||
| }; | ||
|
|
||
| private handleNextState = (nextValue: DateRange) => { | ||
| const { value } = this.state; | ||
| nextValue[0] = DateUtils.getDateTime(nextValue[0], this.state.time[0]); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -904,47 +904,74 @@ describe("<DateRangePicker>", () => { | |
| }); | ||
|
|
||
| it("custom shortcuts set the displayed months correctly when start month changes", () => { | ||
| const dateRange = [new Date(2016, Months.JANUARY, 1), new Date(2016, Months.DECEMBER, 31)] as DateRange; | ||
| const { left, right } = render({ | ||
| initialMonth: new Date(2015, Months.JANUARY, 1), | ||
| shortcuts: [{ label: "custom shortcut", dateRange }], | ||
| }).clickShortcut(); | ||
| assert.isTrue(onChangeSpy.calledOnce); | ||
| left.assertMonthYear(Months.JANUARY, 2016); | ||
| right.assertMonthYear(Months.FEBRUARY, 2016); | ||
| }); | ||
| const dateRange = [ | ||
| new Date(2016, Months.JANUARY, 1, 10, 20, 30), | ||
| new Date(2016, Months.DECEMBER, 31, 10, 20, 30), | ||
| ] as DateRange; | ||
|
|
||
| it( | ||
| "custom shortcuts set the displayed months correctly when start month changes " + | ||
| "and contiguousCalendarMonths is false", | ||
| () => { | ||
| const dateRange = [new Date(2016, Months.JANUARY, 1), new Date(2016, Months.DECEMBER, 31)] as DateRange; | ||
| const test = (shouldChangeTime: boolean) => { | ||
| const { left, right } = render({ | ||
| contiguousCalendarMonths: false, | ||
| initialMonth: new Date(2015, Months.JANUARY, 1), | ||
| shortcuts: [{ label: "custom shortcut", dateRange }], | ||
| shortcuts: [{ label: "custom shortcut", dateRange, shouldChangeTime }], | ||
| }).clickShortcut(); | ||
| assert.isTrue(onChangeSpy.calledOnce); | ||
| left.assertMonthYear(Months.JANUARY, 2016); | ||
| right.assertMonthYear(Months.DECEMBER, 2016); | ||
| right.assertMonthYear(Months.FEBRUARY, 2016); | ||
| }; | ||
|
|
||
| test(true); | ||
| test(false); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ these should be separate honestly i'd prefer if you 1) reverted this entire file and then 2) simply added a single test with |
||
| }); | ||
|
|
||
| it( | ||
| "custom shortcuts set the displayed months correctly when start month changes " + | ||
| "and contiguousCalendarMonths is false", | ||
| () => { | ||
| const dateRange = [ | ||
| new Date(2016, Months.JANUARY, 1, 10, 20, 30), | ||
| new Date(2016, Months.DECEMBER, 31, 10, 20, 30), | ||
| ] as DateRange; | ||
|
|
||
| const test = (shouldChangeTime: boolean) => { | ||
| const { left, right } = render({ | ||
| contiguousCalendarMonths: false, | ||
| initialMonth: new Date(2015, Months.JANUARY, 1), | ||
| shortcuts: [{ label: "custom shortcut", dateRange, shouldChangeTime }], | ||
| }).clickShortcut(); | ||
| assert.isTrue(onChangeSpy.calledOnce); | ||
| left.assertMonthYear(Months.JANUARY, 2016); | ||
| right.assertMonthYear(Months.DECEMBER, 2016); | ||
| }; | ||
|
|
||
| test(true); | ||
| test(false); | ||
| }, | ||
| ); | ||
|
|
||
| it("custom shortcuts set the displayed months correctly when start month stays the same", () => { | ||
| const dateRange = [new Date(2016, Months.JANUARY, 1), new Date(2016, Months.DECEMBER, 31)] as DateRange; | ||
| const { clickShortcut, left, right } = render({ | ||
| initialMonth: new Date(2016, Months.JANUARY, 1), | ||
| shortcuts: [{ label: "custom shortcut", dateRange }], | ||
| }); | ||
| const dateRange = [ | ||
| new Date(2016, Months.JANUARY, 1, 10, 20, 30), | ||
| new Date(2016, Months.DECEMBER, 31, 10, 20, 30), | ||
| ] as DateRange; | ||
|
|
||
| const test = (shouldChangeTime: boolean) => { | ||
| const { clickShortcut, left, right } = render({ | ||
| initialMonth: new Date(2016, Months.JANUARY, 1), | ||
| shortcuts: [{ label: "custom shortcut", dateRange, shouldChangeTime }], | ||
| }); | ||
|
|
||
| clickShortcut(); | ||
| assert.isTrue(onChangeSpy.calledOnce); | ||
| left.assertMonthYear(Months.JANUARY, 2016); | ||
| right.assertMonthYear(Months.FEBRUARY, 2016); | ||
| clickShortcut(); | ||
| assert.isTrue(onChangeSpy.calledOnce); | ||
| left.assertMonthYear(Months.JANUARY, 2016); | ||
| right.assertMonthYear(Months.FEBRUARY, 2016); | ||
|
|
||
| clickShortcut(); | ||
| left.assertMonthYear(Months.JANUARY, 2016); | ||
| right.assertMonthYear(Months.FEBRUARY, 2016); | ||
| clickShortcut(); | ||
| left.assertMonthYear(Months.JANUARY, 2016); | ||
| right.assertMonthYear(Months.FEBRUARY, 2016); | ||
| }; | ||
|
|
||
| test(true); | ||
| test(false); | ||
| }); | ||
| }); | ||
|
|
||
|
|
@@ -1147,11 +1174,28 @@ describe("<DateRangePicker>", () => { | |
| assert.isTrue(DateUtils.areSameDay(onChangeSpy.firstCall.args[0][0] as Date, new Date())); | ||
| }); | ||
|
|
||
| it("clicking a shortcut doesn't change time", () => { | ||
| it("clicking a shortcut with shouldChangeTime=false doesn't change time", () => { | ||
| render({ timePrecision: "minute", defaultValue: defaultRange }).clickShortcut(); | ||
| assert.isTrue(DateUtils.areSameTime(onChangeSpy.firstCall.args[0][0] as Date, defaultRange[0])); | ||
| }); | ||
|
|
||
| it("clicking a shortcut with shouldChangeTime=true changes time", () => { | ||
| const endTime = defaultRange[1]; | ||
| const startTime = new Date(defaultRange[1].getTime()); | ||
| startTime.setHours(startTime.getHours() - 2); | ||
|
|
||
| const shortcuts = [ | ||
| { | ||
| dateRange: [startTime, endTime] as DateRange, | ||
| label: "custom shortcut", | ||
| shouldChangeTime: true, | ||
| }, | ||
| ]; | ||
|
|
||
| render({ timePrecision: "minute", defaultValue: defaultRange, shortcuts }).clickShortcut(); | ||
| assert.equal(onChangeSpy.firstCall.args[0][0] as Date, startTime); | ||
| }); | ||
|
|
||
| it("selecting and unselecting a day doesn't change time", () => { | ||
| const leftDatePicker = render({ timePrecision: "minute", defaultValue: defaultRange }).left; | ||
| leftDatePicker.clickDay(5); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.