UNPKG

4.21 kBJavaScriptView Raw
1/*
2 * Copyright 2018 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import * as React from "react";
17import { Menu, MenuItem } from "@blueprintjs/core";
18import { DATERANGEPICKER_SHORTCUTS } from "./common/classes";
19import { clone, isDayRangeInRange } from "./common/dateUtils";
20export 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 // ok to use this here because it doesn't have a submenu
30 // eslint-disable-next-line deprecation/deprecation, @blueprintjs/no-deprecated-components
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}
43function createShortcut(label, dateRange) {
44 return { dateRange, label };
45}
46function 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 // Don't include a couple of these for the single date shortcut
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//# sourceMappingURL=shortcuts.js.map
\No newline at end of file