1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import * as React from "react";
|
17 | import { Menu, MenuItem } from "@blueprintjs/core";
|
18 | import { DATERANGEPICKER_SHORTCUTS } from "./common/classes";
|
19 | import { clone, isDayRangeInRange } from "./common/dateUtils";
|
20 | export class Shortcuts extends React.PureComponent {
|
21 | static defaultProps = {
|
22 | selectedShortcutIndex: -1,
|
23 | };
|
24 | render() {
|
25 | const shortcuts = this.props.shortcuts === true
|
26 | ? createDefaultShortcuts(this.props.allowSingleDayRange, this.props.timePrecision !== undefined, this.props.useSingleDateShortcuts === true)
|
27 | : this.props.shortcuts;
|
28 | const shortcutElements = shortcuts.map((shortcut, index) => (
|
29 |
|
30 |
|
31 | React.createElement(MenuItem, { active: this.props.selectedShortcutIndex === index, disabled: !this.isShortcutInRange(shortcut.dateRange), key: index, onClick: this.getShorcutClickHandler(shortcut, index), shouldDismissPopover: false, text: shortcut.label })));
|
32 | return (React.createElement(Menu, { "aria-label": "Date picker shortcuts", className: DATERANGEPICKER_SHORTCUTS, tabIndex: 0 }, shortcutElements));
|
33 | }
|
34 | getShorcutClickHandler = (shortcut, index) => () => {
|
35 | const { onShortcutClick } = this.props;
|
36 | onShortcutClick(shortcut, index);
|
37 | };
|
38 | isShortcutInRange = (shortcutDateRange) => {
|
39 | const { minDate, maxDate } = this.props;
|
40 | return isDayRangeInRange(shortcutDateRange, [minDate, maxDate]);
|
41 | };
|
42 | }
|
43 | function createShortcut(label, dateRange) {
|
44 | return { dateRange, label };
|
45 | }
|
46 | function createDefaultShortcuts(allowSingleDayRange, hasTimePrecision, useSingleDateShortcuts) {
|
47 | const today = new Date();
|
48 | const makeDate = (action) => {
|
49 | const returnVal = clone(today);
|
50 | action(returnVal);
|
51 | returnVal.setDate(returnVal.getDate() + 1);
|
52 | return returnVal;
|
53 | };
|
54 | const tomorrow = makeDate(() => null);
|
55 | const yesterday = makeDate(d => d.setDate(d.getDate() - 2));
|
56 | const oneWeekAgo = makeDate(d => d.setDate(d.getDate() - 7));
|
57 | const oneMonthAgo = makeDate(d => d.setMonth(d.getMonth() - 1));
|
58 | const threeMonthsAgo = makeDate(d => d.setMonth(d.getMonth() - 3));
|
59 | const sixMonthsAgo = makeDate(d => d.setMonth(d.getMonth() - 6));
|
60 | const oneYearAgo = makeDate(d => d.setFullYear(d.getFullYear() - 1));
|
61 | const twoYearsAgo = makeDate(d => d.setFullYear(d.getFullYear() - 2));
|
62 | const singleDayShortcuts = allowSingleDayRange || useSingleDateShortcuts
|
63 | ? [
|
64 | createShortcut("Today", [today, hasTimePrecision ? tomorrow : today]),
|
65 | createShortcut("Yesterday", [yesterday, hasTimePrecision ? today : yesterday]),
|
66 | ]
|
67 | : [];
|
68 | return [
|
69 | ...singleDayShortcuts,
|
70 | createShortcut(useSingleDateShortcuts ? "1 week ago" : "Past week", [oneWeekAgo, today]),
|
71 | createShortcut(useSingleDateShortcuts ? "1 month ago" : "Past month", [oneMonthAgo, today]),
|
72 | createShortcut(useSingleDateShortcuts ? "3 months ago" : "Past 3 months", [threeMonthsAgo, today]),
|
73 |
|
74 | ...(useSingleDateShortcuts ? [] : [createShortcut("Past 6 months", [sixMonthsAgo, today])]),
|
75 | createShortcut(useSingleDateShortcuts ? "1 year ago" : "Past year", [oneYearAgo, today]),
|
76 | ...(useSingleDateShortcuts ? [] : [createShortcut("Past 2 years", [twoYearsAgo, today])]),
|
77 | ];
|
78 | }
|
79 |
|
\ | No newline at end of file |