Skip to content

Commit 01fc9b4

Browse files
authored
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`
1 parent 9eef62e commit 01fc9b4

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

core/src/components/datetime/test/parse.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,18 @@ describe('parseMinParts()', () => {
154154
minute: 30,
155155
});
156156
});
157+
it('should return undefined when given invalid info', () => {
158+
const today = {
159+
day: 14,
160+
month: 3,
161+
year: 2022,
162+
minute: 4,
163+
hour: 2,
164+
};
165+
expect(parseMinParts(undefined, today)).toEqual(undefined);
166+
expect(parseMinParts(null, today)).toEqual(undefined);
167+
expect(parseMinParts('foo', today)).toEqual(undefined);
168+
});
157169
});
158170

159171
describe('parseMaxParts()', () => {
@@ -205,4 +217,16 @@ describe('parseMaxParts()', () => {
205217
minute: 59,
206218
});
207219
});
220+
it('should return undefined when given invalid info', () => {
221+
const today = {
222+
day: 14,
223+
month: 3,
224+
year: 2022,
225+
minute: 4,
226+
hour: 2,
227+
};
228+
expect(parseMaxParts(undefined, today)).toEqual(undefined);
229+
expect(parseMaxParts(null, today)).toEqual(undefined);
230+
expect(parseMaxParts('foo', today)).toEqual(undefined);
231+
});
208232
});

core/src/components/datetime/utils/parse.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,17 @@ export const parseAmPm = (hour: number) => {
132132
* For example, max="2012" would fill in the missing
133133
* month, day, hour, and minute information.
134134
*/
135-
export const parseMaxParts = (max: string, todayParts: DatetimeParts): DatetimeParts => {
136-
const { month, day, year, hour, minute } = parseDate(max);
135+
export const parseMaxParts = (max: string, todayParts: DatetimeParts): DatetimeParts | undefined => {
136+
const result = parseDate(max);
137+
138+
/**
139+
* If min was not a valid date then return undefined.
140+
*/
141+
if (result === undefined) {
142+
return;
143+
}
144+
145+
const { month, day, year, hour, minute } = result;
137146

138147
/**
139148
* When passing in `max` or `min`, developers
@@ -168,8 +177,17 @@ export const parseMaxParts = (max: string, todayParts: DatetimeParts): DatetimeP
168177
* For example, min="2012" would fill in the missing
169178
* month, day, hour, and minute information.
170179
*/
171-
export const parseMinParts = (min: string, todayParts: DatetimeParts): DatetimeParts => {
172-
const { month, day, year, hour, minute } = parseDate(min);
180+
export const parseMinParts = (min: string, todayParts: DatetimeParts): DatetimeParts | undefined => {
181+
const result = parseDate(min);
182+
183+
/**
184+
* If min was not a valid date then return undefined.
185+
*/
186+
if (result === undefined) {
187+
return;
188+
}
189+
190+
const { month, day, year, hour, minute } = result;
173191

174192
/**
175193
* When passing in `max` or `min`, developers

0 commit comments

Comments
 (0)