Skip to content

Commit d39dfda

Browse files
committed
refactor and simplify some stuff
1 parent 0bbbaf0 commit d39dfda

File tree

2 files changed

+69
-60
lines changed

2 files changed

+69
-60
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,17 @@ describe('Date range picker', () => {
15601560
expect(dateTimeInputs[1].value).to.equal(null);
15611561
});
15621562

1563+
it('should not be in invalid state on reset for a required control with no value', async () => {
1564+
spec.setProperties({ value: null });
1565+
spec.setProperties({ required: true });
1566+
1567+
spec.assertSubmitFails();
1568+
await checkInputsInvalidState(spec.element, true, true);
1569+
1570+
spec.reset();
1571+
await checkInputsInvalidState(spec.element, false, false);
1572+
});
1573+
15631574
it('should reset to the new default value after setAttribute() call', async () => {
15641575
const date1 = today.add('day', -2);
15651576
const date2 = today.add('day', 2);

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

Lines changed: 58 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,6 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
228228
private _placeholder?: string;
229229
private _defaultMask!: string;
230230
private _currentValue: DateRangeValue | null = null;
231-
private _startDate!: Date | null;
232-
private _endDate!: Date | null;
233-
private _swapDatesFlag = false;
234231
private _visibleMonths: 1 | 2 = 2;
235232

236233
private predefinedRanges: CustomDateRange[] = [
@@ -276,6 +273,12 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
276273
return this.mode === 'dropdown';
277274
}
278275

276+
private get allRanges(): CustomDateRange[] {
277+
return this.usePredefinedRanges
278+
? [...this.predefinedRanges, ...this.customRanges]
279+
: [...this.customRanges];
280+
}
281+
279282
@queryAll(IgcDateTimeInputComponent.tagName)
280283
private _inputs!: IgcDateTimeInputComponent[];
281284

@@ -302,8 +305,6 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
302305
@property({ converter: convertToDateRange })
303306
public set value(value: DateRangeValue | string | null | undefined) {
304307
const converted = convertToDateRange(value);
305-
this._startDate = converted?.start ?? null;
306-
this._endDate = converted?.end ?? null;
307308

308309
this._formValue.setValueAndFormState(converted);
309310
this._validate();
@@ -381,7 +382,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
381382
public get visibleMonths(): 1 | 2 {
382383
return this._visibleMonths;
383384
}
384-
// setter used to ensure that the property is set to either 1 or 2
385+
385386
public set visibleMonths(value: 1 | 2) {
386387
this._visibleMonths = [1, 2].includes(value) ? value : 2;
387388
}
@@ -417,9 +418,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
417418
}
418419

419420
public get placeholder(): string {
420-
const rangePlaceholder = !this.useTwoInputs
421-
? `${this.inputFormat} - ${this.inputFormat}`
422-
: this.inputFormat;
421+
const rangePlaceholder = `${this.inputFormat} - ${this.inputFormat}`;
423422
return this._placeholder ?? rangePlaceholder;
424423
}
425424

@@ -625,8 +624,10 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
625624

626625
protected override formResetCallback() {
627626
super.formResetCallback();
628-
this._startDate = this._formValue.defaultValue?.start ?? null;
629-
this._endDate = this._formValue.defaultValue?.end ?? null;
627+
this._formValue.setValueAndFormState({
628+
start: this._formValue.defaultValue?.start ?? null,
629+
end: this._formValue.defaultValue?.end ?? null,
630+
});
630631

631632
this.setCalendarRangeValues();
632633
this.updateMaskedRangeValue();
@@ -656,65 +657,51 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
656657
}
657658

658659
protected dialogDone() {
659-
this.emitEvent('igcChange', { detail: this.value ?? undefined });
660+
this.emitEvent('igcChange', { detail: this.value });
660661
this._hide(true);
661662
}
662663

663664
protected async handleInputEvent(event: CustomEvent<Date | null>) {
664665
event.stopPropagation();
665-
this._swapDatesFlag = true;
666666
if (this.nonEditable) {
667667
event.preventDefault();
668668
return;
669669
}
670670
const input = event.target as IgcDateTimeInputComponent;
671671
const newValue = input.value ? CalendarDay.from(input.value).native : null;
672672

673-
if (event.target === this._inputs[0]) {
674-
this._startDate = newValue;
675-
} else {
676-
this._endDate = newValue;
677-
}
673+
this.value = this.getUpdatedDateRange(input, newValue);
678674
this._calendar.activeDate = newValue;
679675

680-
this.value = { start: this._startDate, end: this._endDate };
681676
this.emitEvent('igcInput', { detail: this.value });
682677
}
683678

684679
protected handleInputChangeEvent(event: CustomEvent<Date | null>) {
685680
event.stopPropagation();
686-
this._swapDatesFlag = false;
687681

688682
const input = event.target as IgcDateTimeInputComponent;
689683
const newValue = input.value ? CalendarDay.from(input.value).native : null;
690684

691-
if (event.target === this._inputs[0]) {
692-
this._startDate = newValue;
693-
} else {
694-
this._endDate = newValue;
695-
}
685+
const updatedRange = this.getUpdatedDateRange(input, newValue);
686+
const { start, end } = this.swapDates(updatedRange);
696687

697688
this.setCalendarRangeValues();
698-
this.value = { start: this._startDate, end: this._endDate };
699-
this.emitEvent('igcChange', { detail: this.value ?? undefined });
689+
this.value = { start, end };
690+
this.emitEvent('igcChange', { detail: this.value });
700691
}
701692

702693
@watch('open')
703694
protected openChange() {
704695
this._rootClickController.update();
705696

706697
if (this.open && this.mode === 'dialog') {
707-
this._currentValue = this.value ? { ...this.value } : null;
698+
this._currentValue = this.value;
708699
}
709700
}
710701

711702
@watch('locale')
712703
protected updateDefaultMask(): void {
713704
this._defaultMask = DateTimeUtil.getDefaultMask(this.locale);
714-
if (this._inputs[0] && this._inputs[1]) {
715-
this._inputs[0].locale = this.locale;
716-
this._inputs[1].locale = this.locale;
717-
}
718705
this.updateMaskedRangeValue();
719706
}
720707

@@ -761,7 +748,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
761748
}
762749

763750
protected override async handleAnchorClick() {
764-
this._calendar.activeDate = this._startDate ?? this._calendar.activeDate;
751+
this._calendar.activeDate = this.value?.start ?? this._calendar.activeDate;
765752
super.handleAnchorClick();
766753
await this.updateComplete;
767754
this._calendar[focusActiveDate]();
@@ -779,9 +766,10 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
779766
}
780767

781768
const rangeValues = (event.target as IgcCalendarComponent).values;
782-
this._startDate = rangeValues[0];
783-
this._endDate = rangeValues[rangeValues.length - 1];
784-
this.value = { start: this._startDate, end: this._endDate };
769+
this.value = {
770+
start: rangeValues[0],
771+
end: rangeValues[rangeValues.length - 1],
772+
};
785773

786774
if (this.isDropDown) {
787775
this.emitEvent('igcChange', { detail: this.value });
@@ -794,10 +782,16 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
794782
event.preventDefault();
795783
}
796784

797-
private get allRanges(): CustomDateRange[] {
798-
return this.usePredefinedRanges
799-
? [...this.predefinedRanges, ...this.customRanges]
800-
: [...this.customRanges];
785+
private getUpdatedDateRange(
786+
input: IgcDateTimeInputComponent,
787+
newValue: Date | null
788+
): DateRangeValue {
789+
const currentStart = this.value?.start ?? null;
790+
const currentEnd = this.value?.end ?? null;
791+
792+
return input === this._inputs[0]
793+
? { start: newValue, end: currentEnd }
794+
: { start: currentStart, end: newValue };
801795
}
802796

803797
// delegate the inputs validity state to the date-range-picker
@@ -869,35 +863,38 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
869863
return;
870864
}
871865

872-
if (!this._startDate || !this._endDate) {
866+
if (!this.value?.start || !this.value?.end) {
873867
this._calendar.values = null;
874868
return;
875869
}
876870

877-
const calendarDayStart = toCalendarDay(this._startDate);
878-
const calendarDayEnd = toCalendarDay(this._endDate);
879-
const isStartEarlierThanEnd = calendarDayStart.lessThan(calendarDayEnd);
871+
const start = toCalendarDay(this.value.start);
872+
const end = toCalendarDay(this.value.end);
880873

881-
// dates should not be swapped while typing
882-
if (!this._swapDatesFlag && !isStartEarlierThanEnd) {
883-
this.swapDates();
884-
}
885-
if (!calendarDayStart.equalTo(calendarDayEnd)) {
874+
if (!start.equalTo(end)) {
886875
const range = Array.from(
887-
calendarRange({ start: this._startDate, end: this._endDate })
876+
calendarRange({ start: this.value.start!, end: this.value.end! })
888877
);
889878
// calendarRange is non-inclusive
890-
range.push(last(range).add('day', isStartEarlierThanEnd ? 1 : -1));
879+
range.push(last(range).add('day', start.lessThan(end) ? 1 : -1));
891880
this._calendar.values = range.map((d) => d.native);
892881
} else {
893-
this._calendar.values = [this._startDate];
882+
this._calendar.values = [this.value.start!];
894883
}
895884
}
896885

897-
private swapDates() {
898-
const temp = this._startDate;
899-
this._startDate = this._endDate;
900-
this._endDate = temp;
886+
private swapDates(range: DateRangeValue): DateRangeValue {
887+
const { start, end } = range;
888+
if (start && end) {
889+
const calendarDayStart = toCalendarDay(start);
890+
const calendarDayEnd = toCalendarDay(end);
891+
const isStartEarlierThanEnd = calendarDayStart.lessThan(calendarDayEnd);
892+
893+
if (!isStartEarlierThanEnd) {
894+
return { start: end, end: start };
895+
}
896+
}
897+
return { start, end };
901898
}
902899

903900
private async _shouldCloseCalendarDropdown() {
@@ -917,7 +914,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
917914

918915
private _select(value: DateRangeValue | null, emitEvent = false) {
919916
this.value = value;
920-
this._calendar.activeDate = this._startDate ?? this._calendar.activeDate;
917+
this._calendar.activeDate = this.value?.start ?? this._calendar.activeDate;
921918
if (emitEvent) {
922919
this.emitEvent('igcChange', { detail: this.value });
923920
}
@@ -988,7 +985,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
988985
?show-week-numbers=${this.showWeekNumbers}
989986
?hide-outside-days=${this.hideOutsideDays}
990987
?hide-header=${hideHeader}
991-
.activeDate=${this.activeDate ?? this._startDate}
988+
.activeDate=${this.activeDate ?? this.value?.start}
992989
.headerOrientation=${this.headerOrientation}
993990
.orientation=${this.orientation}
994991
.visibleMonths=${this._visibleMonths}
@@ -1088,6 +1085,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
10881085
const format = formats.has(this._displayFormat!)
10891086
? `${this._displayFormat}Date`
10901087
: this._displayFormat;
1088+
const value = picker === 'start' ? this.value?.start : this.value?.end;
10911089

10921090
return html`
10931091
<igc-date-time-input
@@ -1097,8 +1095,8 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
10971095
display-format=${ifDefined(format)}
10981096
?disabled=${this.disabled}
10991097
?readonly=${readOnly}
1100-
.value=${picker === 'start' ? this.value?.start : this.value?.end}
1101-
.locale=${this.locale}
1098+
.value=${value ?? null}
1099+
.locale=${live(this.locale)}
11021100
.prompt=${this.prompt}
11031101
.outlined=${this.outlined}
11041102
.placeholder=${placeholder}

0 commit comments

Comments
 (0)