PromucFlow_constructor/app/client/src/components/propertyControls/DatePickerControl.tsx

165 lines
4.6 KiB
TypeScript
Raw Normal View History

2019-11-06 12:12:41 +00:00
import React from "react";
import BaseControl, { ControlProps } from "./BaseControl";
2020-04-23 06:12:13 +00:00
import { StyledDatePicker } from "./StyledControls";
2019-11-06 12:12:41 +00:00
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";
import { ISO_DATE_FORMAT } from "constants/WidgetValidation";
2020-04-15 11:42:11 +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;
2020-04-15 11:42:11 +00:00
&&& {
input {
2020-12-24 04:32:25 +00:00
background: ${(props) => props.theme.colors.paneTextBG};
color: ${(props) => props.theme.colors.textOnDarkBG};
font-size: ${(props) => props.theme.fontSizes[3]}px;
2020-04-15 11:42:11 +00:00
box-shadow: none;
2020-12-24 04:32:25 +00:00
border: ${(props) =>
!props.isValid
? `1px solid ${props.theme.colors.error}`
: `1px solid transparent`};
2020-04-15 11:42:11 +00:00
}
}
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
> {
now = moment();
year = this.now.get("year");
maxDate: Date = this.now
.clone()
.set({ month: 11, date: 31, year: this.year + 20 })
.toDate();
minDate: Date = this.now
.clone()
.set({ month: 0, date: 1, year: this.year - 20 })
.toDate();
2020-05-07 10:51:37 +00:00
constructor(props: DatePickerControlProps) {
super(props);
this.state = {
selectedDate: props.propertyValue,
};
}
2019-11-06 12:12:41 +00:00
render() {
const version = this.props.widgetProperties.version;
const dateFormat =
version === 2
? ISO_DATE_FORMAT
: this.props.widgetProperties.dateFormat || ISO_DATE_FORMAT;
const isValid = this.state.selectedDate
? this.validateDate(moment(this.state.selectedDate, dateFormat).toDate())
: true;
const value =
this.props.propertyValue && isValid
? version === 2
? new Date(this.props.propertyValue)
: this.parseDate(this.props.propertyValue)
: null;
2019-11-06 12:12:41 +00:00
return (
<DatePickerControlWrapper isValid={true}>
2020-04-15 11:42:11 +00:00
<StyledDatePicker
formatDate={this.formatDate}
parseDate={this.parseDate}
2020-04-23 06:12:13 +00:00
placeholder="DD/MM/YYYY HH:mm"
2020-04-15 11:42:11 +00:00
showActionsBar
2020-04-23 06:12:13 +00:00
timePrecision={TimePrecision.MINUTE}
2020-04-15 11:42:11 +00:00
closeOnSelection
onChange={this.onDateSelected}
value={value}
2020-04-15 11:42:11 +00:00
/>
</DatePickerControlWrapper>
2019-11-06 12:12:41 +00:00
);
}
getValidDate = (date: string, format: string) => {
const _date = moment(date, format);
return _date.isValid() ? _date.toDate() : undefined;
};
/**
* 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
*/
onDateSelected = (date: Date, isUserChange: boolean): void => {
if (isUserChange) {
const selectedDate = date
? this.props.widgetProperties.version === 2
? date.toISOString()
: this.formatDate(date)
: undefined;
const isValid = this.validateDate(date);
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
};
/**
* checks if date is of valid date format
*/
validateDate = (date: Date): boolean => {
const dateFormat =
this.props.widgetProperties.version === 2
? ISO_DATE_FORMAT
: this.props.widgetProperties.dateFormat || ISO_DATE_FORMAT;
return date ? moment(date, dateFormat).isValid() : true;
};
2020-04-15 11:42:11 +00:00
formatDate = (date: Date): string => {
const dateFormat =
this.props.widgetProperties.dateFormat || ISO_DATE_FORMAT;
return moment(date).format(dateFormat);
2020-04-15 11:42:11 +00:00
};
parseDate = (dateStr: string): Date => {
const dateFormat =
this.props.widgetProperties.version === 2
? ISO_DATE_FORMAT
: this.props.widgetProperties.dateFormat || ISO_DATE_FORMAT;
const date = moment(dateStr, dateFormat);
if (date.isValid()) return moment(dateStr, dateFormat).toDate();
else return moment().toDate();
2019-11-06 12:12:41 +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;
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;