Skip to content

Commit 12a0b1d

Browse files
committed
refactor: General date (range) picker code refactor
1 parent bd6f682 commit 12a0b1d

File tree

7 files changed

+125
-162
lines changed

7 files changed

+125
-162
lines changed

src/components/calendar/model.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ export class CalendarDay {
4444
});
4545
}
4646

47+
/**
48+
* Compares the date portion of two date objects.
49+
*
50+
* @returns
51+
* ```
52+
* first === second // 0
53+
* first > second // 1
54+
* first < second // -1
55+
* ```
56+
*/
57+
public static compare(first: DayParameter, second: DayParameter) {
58+
const a = toCalendarDay(first);
59+
const b = toCalendarDay(second);
60+
61+
if (a.equalTo(b)) {
62+
return 0;
63+
}
64+
return a.greaterThan(b) ? 1 : -1;
65+
}
66+
4767
constructor(args: CalendarDayParams) {
4868
this._date = new Date(args.year, args.month, args.date ?? 1);
4969
}

src/components/common/validators.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DateTimeUtil } from '../date-time-input/date-util.js';
1+
import { CalendarDay } from '../calendar/model.js';
22
import validatorMessages from './localization/validation-en.js';
33
import {
44
asNumber,
@@ -135,9 +135,7 @@ export const minDateValidator: Validator<{
135135
key: 'rangeUnderflow',
136136
message: ({ min }) => formatString(validatorMessages.min, min),
137137
isValid: ({ value, min }) =>
138-
value && min
139-
? !DateTimeUtil.lessThanMinValue(value, min, false, true)
140-
: true,
138+
value && min ? CalendarDay.compare(value, min) >= 0 : true,
141139
};
142140

143141
export const maxDateValidator: Validator<{
@@ -147,7 +145,5 @@ export const maxDateValidator: Validator<{
147145
key: 'rangeOverflow',
148146
message: ({ max }) => formatString(validatorMessages.max, max),
149147
isValid: ({ value, max }) =>
150-
value && max
151-
? !DateTimeUtil.greaterThanMaxValue(value, max, false, true)
152-
: true,
148+
value && max ? CalendarDay.compare(value, max) <= 0 : true,
153149
};

src/components/date-picker/date-picker.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -654,9 +654,8 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
654654
//#region Render methods
655655

656656
private _renderClearIcon() {
657-
return !this.value
658-
? nothing
659-
: html`
657+
return this.value
658+
? html`
660659
<span
661660
slot="suffix"
662661
part="clear-icon"
@@ -670,7 +669,8 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
670669
></igc-icon>
671670
</slot>
672671
</span>
673-
`;
672+
`
673+
: nothing;
674674
}
675675

676676
private _renderCalendarIcon() {
@@ -844,10 +844,9 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
844844
const id = this.id || this._inputId;
845845

846846
return html`
847-
${!this._isMaterialTheme ? this._renderLabel(id) : nothing}
848-
${this._renderInput(id)}${this._renderPicker(
849-
id
850-
)}${this._renderHelperText()}
847+
${this._isMaterialTheme ? nothing : this._renderLabel(id)}
848+
${this._renderInput(id)} ${this._renderPicker(id)}
849+
${this._renderHelperText()}
851850
`;
852851
}
853852

0 commit comments

Comments
 (0)