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

103 lines
2.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";
const DatePickerControlWrapper = styled.div`
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 {
background: ${props => props.theme.colors.paneTextBG};
color: ${props => props.theme.colors.textOnDarkBG};
font-size: ${props => props.theme.fontSizes[3]}px;
box-shadow: none;
}
}
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 {
color: ${props => props.theme.colors.paneText};
font-size: ${props => props.theme.fontSizes[3]}px;
}
.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
> {
constructor(props: DatePickerControlProps) {
super(props);
this.state = {
selectedDate: props.propertyValue,
};
}
2019-11-06 12:12:41 +00:00
render() {
2020-04-15 11:42:11 +00:00
const now = moment();
const year = now.get("year");
2020-04-29 10:29:02 +00:00
const minDate = now.clone().set({ month: 0, date: 1, year: year - 20 });
const maxDate = now.clone().set({ month: 11, date: 31, year: year + 20 });
2019-11-06 12:12:41 +00:00
return (
2020-04-15 11:42:11 +00:00
<DatePickerControlWrapper>
<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}
maxDate={maxDate.toDate()}
minDate={minDate.toDate()}
2020-04-29 10:29:02 +00:00
value={
this.props.propertyValue
? moment(this.props.propertyValue).toDate()
: null
}
2020-04-15 11:42:11 +00:00
/>
</DatePickerControlWrapper>
2019-11-06 12:12:41 +00:00
);
}
onDateSelected = (date: Date): void => {
2020-05-07 10:51:37 +00:00
this.setState({ selectedDate: moment(date).toISOString(true) });
2020-04-23 06:12:13 +00:00
this.updateProperty(
this.props.propertyName,
moment(date).toISOString(true),
);
2020-04-15 11:42:11 +00:00
};
formatDate = (date: Date): string => {
2020-04-23 06:12:13 +00:00
return moment(date).format("DD/MM/YYYY HH:mm");
2020-04-15 11:42:11 +00:00
};
parseDate = (dateStr: string): Date => {
2020-04-29 10:29:02 +00:00
return moment(dateStr, "DD/MM/YYYY HH:mm").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;
2019-11-06 12:12:41 +00:00
}
2020-05-07 10:51:37 +00:00
interface DatePickerControlState {
selectedDate: string;
}
2019-11-06 12:12:41 +00:00
export default DatePickerControl;