1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import classNames from "classnames";
|
18 | import * as React from "react";
|
19 |
|
20 | import { AbstractPureComponent2, DISPLAYNAME_PREFIX, Props } from "@blueprintjs/core";
|
21 |
|
22 | import * as Classes from "./common/classes";
|
23 | import * as DateUtils from "./common/dateUtils";
|
24 | import { DatePicker, DatePickerProps } from "./datePicker";
|
25 | import { TimePicker, TimePickerProps } from "./timePicker";
|
26 |
|
27 | export interface IDateTimePickerProps extends Props {
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | defaultValue?: Date;
|
35 |
|
36 | |
37 |
|
38 |
|
39 |
|
40 | datePickerProps?: DatePickerProps;
|
41 |
|
42 | |
43 |
|
44 |
|
45 | onChange?: (selectedDate: Date, isUserChange: boolean) => void;
|
46 |
|
47 | |
48 |
|
49 |
|
50 |
|
51 | timePickerProps?: TimePickerProps;
|
52 |
|
53 | |
54 |
|
55 |
|
56 | value?: Date | null;
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 | canClearSelection?: boolean;
|
64 | }
|
65 |
|
66 |
|
67 | export interface IDateTimePickerState {
|
68 | dateValue?: Date;
|
69 | timeValue?: Date;
|
70 | }
|
71 |
|
72 |
|
73 | export 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 |
|
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 | }
|