Skip to content

Commit

Permalink
fix(DateInput): handle invalid dates
Browse files Browse the repository at this point in the history
  • Loading branch information
zouxuoz committed Nov 23, 2018
1 parent 6eb8c23 commit d5c1508
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/atoms/dataEntry/DateInput/DateInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class DateInput extends React.Component<DateInputProps, DateInputState> {
this.setState({ textValue: value });

if (!value) {
this.props.onChange(value);
this.props.onChange(null);

return;
}
Expand Down Expand Up @@ -93,7 +93,7 @@ class DateInput extends React.Component<DateInputProps, DateInputState> {
const dateFormat = withTime ? utils.DATETIME_FORMAT : utils.DATE_FORMAT;

return {
selected: value ? new Date(value) : null,
selected: utils.fromISOtoJSDate(value),
dateFormat,
...rest,
showTimeSelect: withTime,
Expand Down
40 changes: 33 additions & 7 deletions src/atoms/dataEntry/DateInput/DateInput.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,43 @@ export const DATETIME_FORMAT = 'MM/dd/yyyy, hh:mm a';

export const fromISOToViewFormat = (value: ?string, withTime: ?boolean) => {
if (value) {
value = DateTime.fromISO(value).toFormat(withTime ? DATETIME_FORMAT : DATE_FORMAT);
value = DateTime.fromISO(value);

if (value.isValid) {
value = value.toFormat(withTime ? DATETIME_FORMAT : DATE_FORMAT);
} else {
value = null;
}
} else {
value = null;
}

return value;
};

export const fromJSDateToISO = (value: ?Date, withTime: ?boolean) => {
export const fromISOtoJSDate = (value: ?string) => {
if (value) {
value = DateTime.fromJSDate(value).toISO();
value = DateTime.fromISO(value);

if (!withTime) {
value = value.slice(0, 10);
if (value.isValid) {
value = value.toJSDate();
} else {
value = null;
}
} else {
value = null;
}

return value;
};

export const fromJSDateToISO = (value: ?Date, withTime: ?boolean) => {
if (value) {
value = DateTime.fromJSDate(value);

value = fromLuxonToISO(value, withTime);
} else {
value = null;
}

return value;
Expand All @@ -31,14 +55,16 @@ export const fromJSDateToISO = (value: ?Date, withTime: ?boolean) => {
export const fromViewFormatToLuxon = (value: ?string, withTime: ?boolean) => {
if (value) {
value = DateTime.fromFormat(value, withTime ? DATETIME_FORMAT : DATE_FORMAT);
} else {
value = null;
}

return value;
};

export const fromLuxonToISO = (value: ?Object, withTime: ?boolean) => {
if (value) {
value = value.toISO();
if (value && value.isValid) {
value = value.setZone('utc').toISO();

if (!withTime) {
value = value.slice(0, 10);
Expand Down

0 comments on commit d5c1508

Please sign in to comment.