Skip to content

Commit 361fb4a

Browse files
Review: Michel
1 parent 58e2d48 commit 361fb4a

File tree

24 files changed

+41
-55
lines changed

24 files changed

+41
-55
lines changed

docs/data/migration/migration-pickers-v7/migration-pickers-v7.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ This change causes a few breaking changes:
371371

372372
// This contains a small behavior change.
373373
// If the picker receives an invalid date,
374-
// the old value would equal `null`.
375-
// the new value would equal the invalid date received.
374+
// the old value equals `null`.
375+
// the new value equals the invalid date received.
376376
-const { value } = props;
377377
+const { value } = usePickerContext();
378378
```

packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ const DateRangeCalendar = React.forwardRef(function DateRangeCalendar(
292292
// This makes sure that `isWithinRange` works with any time in the start and end day.
293293
const valueDayRange = React.useMemo<PickerRangeValue>(
294294
() => [
295-
value[0] == null || !utils.isValid(value[0]) ? value[0] : utils.startOfDay(value[0]),
296-
value[1] == null || !utils.isValid(value[1]) ? value[1] : utils.endOfDay(value[1]),
295+
!utils.isValid(value[0]) ? value[0] : utils.startOfDay(value[0]),
296+
!utils.isValid(value[1]) ? value[1] : utils.endOfDay(value[1]),
297297
],
298298
[value, utils],
299299
);
@@ -386,7 +386,7 @@ const DateRangeCalendar = React.forwardRef(function DateRangeCalendar(
386386
const prevValue = React.useRef<PickerRangeValue | null>(null);
387387
React.useEffect(() => {
388388
const date = rangePosition === 'start' ? value[0] : value[1];
389-
if (!date || !utils.isValid(date)) {
389+
if (!utils.isValid(date)) {
390390
return;
391391
}
392392

packages/x-date-pickers-pro/src/DateRangePicker/DateRangePickerToolbar.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const DateRangePickerToolbar = React.forwardRef(function DateRangePickerToolbar(
9999
const toolbarFormat = toolbarFormatProp ?? utils.formats.shortDate;
100100

101101
const formatDate = (date: PickerValidDate | null, fallback: string) => {
102-
if (date == null || !utils.isValid(date)) {
102+
if (!utils.isValid(date)) {
103103
return fallback;
104104
}
105105

packages/x-date-pickers-pro/src/internals/utils/date-range-manager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export function calculateRangeChange({
3131
shouldMergeDateAndTime = false,
3232
referenceDate,
3333
}: CalculateRangeChangeOptions): CalculateRangeChangeResponse {
34-
const start = range[0] == null || !utils.isValid(range[0]) ? null : range[0];
35-
const end = range[1] == null || !utils.isValid(range[1]) ? null : range[1];
34+
const start = !utils.isValid(range[0]) ? null : range[0];
35+
const end = !utils.isValid(range[1]) ? null : range[1];
3636

3737
if (shouldMergeDateAndTime && selectedDate) {
3838
// If there is a date already selected, then we want to keep its time

packages/x-date-pickers-pro/src/internals/utils/valueManagers.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export const rangeValueManager: RangePickerValueManager = {
3434
getTodayDate(utils, timezone, valueType),
3535
],
3636
getInitialReferenceValue: ({ value, referenceDate: referenceDateProp, ...params }) => {
37-
const shouldKeepStartDate = value[0] != null && params.utils.isValid(value[0]);
38-
const shouldKeepEndDate = value[1] != null && params.utils.isValid(value[1]);
37+
const shouldKeepStartDate = params.utils.isValid(value[0]);
38+
const shouldKeepEndDate = params.utils.isValid(value[1]);
3939

4040
if (shouldKeepStartDate && shouldKeepEndDate) {
4141
return value as PickerNonNullableRangeValue;
@@ -56,10 +56,8 @@ export const rangeValueManager: RangePickerValueManager = {
5656
hasError: (error) => error[0] != null || error[1] != null,
5757
defaultErrorState: [null, null],
5858
getTimezone: (utils, value) => {
59-
const timezoneStart =
60-
value[0] == null || !utils.isValid(value[0]) ? null : utils.getTimezone(value[0]);
61-
const timezoneEnd =
62-
value[1] == null || !utils.isValid(value[1]) ? null : utils.getTimezone(value[1]);
59+
const timezoneStart = utils.isValid(value[0]) ? utils.getTimezone(value[0]) : null;
60+
const timezoneEnd = utils.isValid(value[1]) ? utils.getTimezone(value[1]) : null;
6361

6462
if (timezoneStart != null && timezoneEnd != null && timezoneStart !== timezoneEnd) {
6563
throw new Error('MUI X: The timezone of the start and the end date should be the same.');
@@ -79,8 +77,8 @@ export const getRangeFieldValueManager = ({
7977
dateSeparator: string | undefined;
8078
}): FieldValueManager<PickerRangeValue> => ({
8179
updateReferenceValue: (utils, value, prevReferenceValue) => {
82-
const shouldKeepStartDate = value[0] != null && utils.isValid(value[0]);
83-
const shouldKeepEndDate = value[1] != null && utils.isValid(value[1]);
80+
const shouldKeepStartDate = utils.isValid(value[0]);
81+
const shouldKeepEndDate = utils.isValid(value[1]);
8482

8583
if (!shouldKeepStartDate && !shouldKeepEndDate) {
8684
return prevReferenceValue;
@@ -184,10 +182,9 @@ export const getRangeFieldValueManager = ({
184182
},
185183
getNewValuesFromNewActiveDate: (newActiveDate) => ({
186184
value: updateDateInRange(newActiveDate, state.value),
187-
referenceValue:
188-
newActiveDate == null || !utils.isValid(newActiveDate)
189-
? state.referenceValue
190-
: updateDateInRange(newActiveDate, state.referenceValue),
185+
referenceValue: !utils.isValid(newActiveDate)
186+
? state.referenceValue
187+
: updateDateInRange(newActiveDate, state.referenceValue),
191188
}),
192189
};
193190
},

packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class AdapterDateFns
107107
return dateFnsParse(value, format, new Date(), { locale: this.locale });
108108
};
109109

110-
public isValid = (value: Date | null): boolean => {
110+
public isValid = (value: Date | null): value is Date => {
111111
if (value == null) {
112112
return false;
113113
}

packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class AdapterDateFnsJalali
155155
return dateFnsParse(value, format, new Date(), { locale: this.locale });
156156
};
157157

158-
public isValid = (value: Date | null): boolean => {
158+
public isValid = (value: Date | null): value is Date => {
159159
if (value == null) {
160160
return false;
161161
}

packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class AdapterDateFnsJalali
162162
return dateFnsParse(value, format, new Date(), { locale: this.locale });
163163
};
164164

165-
public isValid = (value: Date | null): boolean => {
165+
public isValid = (value: Date | null): value is Date => {
166166
if (value == null) {
167167
return false;
168168
}

packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class AdapterDateFns
115115
return dateFnsParse(value, format, new Date(), { locale: this.locale });
116116
};
117117

118-
public isValid = (value: Date | null): boolean => {
118+
public isValid = (value: Date | null): value is Date => {
119119
if (value == null) {
120120
return false;
121121
}

packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export class AdapterDayjs implements MuiPickersAdapter<string> {
414414
);
415415
};
416416

417-
public isValid = (value: Dayjs | null) => {
417+
public isValid = (value: Dayjs | null): value is Dayjs => {
418418
if (value == null) {
419419
return false;
420420
}

packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export class AdapterLuxon implements MuiPickersAdapter<string> {
247247
);
248248
};
249249

250-
public isValid = (value: DateTime | null): boolean => {
250+
public isValid = (value: DateTime | null): value is DateTime => {
251251
if (value === null) {
252252
return false;
253253
}

packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ export class AdapterMoment implements MuiPickersAdapter<string> {
304304
.join('');
305305
};
306306

307-
public isValid = (value: Moment | null) => {
307+
public isValid = (value: Moment | null): value is Moment => {
308308
if (value == null) {
309309
return false;
310310
}

packages/x-date-pickers/src/DateCalendar/DateCalendar.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ export const DateCalendar = React.forwardRef(function DateCalendar(
293293
});
294294

295295
React.useEffect(() => {
296-
if (value != null && utils.isValid(value)) {
296+
if (utils.isValid(value)) {
297297
changeMonth(value);
298298
}
299299
}, [value]); // eslint-disable-line

packages/x-date-pickers/src/DatePicker/DatePickerToolbar.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const DatePickerToolbar = React.forwardRef(function DatePickerToolbar(
9494
const classes = useUtilityClasses(classesProp);
9595

9696
const dateText = React.useMemo(() => {
97-
if (value == null || !utils.isValid(value)) {
97+
if (!utils.isValid(value)) {
9898
return toolbarPlaceholder;
9999
}
100100

packages/x-date-pickers/src/DateTimePicker/DateTimePickerToolbar.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ function DateTimePickerToolbar(inProps: DateTimePickerToolbarProps) {
303303
const toolbarTitle = inToolbarTitle ?? translations.dateTimePickerToolbarTitle;
304304

305305
const dateText = React.useMemo(() => {
306-
if (value == null || !utils.isValid(value)) {
306+
if (!utils.isValid(value)) {
307307
return toolbarPlaceholder;
308308
}
309309

@@ -315,7 +315,7 @@ function DateTimePickerToolbar(inProps: DateTimePickerToolbarProps) {
315315
}, [value, toolbarFormat, toolbarPlaceholder, utils]);
316316

317317
const formatSection = (format: keyof AdapterFormats, fallback: string) => {
318-
if (value == null || !utils.isValid(value)) {
318+
if (!utils.isValid(value)) {
319319
return fallback;
320320
}
321321

packages/x-date-pickers/src/TimePicker/TimePickerToolbar.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function TimePickerToolbar(inProps: TimePickerToolbarProps) {
168168
);
169169

170170
const formatSection = (format: keyof AdapterFormats) => {
171-
if (value == null || !utils.isValid(value)) {
171+
if (!utils.isValid(value)) {
172172
return '--';
173173
}
174174

packages/x-date-pickers/src/internals/hooks/date-helpers-hooks.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ export function useMeridiemMode(
5757
selectionState?: PickerSelectionState,
5858
) {
5959
const utils = useUtils();
60-
const cleanDate = React.useMemo(
61-
() => (date == null || !utils.isValid(date) ? null : date),
62-
[utils, date],
63-
);
60+
const cleanDate = React.useMemo(() => (!utils.isValid(date) ? null : date), [utils, date]);
6461

6562
const meridiemMode = getMeridiem(cleanDate, utils);
6663

packages/x-date-pickers/src/internals/hooks/useField/buildSectionsFromFormat.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ const createSection = ({
143143
? hasLeadingZerosInFormat
144144
: sectionConfig.contentType === 'digit';
145145

146-
const isValidDate = date != null && utils.isValid(date);
146+
const isValidDate = utils.isValid(date);
147147
let sectionValue = isValidDate ? utils.formatByString(date, token) : '';
148148
let maxLength: number | null = null;
149149

packages/x-date-pickers/src/internals/hooks/useField/useField.utils.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -548,10 +548,7 @@ export const getSectionsBoundaries = (
548548
}),
549549
day: ({ currentDate }) => ({
550550
minimum: 1,
551-
maximum:
552-
currentDate != null && utils.isValid(currentDate)
553-
? utils.getDaysInMonth(currentDate)
554-
: maxDaysInMonth,
551+
maximum: utils.isValid(currentDate) ? utils.getDaysInMonth(currentDate) : maxDaysInMonth,
555552
longestMonth: longestMonth!,
556553
}),
557554
weekDay: ({ format, contentType }) => {

packages/x-date-pickers/src/internals/hooks/useField/useFieldState.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export const useFieldState = <
274274
const updateValueFromValueStr = (valueStr: string) => {
275275
const parseDateStr = (dateStr: string, referenceDate: PickerValidDate) => {
276276
const date = utils.parse(dateStr, format);
277-
if (date == null || !utils.isValid(date)) {
277+
if (!utils.isValid(date)) {
278278
return null;
279279
}
280280

@@ -335,7 +335,7 @@ export const useFieldState = <
335335
* Then we merge the value of the modified sections into the reference date.
336336
* This makes sure that we don't lose some information of the initial date (like the time on a date field).
337337
*/
338-
if (newActiveDate != null && utils.isValid(newActiveDate)) {
338+
if (utils.isValid(newActiveDate)) {
339339
const mergedDate = mergeDateIntoReferenceDate(
340340
utils,
341341
newActiveDate,

packages/x-date-pickers/src/internals/utils/date-utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const findClosestEnabledDate = ({
9696
export const replaceInvalidDateByNull = (
9797
utils: MuiPickersAdapter,
9898
value: PickerValidDate | null,
99-
): PickerValidDate | null => (value == null || !utils.isValid(value) ? null : value);
99+
): PickerValidDate | null => (!utils.isValid(value) ? null : value);
100100

101101
export const applyDefaultDate = (
102102
utils: MuiPickersAdapter,

packages/x-date-pickers/src/internals/utils/valueManagers.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const singleItemValueManager: SingleItemPickerValueManager = {
1717
emptyValue: null,
1818
getTodayValue: getTodayDate,
1919
getInitialReferenceValue: ({ value, referenceDate, ...params }) => {
20-
if (value != null && params.utils.isValid(value)) {
20+
if (params.utils.isValid(value)) {
2121
return value;
2222
}
2323

@@ -32,15 +32,14 @@ export const singleItemValueManager: SingleItemPickerValueManager = {
3232
isSameError: (a, b) => a === b,
3333
hasError: (error) => error != null,
3434
defaultErrorState: null,
35-
getTimezone: (utils, value) =>
36-
value == null || !utils.isValid(value) ? null : utils.getTimezone(value),
35+
getTimezone: (utils, value) => (utils.isValid(value) ? utils.getTimezone(value) : null),
3736
setTimezone: (utils, timezone, value) =>
3837
value == null ? null : utils.setTimezone(value, timezone),
3938
};
4039

4140
export const singleItemFieldValueManager: FieldValueManager<PickerValue> = {
4241
updateReferenceValue: (utils, value, prevReferenceValue) =>
43-
value == null || !utils.isValid(value) ? prevReferenceValue : value,
42+
!utils.isValid(value) ? prevReferenceValue : value,
4443
getSectionsFromValue: (utils, date, prevSections, getSectionsFromDate) => {
4544
const shouldReUsePrevDateSections = !utils.isValid(date) && !!prevSections;
4645

@@ -58,10 +57,7 @@ export const singleItemFieldValueManager: FieldValueManager<PickerValue> = {
5857
getSections: (sections) => sections,
5958
getNewValuesFromNewActiveDate: (newActiveDate) => ({
6059
value: newActiveDate,
61-
referenceValue:
62-
newActiveDate == null || !utils.isValid(newActiveDate)
63-
? state.referenceValue
64-
: newActiveDate,
60+
referenceValue: utils.isValid(newActiveDate) ? newActiveDate : state.referenceValue,
6561
}),
6662
}),
6763
parseValueStr: (valueStr, referenceValue, parseDate) =>

packages/x-date-pickers/src/locales/utils/getPickersLocalization.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ export const buildGetOpenDialogAriaText = (params: {
2222
const { utils, formatKey, contextTranslation, propsTranslation } = params;
2323

2424
return (value: PickerValidDate | null) => {
25-
const formattedValue =
26-
value !== null && utils.isValid(value) ? utils.format(value, formatKey) : null;
25+
const formattedValue = utils.isValid(value) ? utils.format(value, formatKey) : null;
2726
const translation = propsTranslation ?? contextTranslation;
2827
return translation(formattedValue);
2928
};

packages/x-date-pickers/src/models/adapters.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export interface MuiPickersAdapter<TLocale = any> {
243243
* @param {PickerValidDate | null} value The value to test.
244244
* @returns {boolean} `true` if the value is a valid date according to the date library.
245245
*/
246-
isValid(value: PickerValidDate | null): boolean;
246+
isValid(value: PickerValidDate | null): value is PickerValidDate;
247247
/**
248248
* Format a date using an adapter format string (see the `AdapterFormats` interface)
249249
* @param {PickerValidDate} value The date to format.

0 commit comments

Comments
 (0)