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

136 lines
3.9 KiB
TypeScript
Raw Normal View History

import React from "react";
import styled from "styled-components";
2020-01-28 08:21:22 +00:00
import { 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";
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";
import { WIDGET_PADDING } from "constants/WidgetConstants";
2020-03-13 07:24:03 +00:00
import { Colors } from "constants/Colors";
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;
border: 1px solid;
border-color: ${Colors.GEYSER_LIGHT};
border-radius: ${props => props.theme.radii[1]}px;
width: 100%;
height: inherit;
align-items: center;
&:active {
border-color: ${Colors.HIT_GRAY};
}
&:focus {
border-color: ${Colors.MYSTIC};
}
}
.${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);
}
}
`;
2019-11-06 12:12:41 +00:00
class DatePickerComponent extends React.Component<DatePickerComponentProps> {
render() {
return (
<StyledControlGroup fill>
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>
)}
{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,
2020-03-16 15:42:39 +00:00
value: this.props.selectedDate,
showArrowButtons: true,
}
: undefined
}
closeOnSelection={true}
onChange={this.onDateSelected}
2020-03-16 15:42:39 +00:00
value={this.props.selectedDate}
/>
) : (
<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;
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;