Skip to content
Permalink
Browse files

feat(upgrade): propagate touched state of NgModelController

  • Loading branch information...
Sergey Shevchenko authored and alxhub committed Jul 6, 2017
1 parent e03adb9 commit 59c23c7bd798b00be8e22536896eb1852c29fdc9
@@ -90,6 +90,9 @@ export function hookupNgModel(ngModel: angular.INgModelController, component: an
if (ngModel && supportsNgModel(component)) {
ngModel.$render = () => { component.writeValue(ngModel.$viewValue); };
component.registerOnChange(ngModel.$setViewValue.bind(ngModel));
if (typeof component.registerOnTouched === 'function') {
component.registerOnTouched(ngModel.$setTouched.bind(ngModel));
}
}
}

@@ -478,9 +478,12 @@ export function main() {
class Ng2 {
private _value: any = '';
private _onChangeCallback: (_: any) => void = () => {};
private _onTouchedCallback: () => void = () => {};
constructor() { ng2Instance = this; }
writeValue(value: any) { this._value = value; }
registerOnChange(fn: any) { this._onChangeCallback = fn; }
registerOnTouched(fn: any) { this._onTouchedCallback = fn; }
doTouch() { this._onTouchedCallback(); }
doChange(newValue: string) {
this._value = newValue;
this._onChangeCallback(newValue);
@@ -509,6 +512,13 @@ export function main() {
expect($rootScope.modelA).toBe('C');
expect(multiTrim(document.body.textContent)).toEqual('C | C');

const downgradedElement = <Element>document.body.querySelector('ng2');
expect(downgradedElement.classList.contains('ng-touched')).toBe(false);

ng2Instance.doTouch();
$rootScope.$apply();
expect(downgradedElement.classList.contains('ng-touched')).toBe(true);

ref.dispose();
});
}));
@@ -341,9 +341,12 @@ export function main() {
class Ng2 {
private _value: any = '';
private _onChangeCallback: (_: any) => void = () => {};
private _onTouchedCallback: () => void = () => {};
constructor() { ng2Instance = this; }
writeValue(value: any) { this._value = value; }
registerOnChange(fn: any) { this._onChangeCallback = fn; }
registerOnTouched(fn: any) { this._onTouchedCallback = fn; }
doTouch() { this._onTouchedCallback(); }
doChange(newValue: string) {
this._value = newValue;
this._onChangeCallback(newValue);
@@ -374,6 +377,13 @@ export function main() {
ng2Instance.doChange('C');
expect($rootScope.modelA).toBe('C');
expect(multiTrim(document.body.textContent)).toEqual('C | C');

const downgradedElement = <Element>document.body.querySelector('ng2');
expect(downgradedElement.classList.contains('ng-touched')).toBe(false);

ng2Instance.doTouch();
$rootScope.$apply();
expect(downgradedElement.classList.contains('ng-touched')).toBe(true);
});
}));

0 comments on commit 59c23c7

Please sign in to comment.
You can’t perform that action at this time.