changed date picker to store formatted date

This commit is contained in:
Nikhil Nandagopal 2020-06-19 13:21:07 +05:30
parent 0ea0d5f3d1
commit fa71e7c4ef
3 changed files with 33 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import { StyledDatePicker } from "./StyledControls";
import moment from "moment-timezone"; import moment from "moment-timezone";
import styled from "styled-components"; import styled from "styled-components";
import { TimePrecision } from "@blueprintjs/datetime"; import { TimePrecision } from "@blueprintjs/datetime";
import BaseWidget, { WidgetProps } from "widgets/BaseWidget";
const DatePickerControlWrapper = styled.div` const DatePickerControlWrapper = styled.div`
display: flex; display: flex;
@ -36,17 +37,25 @@ class DatePickerControl extends BaseControl<
DatePickerControlProps, DatePickerControlProps,
DatePickerControlState 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();
constructor(props: DatePickerControlProps) { constructor(props: DatePickerControlProps) {
super(props); super(props);
this.state = { this.state = {
selectedDate: props.propertyValue, selectedDate: props.propertyValue,
}; };
} }
render() { render() {
const now = moment();
const year = now.get("year");
const minDate = now.clone().set({ month: 0, date: 1, year: year - 20 });
const maxDate = now.clone().set({ month: 11, date: 31, year: year + 20 });
return ( return (
<DatePickerControlWrapper> <DatePickerControlWrapper>
<StyledDatePicker <StyledDatePicker
@ -57,11 +66,11 @@ class DatePickerControl extends BaseControl<
timePrecision={TimePrecision.MINUTE} timePrecision={TimePrecision.MINUTE}
closeOnSelection closeOnSelection
onChange={this.onDateSelected} onChange={this.onDateSelected}
maxDate={maxDate.toDate()} maxDate={this.maxDate}
minDate={minDate.toDate()} minDate={this.minDate}
value={ value={
this.props.propertyValue this.props.propertyValue
? moment(this.props.propertyValue).toDate() ? this.parseDate(this.props.propertyValue)
: null : null
} }
/> />
@ -70,17 +79,22 @@ class DatePickerControl extends BaseControl<
} }
onDateSelected = (date: Date): void => { onDateSelected = (date: Date): void => {
const selectedDate = date ? moment(date).toISOString(true) : undefined; const selectedDate = date ? this.formatDate(date) : undefined;
this.setState({ selectedDate: selectedDate }); this.setState({ selectedDate: selectedDate });
this.updateProperty(this.props.propertyName, selectedDate); this.updateProperty(this.props.propertyName, selectedDate);
}; };
formatDate = (date: Date): string => { formatDate = (date: Date): string => {
return moment(date).format("DD/MM/YYYY HH:mm"); return moment(date).format(
this.props.widgetProperties.dateFormat || "DD/MM/YYYY HH:mm",
);
}; };
parseDate = (dateStr: string): Date => { parseDate = (dateStr: string): Date => {
return moment(dateStr, "DD/MM/YYYY HH:mm").toDate(); return moment(
dateStr,
this.props.widgetProperties.dateFormat || "DD/MM/YYYY HH:mm",
).toDate();
}; };
static getControlType() { static getControlType() {
@ -91,6 +105,7 @@ class DatePickerControl extends BaseControl<
export interface DatePickerControlProps extends ControlProps { export interface DatePickerControlProps extends ControlProps {
placeholderText: string; placeholderText: string;
propertyValue: string; propertyValue: string;
widgetProperties: WidgetProps;
} }
interface DatePickerControlState { interface DatePickerControlState {

View File

@ -61,6 +61,7 @@ const PropertyControl = (props: Props) => {
validationMessage, validationMessage,
dataTreePath, dataTreePath,
evaluatedValue, evaluatedValue,
widgetProperties: widgetProperties,
expected: FIELD_EXPECTED_VALUE[widgetProperties.type][propertyName], expected: FIELD_EXPECTED_VALUE[widgetProperties.type][propertyName],
}; };
const isDynamic: boolean = _.get( const isDynamic: boolean = _.get(

View File

@ -387,7 +387,7 @@ export const VALIDATORS: Record<ValidationType, Validator> = {
return { isValid, parsed }; return { isValid, parsed };
}, },
[VALIDATION_TYPES.DATE]: ( [VALIDATION_TYPES.DATE]: (
value: string, dateString: string,
props: WidgetProps, props: WidgetProps,
dataTree?: DataTree, dataTree?: DataTree,
): ValidationResponse => { ): ValidationResponse => {
@ -396,8 +396,10 @@ export const VALIDATORS: Record<ValidationType, Validator> = {
.minute(0) .minute(0)
.second(0) .second(0)
.millisecond(0); .millisecond(0);
const dateFormat = props.dateFormat ? props.dateFormat : moment.ISO_8601;
if (value === undefined) { const dateStr = moment().toISOString();
const todayDateString = today.format(dateFormat);
if (dateString === undefined) {
return { return {
isValid: false, isValid: false,
parsed: "", parsed: "",
@ -407,8 +409,8 @@ export const VALIDATORS: Record<ValidationType, Validator> = {
: "", : "",
}; };
} }
const isValid = moment(value, props.dateFormat).isValid(); const isValid = moment(dateString, dateFormat).isValid();
const parsed = isValid ? value : today; const parsed = isValid ? dateString : todayDateString;
return { return {
isValid, isValid,
parsed, parsed,