2019-09-12 08:11:25 +00:00
|
|
|
import React from "react";
|
2019-09-13 10:45:49 +00:00
|
|
|
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { WidgetType } from "constants/WidgetConstants";
|
2020-02-18 10:41:52 +00:00
|
|
|
import { EventType } from "constants/ActionConstants";
|
2019-11-25 05:07:27 +00:00
|
|
|
import DatePickerComponent from "components/designSystems/blueprint/DatePickerComponent";
|
2020-03-16 07:59:07 +00:00
|
|
|
import {
|
|
|
|
|
WidgetPropertyValidationType,
|
|
|
|
|
BASE_WIDGET_VALIDATION,
|
|
|
|
|
} from "utils/ValidationFactory";
|
2019-11-22 13:12:39 +00:00
|
|
|
import { VALIDATION_TYPES } from "constants/WidgetValidation";
|
2020-03-06 09:45:21 +00:00
|
|
|
import {
|
|
|
|
|
DerivedPropertiesMap,
|
|
|
|
|
TriggerPropertiesMap,
|
|
|
|
|
} from "utils/WidgetFactory";
|
2020-04-15 11:42:11 +00:00
|
|
|
import moment from "moment-timezone";
|
2019-09-12 08:11:25 +00:00
|
|
|
|
2019-09-13 10:45:49 +00:00
|
|
|
class DatePickerWidget extends BaseWidget<DatePickerWidgetProps, WidgetState> {
|
2019-11-22 13:12:39 +00:00
|
|
|
static getPropertyValidationMap(): WidgetPropertyValidationType {
|
|
|
|
|
return {
|
2020-03-16 07:59:07 +00:00
|
|
|
...BASE_WIDGET_VALIDATION,
|
2019-11-22 13:12:39 +00:00
|
|
|
timezone: VALIDATION_TYPES.TEXT,
|
|
|
|
|
enableTimePicker: VALIDATION_TYPES.BOOLEAN,
|
|
|
|
|
dateFormat: VALIDATION_TYPES.TEXT,
|
|
|
|
|
label: VALIDATION_TYPES.TEXT,
|
|
|
|
|
datePickerType: VALIDATION_TYPES.TEXT,
|
|
|
|
|
maxDate: VALIDATION_TYPES.DATE,
|
|
|
|
|
minDate: VALIDATION_TYPES.DATE,
|
2020-03-06 09:45:21 +00:00
|
|
|
isRequired: VALIDATION_TYPES.BOOLEAN,
|
2020-03-31 10:40:52 +00:00
|
|
|
onDateSelected: VALIDATION_TYPES.ACTION_SELECTOR,
|
2020-04-02 07:53:03 +00:00
|
|
|
onDateRangeSelected: VALIDATION_TYPES.ACTION_SELECTOR,
|
2019-11-22 13:12:39 +00:00
|
|
|
};
|
|
|
|
|
}
|
2020-03-06 09:45:21 +00:00
|
|
|
|
|
|
|
|
static getDerivedPropertiesMap(): DerivedPropertiesMap {
|
|
|
|
|
return {
|
|
|
|
|
isValid: `{{ this.isRequired ? !!this.selectedDate : true }}`,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 10:41:52 +00:00
|
|
|
static getTriggerPropertyMap(): TriggerPropertiesMap {
|
|
|
|
|
return {
|
|
|
|
|
onDateSelected: true,
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-03-16 15:42:39 +00:00
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
componentDidMount() {
|
|
|
|
|
super.componentDidMount();
|
|
|
|
|
if (this.props.defaultDate) {
|
|
|
|
|
this.updateWidgetMetaProperty(
|
|
|
|
|
"selectedDate",
|
|
|
|
|
new Date(this.props.defaultDate.dateValue),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 15:42:39 +00:00
|
|
|
componentDidUpdate(prevProps: DatePickerWidgetProps) {
|
|
|
|
|
super.componentDidUpdate(prevProps);
|
2020-04-15 11:42:11 +00:00
|
|
|
if (
|
|
|
|
|
this.props.defaultDate &&
|
|
|
|
|
(prevProps.defaultDate === undefined ||
|
|
|
|
|
!moment(this.props.defaultDate.dateValue).isSame(
|
|
|
|
|
moment(prevProps.defaultDate.dateValue),
|
|
|
|
|
"second",
|
|
|
|
|
))
|
|
|
|
|
) {
|
|
|
|
|
this.updateWidgetMetaProperty(
|
|
|
|
|
"selectedDate",
|
|
|
|
|
new Date(this.props.defaultDate.dateValue),
|
|
|
|
|
);
|
2020-03-16 15:42:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 08:11:25 +00:00
|
|
|
getPageView() {
|
2019-11-06 12:12:41 +00:00
|
|
|
return (
|
|
|
|
|
<DatePickerComponent
|
2020-03-06 09:45:21 +00:00
|
|
|
label={`${this.props.label}${this.props.isRequired ? " *" : ""}`}
|
2019-11-06 12:12:41 +00:00
|
|
|
dateFormat={this.props.dateFormat}
|
|
|
|
|
widgetId={this.props.widgetId}
|
2020-04-15 11:42:11 +00:00
|
|
|
timezone={
|
|
|
|
|
this.props.defaultDate && this.props.defaultDate.timezone
|
|
|
|
|
? this.props.defaultDate.timezone
|
|
|
|
|
: ""
|
|
|
|
|
}
|
|
|
|
|
enableTimePicker={
|
|
|
|
|
this.props.defaultDate && this.props.defaultDate.isTimeEnabled
|
|
|
|
|
? this.props.defaultDate.isTimeEnabled
|
|
|
|
|
: false
|
|
|
|
|
}
|
2020-03-20 11:03:50 +00:00
|
|
|
isDisabled={this.props.isDisabled}
|
2019-11-06 12:12:41 +00:00
|
|
|
datePickerType={"DATE_PICKER"}
|
|
|
|
|
onDateSelected={this.onDateSelected}
|
|
|
|
|
selectedDate={this.props.selectedDate}
|
2019-12-03 04:41:10 +00:00
|
|
|
isLoading={this.props.isLoading}
|
2019-11-06 12:12:41 +00:00
|
|
|
/>
|
|
|
|
|
);
|
2019-09-12 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
2019-11-06 12:12:41 +00:00
|
|
|
onDateSelected = (selectedDate: Date) => {
|
2020-03-06 09:45:21 +00:00
|
|
|
this.updateWidgetMetaProperty("selectedDate", selectedDate);
|
2020-02-18 10:41:52 +00:00
|
|
|
if (this.props.onDateSelected) {
|
|
|
|
|
super.executeAction({
|
|
|
|
|
dynamicString: this.props.onDateSelected,
|
|
|
|
|
event: {
|
|
|
|
|
type: EventType.ON_DATE_SELECTED,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-11-06 12:12:41 +00:00
|
|
|
};
|
|
|
|
|
|
2019-09-12 08:11:25 +00:00
|
|
|
getWidgetType(): WidgetType {
|
|
|
|
|
return "DATE_PICKER_WIDGET";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 10:45:49 +00:00
|
|
|
export type DatePickerType = "DATE_PICKER" | "DATE_RANGE_PICKER";
|
2019-09-12 08:11:25 +00:00
|
|
|
|
2019-09-13 10:45:49 +00:00
|
|
|
export interface DatePickerWidgetProps extends WidgetProps {
|
2020-04-15 11:42:11 +00:00
|
|
|
defaultDate: {
|
|
|
|
|
dateValue: number;
|
|
|
|
|
isTimeEnabled: boolean;
|
|
|
|
|
timezone: string;
|
|
|
|
|
};
|
2019-11-06 12:12:41 +00:00
|
|
|
selectedDate: Date;
|
2020-03-20 11:03:50 +00:00
|
|
|
isDisabled: boolean;
|
2019-11-06 12:12:41 +00:00
|
|
|
dateFormat: string;
|
2019-09-13 10:45:49 +00:00
|
|
|
label: string;
|
|
|
|
|
datePickerType: DatePickerType;
|
2020-02-18 10:41:52 +00:00
|
|
|
onDateSelected?: string;
|
|
|
|
|
onDateRangeSelected?: string;
|
2019-11-06 12:12:41 +00:00
|
|
|
maxDate: Date;
|
|
|
|
|
minDate: Date;
|
2020-03-06 09:45:21 +00:00
|
|
|
isRequired?: boolean;
|
2019-09-12 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default DatePickerWidget;
|