UNPKG

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