1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import classNames from "classnames";
|
17 | import * as React from "react";
|
18 | import { AbstractPureComponent2, DISPLAYNAME_PREFIX } from "@blueprintjs/core";
|
19 | import * as Classes from "./common/classes";
|
20 | import * as DateUtils from "./common/dateUtils";
|
21 | import { DatePicker } from "./datePicker";
|
22 | import { TimePicker } from "./timePicker";
|
23 |
|
24 | export class DateTimePicker extends AbstractPureComponent2 {
|
25 | static defaultProps = {
|
26 | canClearSelection: true,
|
27 | defaultValue: new Date(),
|
28 | };
|
29 | static displayName = `${DISPLAYNAME_PREFIX}.DateTimePicker`;
|
30 | constructor(props, context) {
|
31 | super(props, context);
|
32 | const initialValue = this.props.value !== undefined ? this.props.value : this.props.defaultValue;
|
33 | this.state = {
|
34 | dateValue: initialValue,
|
35 | timeValue: initialValue,
|
36 | };
|
37 | }
|
38 | render() {
|
39 | const value = DateUtils.getDateTime(this.state.dateValue, this.state.timeValue);
|
40 | return (React.createElement("div", { className: classNames(Classes.DATETIMEPICKER, this.props.className) },
|
41 | React.createElement(DatePicker, { ...this.props.datePickerProps, canClearSelection: this.props.canClearSelection, onChange: this.handleDateChange, value: value }),
|
42 | React.createElement(TimePicker, { ...this.props.timePickerProps, onChange: this.handleTimeChange, value: this.state.timeValue })));
|
43 | }
|
44 | componentDidUpdate(prevProps) {
|
45 | if (this.props.value === prevProps.value) {
|
46 | return;
|
47 | }
|
48 | else if (this.props.value != null) {
|
49 | this.setState({
|
50 | dateValue: this.props.value,
|
51 | timeValue: this.props.value,
|
52 | });
|
53 | }
|
54 | else {
|
55 |
|
56 | this.setState({ dateValue: null });
|
57 | }
|
58 | }
|
59 | handleDateChange = (dateValue, isUserChange) => {
|
60 | if (this.props.value === undefined) {
|
61 | this.setState({ dateValue });
|
62 | }
|
63 | const value = DateUtils.getDateTime(dateValue, this.state.timeValue);
|
64 | this.props.onChange?.(value, isUserChange);
|
65 | };
|
66 | handleTimeChange = (timeValue) => {
|
67 | if (this.props.value === undefined) {
|
68 | this.setState({ timeValue });
|
69 | }
|
70 | const value = DateUtils.getDateTime(this.state.dateValue, timeValue);
|
71 | this.props.onChange?.(value, true);
|
72 | };
|
73 | }
|
74 |
|
\ | No newline at end of file |