Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pickers] Fix date and time merging to retain milliseconds #14173

Merged
merged 7 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ describe('<DateCalendar />', () => {

userEvent.mousePress(screen.getByRole('gridcell', { name: '2' }));
expect(onChange.callCount).to.equal(1);
expect(onChange.lastCall.firstArg).toEqualDateTime(adapterToUse.date('2018-01-02T11:11:11'));
expect(onChange.lastCall.firstArg).toEqualDateTime(
adapterToUse.date('2018-01-02T11:11:11.111'),
);
});

it('should complete weeks when showDaysOutsideCurrentMonth=true', () => {
Expand Down
60 changes: 39 additions & 21 deletions packages/x-date-pickers/src/internals/utils/date-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ describe('findClosestEnabledDate', () => {
disableFuture: false,
disablePast: false,
timezone: 'default',
})!;
});

expect(adapterToUse.isSameDay(result, adapterToUse.date('2000-01-01'))).to.equal(true);
expect(result).toEqualDateTime(adapterToUse.date('2000-01-01'));
});

it('should return next 18th going from 10th', () => {
Expand All @@ -47,9 +47,9 @@ describe('findClosestEnabledDate', () => {
disableFuture: false,
disablePast: false,
timezone: 'default',
})!;
});

expect(adapterToUse.isSameDay(result, adapterToUse.date('2018-08-18'))).to.equal(true);
expect(result).toEqualDateTime(adapterToUse.date('2018-08-18'));
});

it('should return previous 18th going from 1st', () => {
Expand All @@ -62,9 +62,9 @@ describe('findClosestEnabledDate', () => {
disableFuture: false,
disablePast: false,
timezone: 'default',
})!;
});

expect(adapterToUse.isSameDay(result, adapterToUse.date('2018-07-18'))).to.equal(true);
expect(result).toEqualDateTime(adapterToUse.date('2018-07-18'));
});

it('should return future 18th if disablePast', () => {
Expand All @@ -78,7 +78,7 @@ describe('findClosestEnabledDate', () => {
disableFuture: false,
disablePast: true,
timezone: 'default',
})!;
});

expect(adapterToUse.isBefore(result, today)).to.equal(false);
expect(adapterToUse.isBefore(result, adapterToUse.addDays(today, 31))).to.equal(true);
Expand All @@ -95,9 +95,9 @@ describe('findClosestEnabledDate', () => {
disableFuture: true,
disablePast: true,
timezone: 'default',
})!;
});

expect(adapterToUse.isSameDay(result, today)).to.equal(true);
expect(result).toEqualDateTime(today);
});

it('should return now with given time part if disablePast and now is valid', () => {
Expand All @@ -113,13 +113,13 @@ describe('findClosestEnabledDate', () => {
disableFuture: false,
disablePast: true,
timezone: 'default',
})!;
});

expect(result).toEqualDateTime(adapterToUse.addDays(tryDate, 1));
clock.reset();
});

it('should fallback to today if disablePast+disableFuture and now is invalid', () => {
it('should return `null` when disablePast+disableFuture and now is invalid', () => {
const today = adapterToUse.date();
const result = findClosestEnabledDate({
date: adapterToUse.date('2000-01-01'),
Expand All @@ -132,7 +132,7 @@ describe('findClosestEnabledDate', () => {
timezone: 'default',
});

expect(adapterToUse.isEqual(result, adapterToUse.date()));
expect(result).to.equal(null);
});

it('should return minDate if it is after the date and valid', () => {
Expand All @@ -145,9 +145,9 @@ describe('findClosestEnabledDate', () => {
disableFuture: false,
disablePast: false,
timezone: 'default',
})!;
});

expect(adapterToUse.isSameDay(result, adapterToUse.date('2018-08-18'))).to.equal(true);
expect(result).toEqualDateTime(adapterToUse.date('2018-08-18'));
});

it('should return next 18th after minDate', () => {
Expand All @@ -160,9 +160,27 @@ describe('findClosestEnabledDate', () => {
disableFuture: false,
disablePast: false,
timezone: 'default',
})!;
});

expect(result).toEqualDateTime(adapterToUse.date('2018-08-18'));
});

it('should keep the time of the `date` when `disablePast`', () => {
const clock = useFakeTimers({ now: new Date('2000-01-02T11:12:13.123Z') });

const result = findClosestEnabledDate({
date: adapterToUse.date('2000-01-01T11:12:13.550Z'),
minDate: adapterToUse.date('1900-01-01'),
maxDate: adapterToUse.date('2100-01-01'),
utils: adapterToUse,
isDateDisabled: () => false,
disableFuture: false,
disablePast: true,
timezone: 'default',
});

expect(adapterToUse.isSameDay(result, adapterToUse.date('2018-08-18'))).to.equal(true);
expect(result).toEqualDateTime(adapterToUse.date('2000-01-02T11:12:13.550Z'));
clock.reset();
});

it('should return maxDate if it is before the date and valid', () => {
Expand All @@ -175,9 +193,9 @@ describe('findClosestEnabledDate', () => {
disableFuture: false,
disablePast: false,
timezone: 'default',
})!;
});

expect(adapterToUse.isSameDay(result, adapterToUse.date('2018-07-18'))).to.equal(true);
expect(result).toEqualDateTime(adapterToUse.date('2018-07-18'));
});

it('should return previous 18th before maxDate', () => {
Expand All @@ -190,9 +208,9 @@ describe('findClosestEnabledDate', () => {
disableFuture: false,
disablePast: false,
timezone: 'default',
})!;
});

expect(adapterToUse.isSameDay(result, adapterToUse.date('2018-07-18'))).to.equal(true);
expect(result).toEqualDateTime(adapterToUse.date('2018-07-18'));
});

it('should return null if minDate is after maxDate', () => {
Expand All @@ -205,7 +223,7 @@ describe('findClosestEnabledDate', () => {
disableFuture: false,
disablePast: false,
timezone: 'default',
})!;
});

expect(result).to.equal(null);
});
Expand Down
1 change: 1 addition & 0 deletions packages/x-date-pickers/src/internals/utils/date-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const mergeDateAndTime = <TDate extends PickerValidDate>(
mergedDate = utils.setHours(mergedDate, utils.getHours(timeParam));
mergedDate = utils.setMinutes(mergedDate, utils.getMinutes(timeParam));
mergedDate = utils.setSeconds(mergedDate, utils.getSeconds(timeParam));
mergedDate = utils.setMilliseconds(mergedDate, utils.getMilliseconds(timeParam));

return mergedDate;
};
Expand Down