Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(datepicker): validate min/max date
VPAT-664
- Loading branch information
1 parent
cb32ff2
commit e9adcc3
Showing
7 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
projects/angular/src/forms/datepicker/date-input.validator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2016-2022 VMware, Inc. All Rights Reserved. | ||
* This software is released under MIT license. | ||
* The full license information can be found in LICENSE in the root directory of this project. | ||
*/ | ||
|
||
import { Component } from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { FormControl, ReactiveFormsModule } from '@angular/forms'; | ||
|
||
import { ClrDatepickerModule } from './datepicker.module'; | ||
|
||
@Component({ | ||
template: ` | ||
<clr-date-container> | ||
<input type="date" clrDate min="2022-01-01" max="2022-12-31" [formControl]="formControl" /> | ||
</clr-date-container> | ||
`, | ||
}) | ||
class TestComponent { | ||
readonly formControl = new FormControl(); | ||
} | ||
|
||
export default function () { | ||
describe('Date Input Validator', () => { | ||
let fixture: ComponentFixture<TestComponent>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ReactiveFormsModule, ClrDatepickerModule], | ||
declarations: [TestComponent], | ||
}); | ||
|
||
fixture = TestBed.createComponent(TestComponent); | ||
}); | ||
|
||
it('should not allow a date less than the min date', () => { | ||
fixture.componentInstance.formControl.setValue('12/31/2021'); | ||
fixture.detectChanges(); | ||
|
||
const expectedErrors = { | ||
min: { min: '1/1/2022', actual: '12/31/2021' }, | ||
}; | ||
|
||
expect(fixture.componentInstance.formControl.errors).toEqual(expectedErrors); | ||
}); | ||
|
||
it('should allow a date equal to the min date', () => { | ||
fixture.componentInstance.formControl.setValue('1/1/2022'); | ||
fixture.detectChanges(); | ||
|
||
expect(fixture.componentInstance.formControl.errors).toBeNull(); | ||
}); | ||
|
||
it('should allow a date between the min and max dates', () => { | ||
fixture.componentInstance.formControl.setValue('6/1/2022'); | ||
fixture.detectChanges(); | ||
|
||
expect(fixture.componentInstance.formControl.errors).toBeNull(); | ||
}); | ||
|
||
it('should allow a date equal to the max date', () => { | ||
fixture.componentInstance.formControl.setValue('12/31/2022'); | ||
fixture.detectChanges(); | ||
|
||
expect(fixture.componentInstance.formControl.errors).toBeNull(); | ||
}); | ||
|
||
it('should not allow a date greater than the max date', () => { | ||
fixture.componentInstance.formControl.setValue('1/1/2023'); | ||
fixture.detectChanges(); | ||
|
||
const expectedErrors = { | ||
max: { max: '12/31/2022', actual: '1/1/2023' }, | ||
}; | ||
|
||
expect(fixture.componentInstance.formControl.errors).toEqual(expectedErrors); | ||
}); | ||
}); | ||
} |
34 changes: 34 additions & 0 deletions
34
projects/angular/src/forms/datepicker/date-input.validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2016-2022 VMware, Inc. All Rights Reserved. | ||
* This software is released under MIT license. | ||
* The full license information can be found in LICENSE in the root directory of this project. | ||
*/ | ||
|
||
import { Directive, Optional } from '@angular/core'; | ||
import { AbstractControl, NG_VALIDATORS, ValidationErrors, Validator } from '@angular/forms'; | ||
|
||
import { DateIOService } from './providers/date-io.service'; | ||
|
||
@Directive({ | ||
selector: '[clrDate]', | ||
providers: [{ provide: NG_VALIDATORS, useExisting: ClrDateInputValidator, multi: true }], | ||
}) | ||
export class ClrDateInputValidator implements Validator { | ||
constructor(@Optional() private dateIOService: DateIOService) {} | ||
|
||
validate(control: AbstractControl): ValidationErrors { | ||
if (this.dateIOService) { | ||
const value = this.dateIOService.getDateValueFromDateString(control.value); | ||
const minDate = this.dateIOService.disabledDates.minDate.toDate(); | ||
const maxDate = this.dateIOService.disabledDates.maxDate.toDate(); | ||
|
||
if (value && value < this.dateIOService.disabledDates.minDate.toDate()) { | ||
return { min: { min: minDate.toLocaleDateString(), actual: value.toLocaleDateString() } }; | ||
} else if (value && value > this.dateIOService.disabledDates.maxDate.toDate()) { | ||
return { max: { max: maxDate.toLocaleDateString(), actual: value.toLocaleDateString() } }; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters