This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 748
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NG] clrInput and form control validation components (#2295)
* Renamed FormControlService and WrappedFormControl * Added ClrInput, ClrInputContainer for tracking input control states * Added ClrControlHelper and ClrControlError for validation * Added ClrIfError directive to conditionally display error messages Signed-off-by: Jeremy Wilken <[email protected]>
- Loading branch information
1 parent
9a729b9
commit 4863786
Showing
52 changed files
with
1,046 additions
and
83 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2016-2018 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 { TestBed } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
import { ClrControlError } from './error'; | ||
|
||
@Component({ template: `<clr-control-error>Test error</clr-control-error>` }) | ||
class SimpleTest {} | ||
|
||
export default function(): void { | ||
describe('ClrControlError', () => { | ||
let fixture; | ||
|
||
beforeEach(function() { | ||
TestBed.configureTestingModule({ declarations: [ClrControlError, SimpleTest] }); | ||
fixture = TestBed.createComponent(SimpleTest); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('projects content', function() { | ||
expect(fixture.debugElement.query(By.directive(ClrControlError)).nativeElement.innerText).toContain('Test error'); | ||
}); | ||
|
||
it('adds the .clr-subtext class to host', function() { | ||
expect( | ||
fixture.debugElement.query(By.directive(ClrControlError)).nativeElement.classList.contains('clr-subtext') | ||
).toBeTrue(); | ||
}); | ||
}); | ||
} |
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,16 @@ | ||
/** | ||
* Copyright (c) 2016-2018 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'; | ||
|
||
@Component({ | ||
selector: 'clr-control-error', | ||
template: ` | ||
<ng-content></ng-content> | ||
`, | ||
host: { '[class.clr-subtext]': 'true' }, | ||
}) | ||
export class ClrControlError {} |
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,37 @@ | ||
/* | ||
* Copyright (c) 2016-2018 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 { TestBed } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
import { ClrControlHelper } from './helper'; | ||
|
||
@Component({ template: `<clr-control-helper>Test helper</clr-control-helper>` }) | ||
class SimpleTest {} | ||
|
||
export default function(): void { | ||
describe('ClrControlHelper', () => { | ||
let fixture; | ||
|
||
beforeEach(function() { | ||
TestBed.configureTestingModule({ declarations: [ClrControlHelper, SimpleTest] }); | ||
fixture = TestBed.createComponent(SimpleTest); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('projects content', function() { | ||
expect(fixture.debugElement.query(By.directive(ClrControlHelper)).nativeElement.innerText).toContain( | ||
'Test helper' | ||
); | ||
}); | ||
|
||
it('adds the .clr-subtext class to host', function() { | ||
expect( | ||
fixture.debugElement.query(By.directive(ClrControlHelper)).nativeElement.classList.contains('clr-subtext') | ||
).toBeTrue(); | ||
}); | ||
}); | ||
} |
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,16 @@ | ||
/** | ||
* Copyright (c) 2016-2018 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'; | ||
|
||
@Component({ | ||
selector: 'clr-control-helper', | ||
template: ` | ||
<ng-content></ng-content> | ||
`, | ||
host: { '[class.clr-subtext]': 'true' }, | ||
}) | ||
export class ClrControlHelper {} |
62 changes: 62 additions & 0 deletions
62
src/clr-angular/forms/common/if-error/if-error.service.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,62 @@ | ||
/* | ||
* Copyright (c) 2016-2018 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 { FormControl } from '@angular/forms'; | ||
|
||
import { NgControlService } from '../providers/ng-control.service'; | ||
|
||
import { IfErrorService } from './if-error.service'; | ||
|
||
export default function(): void { | ||
describe('IfErrorService', function() { | ||
let service, ngControlService, testControl; | ||
|
||
beforeEach(() => { | ||
testControl = new FormControl(true); | ||
ngControlService = new NgControlService(); | ||
service = new IfErrorService(ngControlService); | ||
}); | ||
|
||
it('subscribes to the statusChanges when the control is emitted', () => { | ||
spyOn(testControl.statusChanges, 'subscribe').and.callThrough(); | ||
ngControlService.setControl(testControl); | ||
expect(testControl.statusChanges.subscribe).toHaveBeenCalled(); | ||
}); | ||
|
||
it('provides observable for statusChanges, passing the control', () => { | ||
const cb = jasmine.createSpy('cb'); | ||
const sub = service.statusChanges.subscribe(control => cb(control)); | ||
ngControlService.setControl(testControl); | ||
// Change the state of the input to trigger statusChange | ||
testControl.markAsTouched(); | ||
testControl.updateValueAndValidity(); | ||
expect(cb).toHaveBeenCalled(); | ||
expect(cb).toHaveBeenCalledWith(testControl); | ||
sub.unsubscribe(); | ||
}); | ||
|
||
it('should allow a manual trigger of status observable', () => { | ||
const cb = jasmine.createSpy('cb'); | ||
const sub = service.statusChanges.subscribe(control => cb(control)); | ||
ngControlService.setControl(testControl); | ||
// Manually trigger status check | ||
service.triggerStatusChange(); | ||
expect(cb).toHaveBeenCalled(); | ||
expect(cb).toHaveBeenCalledWith(testControl); | ||
sub.unsubscribe(); | ||
}); | ||
|
||
it('should not fire status changes if control is untouched', () => { | ||
const cb = jasmine.createSpy('cb'); | ||
const sub = service.statusChanges.subscribe(control => cb(control)); | ||
ngControlService.setControl(testControl); | ||
// Change state of the input, should only fire when touched | ||
testControl.markAsDirty(); | ||
testControl.updateValueAndValidity(); | ||
expect(cb).not.toHaveBeenCalled(); | ||
sub.unsubscribe(); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.