fix(datetime): gracefully handle invalid min/max (#28054)

Issue number: resolves #28041

---------

<!-- Please do not submit updates to dependencies unless it fixes an
issue. -->

<!-- Please try to limit your pull request to one type (bugfix, feature,
etc). Submit multiple pull requests if needed. -->

## What is the current behavior?
<!-- Please describe the current behavior that you are modifying. -->

`parseDate` returns `undefined` when given an invalid value. However,
our min/max processing functions did not account for this. As a result,
we would attempt to destructure an undefined value which resulted in an
error.

Note regarding linked issue: The developer is calling
`setMin(undefined)`. However, this is triggering a React quirk with
Custom Elements where `undefined` is being set to `null` inside of
React. The type signature on min/max is `string | undefined`, so `null`
is being treated as an invalid date value.

## What is the new behavior?
<!-- Please describe the behavior or changes that are being added by
this PR. -->

- Min/Max processing functions now return `undefined` if the input was
invalid.

## Does this introduce a breaking change?

- [ ] Yes
- [x] No

<!-- If this introduces a breaking change, please describe the impact
and migration path for existing applications below. -->


## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->

Dev build: `7.3.2-dev.11692887667.1614d10a`
This commit is contained in:
Liam DeBeasi
2023-08-28 08:39:19 -05:00
committed by GitHub
parent 9eef62e4f2
commit 01fc9b4511
2 changed files with 46 additions and 4 deletions

View File

@ -154,6 +154,18 @@ describe('parseMinParts()', () => {
minute: 30, minute: 30,
}); });
}); });
it('should return undefined when given invalid info', () => {
const today = {
day: 14,
month: 3,
year: 2022,
minute: 4,
hour: 2,
};
expect(parseMinParts(undefined, today)).toEqual(undefined);
expect(parseMinParts(null, today)).toEqual(undefined);
expect(parseMinParts('foo', today)).toEqual(undefined);
});
}); });
describe('parseMaxParts()', () => { describe('parseMaxParts()', () => {
@ -205,4 +217,16 @@ describe('parseMaxParts()', () => {
minute: 59, minute: 59,
}); });
}); });
it('should return undefined when given invalid info', () => {
const today = {
day: 14,
month: 3,
year: 2022,
minute: 4,
hour: 2,
};
expect(parseMaxParts(undefined, today)).toEqual(undefined);
expect(parseMaxParts(null, today)).toEqual(undefined);
expect(parseMaxParts('foo', today)).toEqual(undefined);
});
}); });

View File

@ -132,8 +132,17 @@ export const parseAmPm = (hour: number) => {
* For example, max="2012" would fill in the missing * For example, max="2012" would fill in the missing
* month, day, hour, and minute information. * month, day, hour, and minute information.
*/ */
export const parseMaxParts = (max: string, todayParts: DatetimeParts): DatetimeParts => { export const parseMaxParts = (max: string, todayParts: DatetimeParts): DatetimeParts | undefined => {
const { month, day, year, hour, minute } = parseDate(max); const result = parseDate(max);
/**
* If min was not a valid date then return undefined.
*/
if (result === undefined) {
return;
}
const { month, day, year, hour, minute } = result;
/** /**
* When passing in `max` or `min`, developers * When passing in `max` or `min`, developers
@ -168,8 +177,17 @@ export const parseMaxParts = (max: string, todayParts: DatetimeParts): DatetimeP
* For example, min="2012" would fill in the missing * For example, min="2012" would fill in the missing
* month, day, hour, and minute information. * month, day, hour, and minute information.
*/ */
export const parseMinParts = (min: string, todayParts: DatetimeParts): DatetimeParts => { export const parseMinParts = (min: string, todayParts: DatetimeParts): DatetimeParts | undefined => {
const { month, day, year, hour, minute } = parseDate(min); const result = parseDate(min);
/**
* If min was not a valid date then return undefined.
*/
if (result === undefined) {
return;
}
const { month, day, year, hour, minute } = result;
/** /**
* When passing in `max` or `min`, developers * When passing in `max` or `min`, developers