From 982dc853befe8ccf54163a0b145e563da06f5dc1 Mon Sep 17 00:00:00 2001 From: Sean Perkins Date: Tue, 5 Apr 2022 12:12:13 -0400 Subject: [PATCH] fix(datetime): warn when parsing an invalid date value (#25049) --- core/src/components/datetime/datetime.tsx | 24 ++++++++++++++--------- core/src/utils/logging/index.ts | 9 +++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 core/src/utils/logging/index.ts diff --git a/core/src/components/datetime/datetime.tsx b/core/src/components/datetime/datetime.tsx index b4c38cb95e..396725828f 100644 --- a/core/src/components/datetime/datetime.tsx +++ b/core/src/components/datetime/datetime.tsx @@ -1,5 +1,6 @@ import type { ComponentInterface, EventEmitter } from '@stencil/core'; import { Component, Element, Event, Host, Method, Prop, State, Watch, h, writeTask } from '@stencil/core'; +import { printIonWarning } from '@utils/logging'; import { caretDownSharp, caretUpSharp, chevronBack, chevronDown, chevronForward } from 'ionicons/icons'; import { getIonMode } from '../../global/ionic-global'; @@ -298,15 +299,20 @@ export class Datetime implements ComponentInterface { * This allows us to update the current value's date/time display without * refocusing or shifting the user's display (leaves the user in place). */ - const { month, day, year, hour, minute } = parseDate(this.value); - this.activePartsClone = { - ...this.activeParts, - month, - day, - year, - hour, - minute, - }; + const valueDateParts = parseDate(this.value); + if (valueDateParts) { + const { month, day, year, hour, minute } = valueDateParts; + this.activePartsClone = { + ...this.activeParts, + month, + day, + year, + hour, + minute, + }; + } else { + printIonWarning(`Unable to parse date string: ${this.value}. Please provide a valid ISO 8601 datetime string.`); + } } this.emitStyle(); diff --git a/core/src/utils/logging/index.ts b/core/src/utils/logging/index.ts new file mode 100644 index 0000000000..ebfe9e2e5f --- /dev/null +++ b/core/src/utils/logging/index.ts @@ -0,0 +1,9 @@ +/** + * Logs a warning to the console with an Ionic prefix + * to indicate the library that is warning the developer. + * + * @param message - The string message to be logged to the console. + */ +export const printIonWarning = (message: string) => { + return console.warn(`[Ionic Warning]: ${message}`); +};