Skip to content

Commit

Permalink
Fix min/max
Browse files Browse the repository at this point in the history
  • Loading branch information
martinboulais committed May 22, 2024
1 parent 891d7f8 commit 7ce8761
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions lib/public/components/common/form/inputs/DateTimeInputComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ export class DateTimeInputComponent extends StatefulComponent {
required: this._required,
value: this._value.date,
onchange: (e) => this._patchValue({ date: e.target.value }),
min: inputsMin ? inputsMin.date : undefined,
max: inputsMax ? inputsMax.date : undefined,
// Mithril do not remove min/max if previously set...
min: inputsMin?.date ?? '',
max: inputsMax?.date ?? '',
},
),
h(
Expand All @@ -93,8 +94,9 @@ export class DateTimeInputComponent extends StatefulComponent {
value: this._value.time,
step: this._seconds ? 1 : undefined,
onchange: (e) => this._patchValue({ time: e.target.value }),
min: inputsMin ? inputsMin.time : undefined,
max: inputsMax ? inputsMax.time : undefined,
// Mithril do not remove min/max if previously set...
min: inputsMin?.time ?? '',
max: inputsMax?.time ?? '',
},
),
h(
Expand Down Expand Up @@ -172,17 +174,18 @@ export class DateTimeInputComponent extends StatefulComponent {
return null;
}

const minDateDayAfter = new Date(min + MILLISECONDS_IN_ONE_DAY);
const rawDate = raw.date || null;
const rawTime = raw.time || null;

const minDateAndTime = formatTimestampForDateTimeInput(min, this._seconds);
const ret = {};

if (raw.date !== null && raw.date === minDateAndTime.date) {
if (rawDate !== null && rawDate === minDateAndTime.date) {
ret.time = minDateAndTime.time;
}

if (raw.time !== null && raw.time < minDateAndTime.time) {
ret.date = formatTimestampForDateTimeInput(minDateDayAfter.getTime(), this._seconds).date;
if (rawTime !== null && rawTime < minDateAndTime.time) {
ret.date = formatTimestampForDateTimeInput(min + MILLISECONDS_IN_ONE_DAY, this._seconds).date;
} else {
ret.date = minDateAndTime.date;
}
Expand All @@ -202,16 +205,17 @@ export class DateTimeInputComponent extends StatefulComponent {
return null;
}

const maxDateDayBefore = new Date(max - MILLISECONDS_IN_ONE_DAY);
const rawDate = raw.date || null;
const rawTime = raw.time || null;

const maxDateAndTime = formatTimestampForDateTimeInput(max, this._seconds);
const ret = {};

if (raw.date !== null && raw.date === maxDateAndTime.date) {
if (rawDate !== null && rawDate === maxDateAndTime.date) {
ret.time = maxDateAndTime.time;
}
if (raw.time !== null && raw.time > maxDateAndTime.time) {
ret.date = formatTimestampForDateTimeInput(maxDateDayBefore.getTime(), this._seconds).date;
if (rawTime !== null && rawTime > maxDateAndTime.time) {
ret.date = formatTimestampForDateTimeInput(max - MILLISECONDS_IN_ONE_DAY, this._seconds).date;
} else {
ret.date = maxDateAndTime.date;
}
Expand Down
2 changes: 1 addition & 1 deletion test/public/runs/overview.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ module.exports = () => {
await focusAndType(page, periodInputsSelectors.toDateSelector, '02/05/2021');

await page.waitForFunction(
(selector) => document.querySelector(selector).getAttribute('min') === null,
(selector) => document.querySelector(selector).getAttribute('min') === '',
{ timeout: 500 },
periodInputsSelectors.toTimeSelector,
);
Expand Down

0 comments on commit 7ce8761

Please sign in to comment.