PromucFlow_constructor/app/client/src/widgets/DatePickerWidget.tsx

96 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-09-12 08:11:25 +00:00
import React from "react";
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";
2019-09-12 08:11:25 +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
defaultDate: VALIDATION_TYPES.DATE,
selectedDate: VALIDATION_TYPES.DATE,
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,
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,
};
}
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}
2019-11-12 07:57:12 +00:00
timezone={this.props.timezone}
2019-11-06 12:12:41 +00:00
enableTimePicker={this.props.enableTimePicker}
defaultDate={this.props.defaultDate}
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";
}
}
export type DatePickerType = "DATE_PICKER" | "DATE_RANGE_PICKER";
2019-09-12 08:11:25 +00:00
export interface DatePickerWidgetProps extends WidgetProps {
defaultDate?: Date;
2019-11-06 12:12:41 +00:00
selectedDate: Date;
2019-11-12 07:57:12 +00:00
timezone?: string;
2019-11-06 12:12:41 +00:00
enableTimePicker: boolean;
dateFormat: string;
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;