PromucFlow_constructor/app/client/src/components/designSystems/blueprint/DatePickerComponent.tsx

195 lines
5.6 KiB
TypeScript
Raw Normal View History

import React from "react";
import styled from "styled-components";
import { getBorderCSSShorthand, labelStyle } from "constants/DefaultTheme";
import { ControlGroup, Classes, Label } from "@blueprintjs/core";
2019-11-25 05:07:27 +00:00
import { ComponentProps } from "components/designSystems/appsmith/BaseComponent";
import { DateInput } from "@blueprintjs/datetime";
2019-11-06 12:12:41 +00:00
import moment from "moment-timezone";
import "../../../../node_modules/@blueprintjs/datetime/lib/css/blueprint-datetime.css";
2019-11-25 05:07:27 +00:00
import { DatePickerType } from "widgets/DatePickerWidget";
import { WIDGET_PADDING } from "constants/WidgetConstants";
2020-04-15 11:42:11 +00:00
import { TimePrecision } from "@blueprintjs/datetime";
2020-03-13 07:24:03 +00:00
import { Colors } from "constants/Colors";
import { ISO_DATE_FORMAT } from "constants/WidgetValidation";
2019-11-06 12:12:41 +00:00
const StyledControlGroup = styled(ControlGroup)`
&&& {
2020-03-13 07:24:03 +00:00
.${Classes.INPUT} {
box-shadow: none;
color: ${Colors.OXFORD_BLUE};
font-size: ${(props) => props.theme.fontSizes[3]}px;
border: ${(props) => getBorderCSSShorthand(props.theme.borders[2])};
border-radius: 0;
2020-03-13 07:24:03 +00:00
width: 100%;
height: inherit;
align-items: center;
&:active {
border-color: ${Colors.HIT_GRAY};
}
&:focus {
border: ${(props) => getBorderCSSShorthand(props.theme.borders[2])};
border-color: #80bdff;
outline: 0;
box-shadow: 0 0 0 0.1rem rgba(0, 123, 255, 0.25);
2020-03-13 07:24:03 +00:00
}
}
.${Classes.INPUT_GROUP} {
display: block;
margin: 0;
}
.${Classes.CONTROL_GROUP} {
justify-content: flex-start;
}
label {
2020-01-28 08:21:22 +00:00
${labelStyle}
flex: 0 1 30%;
2020-03-13 07:24:03 +00:00
margin: 7px ${WIDGET_PADDING * 2}px 0 0;
text-align: right;
2020-03-13 07:24:03 +00:00
align-self: flex-start;
max-width: calc(30% - ${WIDGET_PADDING}px);
}
}
`;
2020-03-20 11:03:50 +00:00
2020-04-29 10:29:02 +00:00
class DatePickerComponent extends React.Component<
DatePickerComponentProps,
DatePickerComponentState
> {
constructor(props: DatePickerComponentProps) {
super(props);
this.state = {
selectedDate: props.selectedDate,
};
}
componentDidUpdate(prevProps: DatePickerComponentProps) {
const dateFormat = this.props.dateFormat || ISO_DATE_FORMAT;
2020-04-29 10:29:02 +00:00
if (
2020-05-14 14:00:22 +00:00
this.props.selectedDate !== this.state.selectedDate &&
!moment(this.props.selectedDate, dateFormat).isSame(
moment(prevProps.selectedDate, dateFormat),
2020-04-29 10:29:02 +00:00
"seconds",
)
) {
this.setState({ selectedDate: this.props.selectedDate });
}
}
getValidDate = (date: string, format: string) => {
const _date = moment(date, format);
return _date.isValid() ? _date.toDate() : undefined;
};
2020-04-29 10:29:02 +00:00
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");
const dateFormat = this.props.dateFormat || ISO_DATE_FORMAT;
const minDate = this.props.minDate
? this.getValidDate(this.props.minDate, dateFormat)
: now
.clone()
.set({ month: 0, date: 1, year: year - 100 })
.toDate();
const maxDate = this.props.maxDate
? this.getValidDate(this.props.maxDate, dateFormat)
: now
.clone()
.set({ month: 11, date: 31, year: year + 20 })
.toDate();
return (
2020-04-15 11:42:11 +00:00
<StyledControlGroup
fill
onClick={(e: any) => {
e.stopPropagation();
}}
>
2020-01-28 11:46:04 +00:00
{this.props.label && (
2020-01-31 11:13:16 +00:00
<Label
className={
this.props.isLoading
? Classes.SKELETON
: Classes.TEXT_OVERFLOW_ELLIPSIS
}
>
2020-01-28 11:46:04 +00:00
{this.props.label}
</Label>
)}
{
<DateInput
className={this.props.isLoading ? "bp3-skeleton" : ""}
formatDate={this.formatDate}
parseDate={this.parseDate}
2020-06-09 08:44:13 +00:00
placeholder={"Select Date"}
disabled={this.props.isDisabled}
showActionsBar={true}
2020-04-23 06:12:13 +00:00
timePrecision={TimePrecision.MINUTE}
2020-04-15 11:42:11 +00:00
closeOnSelection
onChange={this.onDateSelected}
2020-04-29 10:29:02 +00:00
value={
this.state.selectedDate
2020-06-09 13:04:47 +00:00
? this.parseDate(this.state.selectedDate)
2020-04-29 10:29:02 +00:00
: null
}
minDate={minDate}
maxDate={maxDate}
/>
}
</StyledControlGroup>
2019-11-06 12:12:41 +00:00
);
}
formatDate = (date: Date): string => {
const dateFormat = this.props.dateFormat || ISO_DATE_FORMAT;
return moment(date).format(dateFormat);
2019-11-06 12:12:41 +00:00
};
parseDate = (dateStr: string): Date => {
const dateFormat = this.props.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
};
/**
* checks if selelectedDate is null or not,
* sets state and calls props onDateSelected
* if its null, don't call onDateSelected
*
* @param selectedDate
*/
onDateSelected = (selectedDate: Date, isUserChange: boolean) => {
if (isUserChange) {
const { onDateSelected } = this.props;
const date = selectedDate ? this.formatDate(selectedDate) : "";
this.setState({ selectedDate: date });
// if date is null ( if date is cleared ), don't call onDateSelected
if (!selectedDate) return false;
onDateSelected(date);
}
2019-11-06 12:12:41 +00:00
};
}
2020-04-29 10:29:02 +00:00
interface DatePickerComponentProps extends ComponentProps {
2019-11-06 12:12:41 +00:00
label: string;
dateFormat: string;
enableTimePicker?: boolean;
2020-06-09 13:04:47 +00:00
selectedDate?: string;
minDate?: string;
maxDate?: string;
2019-11-12 07:57:12 +00:00
timezone?: string;
2019-11-06 12:12:41 +00:00
datePickerType: DatePickerType;
2020-03-20 11:03:50 +00:00
isDisabled: boolean;
2020-04-29 10:29:02 +00:00
onDateSelected: (selectedDate: string) => void;
2019-12-03 04:41:10 +00:00
isLoading: boolean;
2019-11-06 12:12:41 +00:00
}
2020-04-29 10:29:02 +00:00
interface DatePickerComponentState {
2020-06-09 13:04:47 +00:00
selectedDate?: string;
2020-04-29 10:29:02 +00:00
}
2019-11-06 12:12:41 +00:00
export default DatePickerComponent;