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

113 lines
3.1 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";
import * as Sentry from "@sentry/react";
2020-10-06 09:01:51 +00:00
import withMeta, { WithMeta } from "./MetaHOC";
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,
2020-04-17 16:15:09 +00:00
defaultDate: VALIDATION_TYPES.DATE,
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,
// onDateSelected: VALIDATION_TYPES.ACTION_SELECTOR,
// 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 }}`,
value: `{{ this.selectedDate }}`,
2020-03-06 09:45:21 +00:00
};
}
2020-02-18 10:41:52 +00:00
static getTriggerPropertyMap(): TriggerPropertiesMap {
return {
onDateSelected: true,
};
}
2020-03-16 15:42:39 +00:00
2020-04-17 16:15:09 +00:00
static getDefaultPropertiesMap(): Record<string, string> {
return {
selectedDate: "defaultDate",
};
2020-04-15 11:42:11 +00:00
}
2020-04-17 16:15:09 +00:00
static getMetaPropertiesMap(): Record<string, any> {
return {
selectedDate: undefined,
};
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-05-25 09:32:01 +00:00
label={`${this.props.label}`}
2020-06-09 08:44:13 +00:00
dateFormat={this.props.dateFormat}
2019-11-06 12:12:41 +00:00
widgetId={this.props.widgetId}
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}
2020-04-29 10:29:02 +00:00
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
}
2020-04-29 10:29:02 +00:00
onDateSelected = (selectedDate: string) => {
2020-10-06 09:01:51 +00:00
this.props.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
2020-10-06 09:01:51 +00:00
export interface DatePickerWidgetProps extends WidgetProps, WithMeta {
2020-04-23 06:12:13 +00:00
defaultDate: string;
selectedDate: string;
2020-03-20 11:03:50 +00:00
isDisabled: boolean;
2019-11-06 12:12:41 +00:00
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;
2020-10-06 09:01:51 +00:00
export const ProfiledDatePickerWidget = Sentry.withProfiler(
withMeta(DatePickerWidget),
);