2020-01-21 12:48:42 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import styled from "styled-components";
|
2020-01-28 08:21:22 +00:00
|
|
|
import { labelStyle } from "constants/DefaultTheme";
|
2020-01-21 12:48:42 +00:00
|
|
|
import { ControlGroup, Classes, Label } from "@blueprintjs/core";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { ComponentProps } from "components/designSystems/appsmith/BaseComponent";
|
2019-11-06 12:12:41 +00:00
|
|
|
import { DateInput, DateRangeInput } from "@blueprintjs/datetime";
|
|
|
|
|
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";
|
2020-01-21 12:48:42 +00:00
|
|
|
import { WIDGET_PADDING } from "constants/WidgetConstants";
|
2019-11-06 12:12:41 +00:00
|
|
|
|
2020-01-21 12:48:42 +00:00
|
|
|
const StyledControlGroup = styled(ControlGroup)`
|
|
|
|
|
&&& {
|
|
|
|
|
label {
|
2020-01-28 08:21:22 +00:00
|
|
|
${labelStyle}
|
2020-01-21 12:48:42 +00:00
|
|
|
flex: 0 1 30%;
|
|
|
|
|
text-align: right;
|
2020-01-28 08:21:22 +00:00
|
|
|
margin: 0 ${WIDGET_PADDING * 2}px 0 0;
|
2020-01-21 12:48:42 +00:00
|
|
|
align-self: center;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
2019-11-06 12:12:41 +00:00
|
|
|
class DatePickerComponent extends React.Component<DatePickerComponentProps> {
|
|
|
|
|
render() {
|
2020-01-21 12:48:42 +00:00
|
|
|
return (
|
|
|
|
|
<StyledControlGroup fill>
|
2020-01-28 11:46:04 +00:00
|
|
|
{this.props.label && (
|
|
|
|
|
<Label className={Classes.TEXT_OVERFLOW_ELLIPSIS}>
|
|
|
|
|
{this.props.label}
|
|
|
|
|
</Label>
|
|
|
|
|
)}
|
2020-01-21 12:48:42 +00:00
|
|
|
{this.props.datePickerType === "DATE_PICKER" ? (
|
|
|
|
|
<DateInput
|
|
|
|
|
className={this.props.isLoading ? "bp3-skeleton" : ""}
|
|
|
|
|
formatDate={this.formatDate}
|
|
|
|
|
parseDate={this.parseDate}
|
|
|
|
|
placeholder={this.props.dateFormat}
|
|
|
|
|
disabled={this.props.isDisabled}
|
|
|
|
|
showActionsBar={true}
|
|
|
|
|
timePickerProps={
|
|
|
|
|
this.props.enableTimePicker
|
|
|
|
|
? {
|
|
|
|
|
useAmPm: true,
|
|
|
|
|
value: this.props.selectedDate || this.props.defaultDate,
|
|
|
|
|
showArrowButtons: true,
|
|
|
|
|
}
|
|
|
|
|
: undefined
|
|
|
|
|
}
|
|
|
|
|
closeOnSelection={true}
|
|
|
|
|
onChange={this.onDateSelected}
|
|
|
|
|
value={this.props.selectedDate || this.props.defaultDate}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<DateRangeInput
|
|
|
|
|
className={this.props.isLoading ? "bp3-skeleton" : ""}
|
|
|
|
|
allowSingleDayRange={true}
|
|
|
|
|
disabled={this.props.isDisabled}
|
|
|
|
|
contiguousCalendarMonths={false}
|
|
|
|
|
formatDate={this.formatDate}
|
|
|
|
|
minDate={this.props.minDate}
|
|
|
|
|
maxDate={this.props.maxDate}
|
|
|
|
|
closeOnSelection={true}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</StyledControlGroup>
|
2019-11-06 12:12:41 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
formatDate = (date: Date): string => {
|
2019-11-12 07:57:12 +00:00
|
|
|
if (this.props.timezone) {
|
2019-11-06 12:12:41 +00:00
|
|
|
return moment(date)
|
2019-11-12 07:57:12 +00:00
|
|
|
.tz(this.props.timezone)
|
2019-11-06 12:12:41 +00:00
|
|
|
.format(this.props.dateFormat);
|
|
|
|
|
}
|
|
|
|
|
return moment(date).format(this.props.dateFormat);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
parseDate = (dateStr: string): Date => {
|
2019-11-12 07:57:12 +00:00
|
|
|
if (this.props.timezone) {
|
2019-11-06 12:12:41 +00:00
|
|
|
return moment(dateStr)
|
2019-11-12 07:57:12 +00:00
|
|
|
.tz(this.props.timezone)
|
2019-11-06 12:12:41 +00:00
|
|
|
.toDate();
|
|
|
|
|
}
|
|
|
|
|
return moment(dateStr).toDate();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onDateSelected = (selectedDate: Date) => {
|
|
|
|
|
this.props.onDateSelected(selectedDate);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface DatePickerComponentProps extends ComponentProps {
|
|
|
|
|
label: string;
|
|
|
|
|
defaultDate?: Date;
|
|
|
|
|
dateFormat: string;
|
|
|
|
|
enableTimePicker?: boolean;
|
|
|
|
|
selectedDate?: Date;
|
|
|
|
|
minDate?: Date;
|
|
|
|
|
maxDate?: Date;
|
2019-11-12 07:57:12 +00:00
|
|
|
timezone?: string;
|
2019-11-06 12:12:41 +00:00
|
|
|
datePickerType: DatePickerType;
|
|
|
|
|
onDateSelected: (date: Date) => void;
|
2019-12-03 04:41:10 +00:00
|
|
|
isLoading: boolean;
|
2019-11-06 12:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default DatePickerComponent;
|