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

398 lines
11 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";
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
2019-11-25 05:07:27 +00:00
import DatePickerComponent from "components/designSystems/blueprint/DatePickerComponent";
import {
ISO_DATE_FORMAT,
ValidationResponse,
ValidationTypes,
} from "constants/WidgetValidation";
import { DerivedPropertiesMap } from "utils/WidgetFactory";
import * as Sentry from "@sentry/react";
2020-10-06 09:01:51 +00:00
import withMeta, { WithMeta } from "./MetaHOC";
import moment from "moment";
import { AutocompleteDataType } from "utils/autocomplete/TernServer";
2019-09-12 08:11:25 +00:00
function defaultDateValidation(
value: unknown,
props: DatePickerWidgetProps,
_?: any,
moment?: any,
): ValidationResponse {
const dateFormat = props.dateFormat || ISO_DATE_FORMAT;
if (value === null) {
return {
isValid: true,
parsed: "",
message: "",
};
}
if (value === undefined) {
return {
isValid: false,
parsed: "",
message: `This value does not evaluate to type: Date ${dateFormat}`,
};
}
const isValid = moment(value as string, dateFormat).isValid();
return {
isValid,
parsed: isValid ? value : "",
message:
isValid === false
? `Value does not match ISO 8601 standard date string`
: "",
};
}
function minDateValidation(
value: unknown,
props: DatePickerWidgetProps,
_?: any,
moment?: any,
): ValidationResponse {
const dateFormat = props.dateFormat || ISO_DATE_FORMAT;
if (value === undefined) {
return {
isValid: false,
parsed: "",
message:
`Value does not match: Date String ` + (dateFormat ? dateFormat : ""),
};
}
const parsedMinDate = moment(value as string, dateFormat);
let isValid = parsedMinDate.isValid();
if (!props.defaultDate) {
return {
isValid: isValid,
parsed: value,
message: "",
};
}
const parsedDefaultDate = moment(props.defaultDate, dateFormat);
if (
isValid &&
parsedDefaultDate.isValid() &&
parsedDefaultDate.isBefore(parsedMinDate)
) {
isValid = false;
}
if (!isValid) {
return {
isValid: isValid,
parsed: "",
message:
`Value does not match: Date String ` + (dateFormat ? dateFormat : ""),
};
}
return {
isValid: isValid,
parsed: value,
message: "",
};
}
function maxDateValidation(
value: unknown,
props: DatePickerWidgetProps,
_?: any,
moment?: any,
): ValidationResponse {
const dateFormat = props.dateFormat || ISO_DATE_FORMAT;
if (value === undefined) {
return {
isValid: false,
parsed: "",
message:
`Value does not match type: Date String ` +
(dateFormat ? dateFormat : ""),
};
}
const parsedMaxDate = moment(value as string, dateFormat);
let isValid = parsedMaxDate.isValid();
if (!props.defaultDate) {
return {
isValid: isValid,
parsed: value,
message: "",
};
}
const parsedDefaultDate = moment(props.defaultDate, dateFormat);
if (
isValid &&
parsedDefaultDate.isValid() &&
parsedDefaultDate.isAfter(parsedMaxDate)
) {
isValid = false;
}
if (!isValid) {
return {
isValid: isValid,
parsed: "",
message:
`Value does not match type: Date String ` +
(dateFormat ? dateFormat : ""),
};
}
return {
isValid: isValid,
parsed: value,
message: "",
};
}
class DatePickerWidget extends BaseWidget<DatePickerWidgetProps, WidgetState> {
static getPropertyPaneConfig() {
return [
{
sectionName: "General",
children: [
{
propertyName: "defaultDate",
label: "Default Date",
helpText:
"Sets the default date of the widget. The date is updated if the default date changes",
controlType: "DATE_PICKER",
placeholderText: "Enter Default Date",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: {
type: ValidationTypes.FUNCTION,
params: {
fn: defaultDateValidation,
expected: {
type: "ISO 8601 string",
example: moment().toISOString(),
autocompleteDataType: AutocompleteDataType.STRING,
},
},
},
dependencies: ["dateFormat"],
},
{
helpText: "Sets the format of the selected date",
propertyName: "dateFormat",
label: "Date Format",
controlType: "DROP_DOWN",
isJSConvertible: true,
options: [
{
label: "YYYY-MM-DD",
value: "YYYY-MM-DD",
},
{
label: "YYYY-MM-DD HH:mm",
value: "YYYY-MM-DD HH:mm",
},
{
label: "YYYY-MM-DDTHH:mm:ss.sssZ",
value: "YYYY-MM-DDTHH:mm:ss.sssZ",
},
{
label: "DD/MM/YYYY",
value: "DD/MM/YYYY",
},
{
label: "DD/MM/YYYY HH:mm",
value: "DD/MM/YYYY HH:mm",
},
],
isBindProperty: true,
isTriggerProperty: false,
dateFormat: { type: ValidationTypes.TEXT },
},
{
propertyName: "isRequired",
label: "Required",
helpText: "Makes input to the widget mandatory",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
{
propertyName: "isVisible",
label: "Visible",
helpText: "Controls the visibility of the widget",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
{
propertyName: "isDisabled",
label: "Disabled",
helpText: "Disables input to this widget",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
{
propertyName: "minDate",
label: "Min Date",
helpText: "Defines the min date for this widget",
controlType: "DATE_PICKER",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: {
type: ValidationTypes.FUNCTION,
params: {
fn: minDateValidation,
expected: {
type: "ISO 8601 string",
example: moment().toISOString(),
autocompleteDataType: AutocompleteDataType.STRING,
},
},
},
dependencies: ["dateFormat", "defaultDate"],
},
{
propertyName: "maxDate",
label: "Max Date",
helpText: "Defines the max date for this widget",
controlType: "DATE_PICKER",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: {
type: ValidationTypes.FUNCTION,
params: {
fn: maxDateValidation,
expected: {
type: "ISO 8601 string",
example: moment().toISOString(),
autocompleteDataType: AutocompleteDataType.STRING,
},
},
},
dependencies: ["dateFormat", "defaultDate"],
},
],
},
{
sectionName: "Actions",
children: [
{
propertyName: "onDateSelected",
label: "onDateSelected",
controlType: "ACTION_SELECTOR",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: true,
},
],
},
];
}
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-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
}
componentDidUpdate(prevProps: DatePickerWidgetProps) {
if (this.props.dateFormat !== prevProps.dateFormat) {
if (this.props.defaultDate) {
const defaultDate = moment(
this.props.defaultDate,
this.props.dateFormat,
);
if (!defaultDate.isValid()) {
super.updateWidgetProperty("defaultDate", "");
} else {
if (this.props.minDate) {
const minDate = moment(this.props.minDate, this.props.dateFormat);
if (!minDate.isValid() || defaultDate.isBefore(minDate)) {
super.updateWidgetProperty("defaultDate", "");
}
}
if (this.props.maxDate) {
const maxDate = moment(this.props.maxDate, this.props.dateFormat);
if (!maxDate.isValid() || defaultDate.isAfter(maxDate)) {
super.updateWidgetProperty("defaultDate", "");
}
}
}
}
}
}
2019-09-12 08:11:25 +00:00
getPageView() {
2019-11-06 12:12:41 +00:00
return (
<DatePickerComponent
2020-06-09 08:44:13 +00:00
dateFormat={this.props.dateFormat}
2019-11-06 12:12:41 +00:00
datePickerType={"DATE_PICKER"}
isDisabled={this.props.isDisabled}
2019-12-03 04:41:10 +00:00
isLoading={this.props.isLoading}
label={`${this.props.label}`}
maxDate={this.props.maxDate}
minDate={this.props.minDate}
onDateSelected={this.onDateSelected}
selectedDate={this.props.selectedDate}
widgetId={this.props.widgetId}
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 16:47:16 +00:00
this.props.updateWidgetMetaProperty("selectedDate", selectedDate, {
2021-04-23 13:50:55 +00:00
triggerPropertyName: "onDateSelected",
2020-10-06 16:47:16 +00:00
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;
maxDate: string;
minDate: string;
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),
);