Skip to content

Commit 85a7d73

Browse files
committed
Improve validation
1 parent d9239e1 commit 85a7d73

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

core/src/components/datetime/datetime.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ import {
6969
isNextMonthDisabled,
7070
isPrevMonthDisabled,
7171
} from './utils/state';
72-
import { checkForPresentationFormatMismatch, warnIfTimeZoneProvided } from './utils/warn';
72+
import { checkForPresentationFormatMismatch, warnIfTimeZoneProvided } from './utils/validate';
7373

7474
/**
7575
* @virtualProp {"ios" | "md"} mode - The mode determines which platform styles to use.
@@ -182,9 +182,9 @@ export class Datetime implements ComponentInterface {
182182

183183
@Watch('formatOptions')
184184
protected formatOptionsChanged() {
185-
const { formatOptions, presentation } = this;
186-
checkForPresentationFormatMismatch(presentation, formatOptions);
187-
warnIfTimeZoneProvided(formatOptions);
185+
const { el, formatOptions, presentation } = this;
186+
checkForPresentationFormatMismatch(el, presentation, formatOptions);
187+
warnIfTimeZoneProvided(el, formatOptions);
188188
}
189189

190190
/**
@@ -253,8 +253,8 @@ export class Datetime implements ComponentInterface {
253253

254254
@Watch('presentation')
255255
protected presentationChanged() {
256-
const { formatOptions, presentation } = this;
257-
checkForPresentationFormatMismatch(presentation, formatOptions);
256+
const { el, formatOptions, presentation } = this;
257+
checkForPresentationFormatMismatch(el, presentation, formatOptions);
258258
}
259259

260260
private get isGridStyle() {
@@ -1405,8 +1405,8 @@ export class Datetime implements ComponentInterface {
14051405
}
14061406

14071407
if (formatOptions) {
1408-
checkForPresentationFormatMismatch(presentation, formatOptions);
1409-
warnIfTimeZoneProvided(formatOptions);
1408+
checkForPresentationFormatMismatch(el, presentation, formatOptions);
1409+
warnIfTimeZoneProvided(el, formatOptions);
14101410
}
14111411

14121412
const hourValues = (this.parsedHourValues = convertToArrayOfNumbers(this.hourValues));

core/src/components/datetime/utils/warn.ts renamed to core/src/components/datetime/utils/validate.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ import type { DatetimePresentation, FormatOptions } from '../datetime-interface'
77
* differ from what was selected in the Datetime, which could cause
88
* confusion.
99
*/
10-
export const warnIfTimeZoneProvided = (formatOptions?: FormatOptions) => {
10+
export const warnIfTimeZoneProvided = (el: HTMLElement, formatOptions?: FormatOptions) => {
1111
if (
1212
formatOptions?.date?.timeZone ||
1313
formatOptions?.date?.timeZoneName ||
1414
formatOptions?.time?.timeZone ||
1515
formatOptions?.time?.timeZoneName
1616
) {
17-
printIonWarning('Datetime: "timeZone" and "timeZoneName" are not supported in "formatOptions".');
17+
printIonWarning('Datetime: "timeZone" and "timeZoneName" are not supported in "formatOptions".', el);
1818
}
1919
};
2020

2121
export const checkForPresentationFormatMismatch = (
22+
el: HTMLElement,
2223
presentation: DatetimePresentation,
2324
formatOptions?: FormatOptions
2425
) => {
@@ -32,19 +33,20 @@ export const checkForPresentationFormatMismatch = (
3233
case 'month':
3334
case 'year':
3435
if (formatOptions.date === undefined) {
35-
printIonWarning(`Datetime: The '${presentation}' presentation requires a date object in formatOptions.`);
36+
printIonWarning(`Datetime: The '${presentation}' presentation requires a date object in formatOptions.`, el);
3637
}
3738
break;
3839
case 'time':
3940
if (formatOptions.time === undefined) {
40-
printIonWarning(`Datetime: The 'time' presentation requires a time object in formatOptions.`);
41+
printIonWarning(`Datetime: The 'time' presentation requires a time object in formatOptions.`, el);
4142
}
4243
break;
4344
case 'date-time':
4445
case 'time-date':
4546
if (formatOptions.date === undefined && formatOptions.time === undefined) {
4647
printIonWarning(
47-
`Datetime: The '${presentation}' presentation requires either a date or time object (or both) in formatOptions.`
48+
`Datetime: The '${presentation}' presentation requires either a date or time object (or both) in formatOptions.`,
49+
el
4850
);
4951
}
5052
break;

0 commit comments

Comments
 (0)