UNPKG

6.61 kBTypeScriptView 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 */
16
17import * as React from "react";
18
19import { Menu, MenuItem } from "@blueprintjs/core";
20
21import { DATERANGEPICKER_SHORTCUTS } from "./common/classes";
22import { DateRange } from "./common/dateRange";
23import { clone, isDayRangeInRange } from "./common/dateUtils";
24import { TimePrecision } from "./timePicker";
25
26export interface IDateShortcutBase {
27 /** Shortcut label that appears in the list. */
28 label: string;
29
30 /**
31 * Set this prop to `true` to allow this shortcut to change the selected
32 * times as well as the dates. By default, time components of a shortcut are
33 * ignored; clicking a shortcut takes the date components of the `dateRange`
34 * and combines them with the currently selected time.
35 *
36 * @default false
37 */
38 includeTime?: boolean;
39}
40
41// eslint-disable-next-line deprecation/deprecation
42export type DateRangeShortcut = IDateRangeShortcut;
43/** @deprecated use DateRangeShortcut */
44export interface IDateRangeShortcut extends IDateShortcutBase {
45 /**
46 * Date range represented by this shortcut. Note that time components of a
47 * shortcut are ignored by default; set `includeTime: true` to respect them.
48 */
49 dateRange: DateRange;
50}
51
52// eslint-disable-next-line deprecation/deprecation
53export type DatePickerShortcut = IDatePickerShortcut;
54/** @deprecated use DatePickerShortcut */
55export interface IDatePickerShortcut extends IDateShortcutBase {
56 /**
57 * Date represented by this shortcut. Note that time components of a
58 * shortcut are ignored by default; set `includeTime: true` to respect them.
59 */
60 date: Date;
61}
62
63export interface IShortcutsProps {
64 allowSingleDayRange: boolean;
65 minDate: Date;
66 maxDate: Date;
67 shortcuts: DateRangeShortcut[] | true;
68 timePrecision: TimePrecision;
69 selectedShortcutIndex?: number;
70 onShortcutClick: (shortcut: DateRangeShortcut, index: number) => void;
71 /**
72 * The DatePicker component reuses this component for a single date.
73 * This changes the default shortcut labels and affects which shortcuts are used.
74 *
75 * @default false
76 */
77 useSingleDateShortcuts?: boolean;
78}
79
80export class Shortcuts extends React.PureComponent<IShortcutsProps> {
81 public static defaultProps: Partial<IShortcutsProps> = {
82 selectedShortcutIndex: -1,
83 };
84
85 public render() {
86 const shortcuts =
87 this.props.shortcuts === true
88 ? createDefaultShortcuts(
89 this.props.allowSingleDayRange,
90 this.props.timePrecision !== undefined,
91 this.props.useSingleDateShortcuts === true,
92 )
93 : this.props.shortcuts;
94
95 const shortcutElements = shortcuts.map((shortcut, index) => (
96 // ok to use this here because it doesn't have a submenu
97 // eslint-disable-next-line deprecation/deprecation, @blueprintjs/no-deprecated-components
98 <MenuItem
99 active={this.props.selectedShortcutIndex === index}
100 disabled={!this.isShortcutInRange(shortcut.dateRange)}
101 key={index}
102 onClick={this.getShorcutClickHandler(shortcut, index)}
103 shouldDismissPopover={false}
104 text={shortcut.label}
105 />
106 ));
107
108 return (
109 <Menu aria-label="Date picker shortcuts" className={DATERANGEPICKER_SHORTCUTS} tabIndex={0}>
110 {shortcutElements}
111 </Menu>
112 );
113 }
114
115 private getShorcutClickHandler = (shortcut: DateRangeShortcut, index: number) => () => {
116 const { onShortcutClick } = this.props;
117
118 onShortcutClick(shortcut, index);
119 };
120
121 private isShortcutInRange = (shortcutDateRange: DateRange) => {
122 const { minDate, maxDate } = this.props;
123
124 return isDayRangeInRange(shortcutDateRange, [minDate, maxDate]);
125 };
126}
127
128function createShortcut(label: string, dateRange: DateRange): DateRangeShortcut {
129 return { dateRange, label };
130}
131
132function createDefaultShortcuts(
133 allowSingleDayRange: boolean,
134 hasTimePrecision: boolean,
135 useSingleDateShortcuts: boolean,
136) {
137 const today = new Date();
138 const makeDate = (action: (d: Date) => void) => {
139 const returnVal = clone(today);
140 action(returnVal);
141 returnVal.setDate(returnVal.getDate() + 1);
142 return returnVal;
143 };
144
145 const tomorrow = makeDate(() => null);
146 const yesterday = makeDate(d => d.setDate(d.getDate() - 2));
147 const oneWeekAgo = makeDate(d => d.setDate(d.getDate() - 7));
148 const oneMonthAgo = makeDate(d => d.setMonth(d.getMonth() - 1));
149 const threeMonthsAgo = makeDate(d => d.setMonth(d.getMonth() - 3));
150 const sixMonthsAgo = makeDate(d => d.setMonth(d.getMonth() - 6));
151 const oneYearAgo = makeDate(d => d.setFullYear(d.getFullYear() - 1));
152 const twoYearsAgo = makeDate(d => d.setFullYear(d.getFullYear() - 2));
153
154 const singleDayShortcuts =
155 allowSingleDayRange || useSingleDateShortcuts
156 ? [
157 createShortcut("Today", [today, hasTimePrecision ? tomorrow : today]),
158 createShortcut("Yesterday", [yesterday, hasTimePrecision ? today : yesterday]),
159 ]
160 : [];
161
162 return [
163 ...singleDayShortcuts,
164 createShortcut(useSingleDateShortcuts ? "1 week ago" : "Past week", [oneWeekAgo, today]),
165 createShortcut(useSingleDateShortcuts ? "1 month ago" : "Past month", [oneMonthAgo, today]),
166 createShortcut(useSingleDateShortcuts ? "3 months ago" : "Past 3 months", [threeMonthsAgo, today]),
167 // Don't include a couple of these for the single date shortcut
168 ...(useSingleDateShortcuts ? [] : [createShortcut("Past 6 months", [sixMonthsAgo, today])]),
169 createShortcut(useSingleDateShortcuts ? "1 year ago" : "Past year", [oneYearAgo, today]),
170 ...(useSingleDateShortcuts ? [] : [createShortcut("Past 2 years", [twoYearsAgo, today])]),
171 ];
172}