2019-11-06 12:12:41 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
|
|
|
|
import moment from "moment-timezone";
|
2020-04-15 11:42:11 +00:00
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { TimePrecision } from "@blueprintjs/datetime";
|
2020-06-25 10:04:57 +00:00
|
|
|
import { WidgetProps } from "widgets/BaseWidget";
|
2021-02-02 14:42:49 +00:00
|
|
|
import { ISO_DATE_FORMAT } from "constants/WidgetValidation";
|
2021-03-15 12:17:56 +00:00
|
|
|
import DatePickerComponent from "components/ads/DatePickerComponent";
|
2020-04-15 11:42:11 +00:00
|
|
|
|
2020-11-27 08:48:38 +00:00
|
|
|
const DatePickerControlWrapper = styled.div<{ isValid: boolean }>`
|
2020-04-15 11:42:11 +00:00
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
2020-04-29 10:29:02 +00:00
|
|
|
margin: 8px 0 0 0;
|
2022-05-25 10:05:53 +00:00
|
|
|
|
|
|
|
|
&:focus .bp3-input-group input {
|
|
|
|
|
border: 1px solid var(--appsmith-input-focus-border-color);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 10:29:02 +00:00
|
|
|
.vertical-center {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
align-items: center;
|
|
|
|
|
margin: 16px 0 4px 0;
|
|
|
|
|
.label {
|
2020-12-24 04:32:25 +00:00
|
|
|
color: ${(props) => props.theme.colors.paneText};
|
|
|
|
|
font-size: ${(props) => props.theme.fontSizes[3]}px;
|
2020-04-29 10:29:02 +00:00
|
|
|
}
|
|
|
|
|
.bp3-control {
|
|
|
|
|
margin-bottom: 0px;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-15 11:42:11 +00:00
|
|
|
`;
|
|
|
|
|
|
2020-05-07 10:51:37 +00:00
|
|
|
class DatePickerControl extends BaseControl<
|
|
|
|
|
DatePickerControlProps,
|
|
|
|
|
DatePickerControlState
|
|
|
|
|
> {
|
2020-06-19 07:51:07 +00:00
|
|
|
now = moment();
|
|
|
|
|
year = this.now.get("year");
|
|
|
|
|
maxDate: Date = this.now
|
|
|
|
|
.clone()
|
2021-05-07 13:08:43 +00:00
|
|
|
.set({ month: 11, date: 31, year: this.year + 100 })
|
2020-06-19 07:51:07 +00:00
|
|
|
.toDate();
|
|
|
|
|
minDate: Date = this.now
|
|
|
|
|
.clone()
|
2022-01-13 08:48:43 +00:00
|
|
|
.set({ month: 0, date: 1, year: this.year - 150 })
|
2020-06-19 07:51:07 +00:00
|
|
|
.toDate();
|
|
|
|
|
|
2022-05-25 10:05:53 +00:00
|
|
|
private wrapperRef = React.createRef<HTMLInputElement>();
|
|
|
|
|
private inputRef = React.createRef<HTMLInputElement>();
|
|
|
|
|
|
2020-05-07 10:51:37 +00:00
|
|
|
constructor(props: DatePickerControlProps) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
selectedDate: props.propertyValue,
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-06-19 07:51:07 +00:00
|
|
|
|
2022-05-25 10:05:53 +00:00
|
|
|
componentDidMount() {
|
|
|
|
|
window.addEventListener("keydown", this.handleKeydown);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
window.removeEventListener("keydown", this.handleKeydown);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private handleKeydown = (e: KeyboardEvent) => {
|
|
|
|
|
switch (e.key) {
|
|
|
|
|
case "Enter":
|
|
|
|
|
case " ":
|
|
|
|
|
if (document.activeElement === this.wrapperRef?.current) {
|
|
|
|
|
this.inputRef?.current?.focus();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "Escape":
|
|
|
|
|
if (document.activeElement === this.inputRef?.current) {
|
|
|
|
|
this.wrapperRef?.current?.focus();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-06 12:12:41 +00:00
|
|
|
render() {
|
2021-02-23 12:35:09 +00:00
|
|
|
const version = this.props.widgetProperties.version;
|
2021-02-02 14:42:49 +00:00
|
|
|
const dateFormat =
|
2021-02-23 12:35:09 +00:00
|
|
|
version === 2
|
|
|
|
|
? ISO_DATE_FORMAT
|
|
|
|
|
: this.props.widgetProperties.dateFormat || ISO_DATE_FORMAT;
|
2021-02-02 14:42:49 +00:00
|
|
|
const isValid = this.state.selectedDate
|
|
|
|
|
? this.validateDate(moment(this.state.selectedDate, dateFormat).toDate())
|
|
|
|
|
: true;
|
2021-02-23 12:35:09 +00:00
|
|
|
const value =
|
|
|
|
|
this.props.propertyValue && isValid
|
|
|
|
|
? version === 2
|
|
|
|
|
? new Date(this.props.propertyValue)
|
|
|
|
|
: this.parseDate(this.props.propertyValue)
|
|
|
|
|
: null;
|
2022-05-25 10:05:53 +00:00
|
|
|
|
2019-11-06 12:12:41 +00:00
|
|
|
return (
|
2022-05-25 10:05:53 +00:00
|
|
|
<DatePickerControlWrapper isValid ref={this.wrapperRef} tabIndex={0}>
|
2021-03-15 12:17:56 +00:00
|
|
|
<DatePickerComponent
|
2021-04-28 10:28:39 +00:00
|
|
|
closeOnSelection
|
2020-04-15 11:42:11 +00:00
|
|
|
formatDate={this.formatDate}
|
2022-05-25 10:05:53 +00:00
|
|
|
inputRef={this.inputRef}
|
2021-02-25 12:17:13 +00:00
|
|
|
maxDate={this.maxDate}
|
|
|
|
|
minDate={this.minDate}
|
2021-04-28 10:28:39 +00:00
|
|
|
onChange={this.onDateSelected}
|
|
|
|
|
parseDate={this.parseDate}
|
2021-04-23 12:07:45 +00:00
|
|
|
placeholder="YYYY-MM-DD HH:mm"
|
2021-04-28 10:28:39 +00:00
|
|
|
showActionsBar
|
2022-05-25 10:05:53 +00:00
|
|
|
tabIndex={-1}
|
2020-04-23 06:12:13 +00:00
|
|
|
timePrecision={TimePrecision.MINUTE}
|
2021-02-23 12:35:09 +00:00
|
|
|
value={value}
|
2020-04-15 11:42:11 +00:00
|
|
|
/>
|
|
|
|
|
</DatePickerControlWrapper>
|
2019-11-06 12:12:41 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-03 11:30:19 +00:00
|
|
|
getValidDate = (date: string, format: string) => {
|
|
|
|
|
const _date = moment(date, format);
|
|
|
|
|
return _date.isValid() ? _date.toDate() : undefined;
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-27 08:48:38 +00:00
|
|
|
/**
|
|
|
|
|
* here we put the selected state into state
|
|
|
|
|
* before putting it into state, we check if widget date is in range
|
|
|
|
|
* of property value ( min /max range )
|
|
|
|
|
*
|
|
|
|
|
* @param date
|
|
|
|
|
*/
|
2021-07-16 12:26:39 +00:00
|
|
|
onDateSelected = (date: Date | null, isUserChange: boolean): void => {
|
2021-02-02 14:42:49 +00:00
|
|
|
if (isUserChange) {
|
2021-02-23 12:35:09 +00:00
|
|
|
const selectedDate = date
|
|
|
|
|
? this.props.widgetProperties.version === 2
|
|
|
|
|
? date.toISOString()
|
|
|
|
|
: this.formatDate(date)
|
|
|
|
|
: undefined;
|
2021-07-16 12:26:39 +00:00
|
|
|
const isValid = date ? this.validateDate(date) : true;
|
2021-02-02 14:42:49 +00:00
|
|
|
if (!isValid) return;
|
|
|
|
|
// if everything is ok, put date in state
|
|
|
|
|
this.setState({ selectedDate: selectedDate });
|
|
|
|
|
this.updateProperty(this.props.propertyName, selectedDate);
|
|
|
|
|
}
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
2020-11-27 08:48:38 +00:00
|
|
|
/**
|
2021-02-23 12:35:09 +00:00
|
|
|
* checks if date is of valid date format
|
2020-11-27 08:48:38 +00:00
|
|
|
*/
|
|
|
|
|
validateDate = (date: Date): boolean => {
|
2021-02-02 14:42:49 +00:00
|
|
|
const dateFormat =
|
2021-02-23 12:35:09 +00:00
|
|
|
this.props.widgetProperties.version === 2
|
|
|
|
|
? ISO_DATE_FORMAT
|
|
|
|
|
: this.props.widgetProperties.dateFormat || ISO_DATE_FORMAT;
|
|
|
|
|
return date ? moment(date, dateFormat).isValid() : true;
|
2020-11-27 08:48:38 +00:00
|
|
|
};
|
|
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
formatDate = (date: Date): string => {
|
2021-02-02 14:42:49 +00:00
|
|
|
const dateFormat =
|
|
|
|
|
this.props.widgetProperties.dateFormat || ISO_DATE_FORMAT;
|
|
|
|
|
return moment(date).format(dateFormat);
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
2021-07-16 12:26:39 +00:00
|
|
|
parseDate = (dateStr: string): Date | null => {
|
|
|
|
|
if (!dateStr) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
const dateFormat =
|
|
|
|
|
this.props.widgetProperties.version === 2
|
|
|
|
|
? ISO_DATE_FORMAT
|
|
|
|
|
: this.props.widgetProperties.dateFormat || ISO_DATE_FORMAT;
|
|
|
|
|
const date = moment(dateStr, dateFormat);
|
2021-02-03 11:30:19 +00:00
|
|
|
|
2021-07-16 12:26:39 +00:00
|
|
|
if (date.isValid()) return moment(dateStr, dateFormat).toDate();
|
|
|
|
|
else return moment().toDate();
|
|
|
|
|
}
|
2019-11-06 12:12:41 +00:00
|
|
|
};
|
|
|
|
|
|
2020-04-14 05:35:16 +00:00
|
|
|
static getControlType() {
|
2019-11-06 12:12:41 +00:00
|
|
|
return "DATE_PICKER";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface DatePickerControlProps extends ControlProps {
|
|
|
|
|
placeholderText: string;
|
2020-04-23 06:12:13 +00:00
|
|
|
propertyValue: string;
|
2020-06-19 07:51:07 +00:00
|
|
|
widgetProperties: WidgetProps;
|
2019-11-06 12:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-07 10:51:37 +00:00
|
|
|
interface DatePickerControlState {
|
2020-06-09 13:04:47 +00:00
|
|
|
selectedDate?: string;
|
2020-05-07 10:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
2019-11-06 12:12:41 +00:00
|
|
|
export default DatePickerControl;
|