UNPKG

4.82 kBTypeScriptView Raw
1/*
2 * Copyright 2015 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 classNames from "classnames";
18import * as React from "react";
19
20import { AbstractPureComponent2, DISPLAYNAME_PREFIX, Props } from "@blueprintjs/core";
21
22import * as Classes from "./common/classes";
23import * as DateUtils from "./common/dateUtils";
24import { DatePicker, DatePickerProps } from "./datePicker";
25import { TimePicker, TimePickerProps } from "./timePicker";
26
27export interface IDateTimePickerProps extends Props {
28 /**
29 * The initial date and time value that will be set.
30 * This will be ignored if `value` is set.
31 *
32 * @default Date.now()
33 */
34 defaultValue?: Date;
35
36 /**
37 * Any props to be passed on to the `DatePicker` other than the `value` and `onChange` props as they come directly
38 * from the `DateTimePicker` props.
39 */
40 datePickerProps?: DatePickerProps;
41
42 /**
43 * Callback invoked when the user changes the date or time.
44 */
45 onChange?: (selectedDate: Date, isUserChange: boolean) => void;
46
47 /**
48 * Any props to be passed on to the `TimePicker` other than the `value` and `onChange` props as they come directly
49 * from the `DateTimePicker` props.
50 */
51 timePickerProps?: TimePickerProps;
52
53 /**
54 * The currently set date and time. If this prop is provided, the component acts in a controlled manner.
55 */
56 value?: Date | null;
57
58 /**
59 * Allows the user to clear the selection by clicking the currently selected day.
60 *
61 * @default true
62 */
63 canClearSelection?: boolean;
64}
65
66// Handle date and time separately because changing the date shouldn't reset the time.
67export interface IDateTimePickerState {
68 dateValue?: Date;
69 timeValue?: Date;
70}
71
72/** @deprecated since 3.4.0. Prefer `<DatePicker>` with `timePrecision` and `timePickerProps`. */
73export class DateTimePicker extends AbstractPureComponent2<IDateTimePickerProps, IDateTimePickerState> {
74 public static defaultProps: IDateTimePickerProps = {
75 canClearSelection: true,
76 defaultValue: new Date(),
77 };
78
79 public static displayName = `${DISPLAYNAME_PREFIX}.DateTimePicker`;
80
81 public constructor(props?: IDateTimePickerProps, context?: any) {
82 super(props, context);
83
84 const initialValue = this.props.value !== undefined ? this.props.value : this.props.defaultValue;
85 this.state = {
86 dateValue: initialValue,
87 timeValue: initialValue,
88 };
89 }
90
91 public render() {
92 const value = DateUtils.getDateTime(this.state.dateValue, this.state.timeValue);
93 return (
94 <div className={classNames(Classes.DATETIMEPICKER, this.props.className)}>
95 <DatePicker
96 {...this.props.datePickerProps}
97 canClearSelection={this.props.canClearSelection}
98 onChange={this.handleDateChange}
99 value={value}
100 />
101 <TimePicker
102 {...this.props.timePickerProps}
103 onChange={this.handleTimeChange}
104 value={this.state.timeValue}
105 />
106 </div>
107 );
108 }
109
110 public componentDidUpdate(prevProps: DatePickerProps) {
111 if (this.props.value === prevProps.value) {
112 return;
113 } else if (this.props.value != null) {
114 this.setState({
115 dateValue: this.props.value,
116 timeValue: this.props.value,
117 });
118 } else {
119 // clear only the date to remove the selected-date style in the calendar
120 this.setState({ dateValue: null });
121 }
122 }
123
124 public handleDateChange = (dateValue: Date, isUserChange: boolean) => {
125 if (this.props.value === undefined) {
126 this.setState({ dateValue });
127 }
128 const value = DateUtils.getDateTime(dateValue, this.state.timeValue);
129 this.props.onChange?.(value, isUserChange);
130 };
131
132 public handleTimeChange = (timeValue: Date) => {
133 if (this.props.value === undefined) {
134 this.setState({ timeValue });
135 }
136 const value = DateUtils.getDateTime(this.state.dateValue, timeValue);
137 this.props.onChange?.(value, true);
138 };
139}