fix(input): pass disabled down to input when it is set from form

- test modified to disable username and comments on `input/form-inputs`
and then toggle them via a button

see: http://g.recordit.co/RkN510TcHk.gif

fixes #9834
This commit is contained in:
Brandy Carney
2017-01-13 11:45:00 -05:00
committed by Adam Bradley
parent 21667c61ac
commit 5844a83343
3 changed files with 37 additions and 17 deletions

View File

@ -80,8 +80,8 @@ import { Platform } from '../../platform/platform';
@Component({ @Component({
selector: 'ion-input,ion-textarea', selector: 'ion-input,ion-textarea',
template: template:
'<input [(ngModel)]="_value" [type]="type" (blur)="inputBlurred($event)" (focus)="inputFocused($event)" [placeholder]="placeholder" class="text-input" [ngClass]="\'text-input-\' + _mode" *ngIf="_type!==\'textarea\'" #input>' + '<input [(ngModel)]="_value" [type]="type" (blur)="inputBlurred($event)" (focus)="inputFocused($event)" [placeholder]="placeholder" [disabled]="disabled" class="text-input" [ngClass]="\'text-input-\' + _mode" *ngIf="_type!==\'textarea\'" #input>' +
'<textarea [(ngModel)]="_value" (blur)="inputBlurred($event)" (focus)="inputFocused($event)" [placeholder]="placeholder" class="text-input" [ngClass]="\'text-input-\' + _mode" *ngIf="_type===\'textarea\'" #textarea></textarea>' + '<textarea [(ngModel)]="_value" (blur)="inputBlurred($event)" (focus)="inputFocused($event)" [placeholder]="placeholder" [disabled]="disabled" class="text-input" [ngClass]="\'text-input-\' + _mode" *ngIf="_type===\'textarea\'" #textarea></textarea>' +
'<input [type]="type" aria-hidden="true" next-input *ngIf="_useAssist">' + '<input [type]="type" aria-hidden="true" next-input *ngIf="_useAssist">' +
'<button ion-button clear [hidden]="!clearInput" type="button" class="text-input-clear-icon" (click)="clearTextInput()" (mousedown)="clearTextInput()"></button>' + '<button ion-button clear [hidden]="!clearInput" type="button" class="text-input-clear-icon" (click)="clearTextInput()" (mousedown)="clearTextInput()"></button>' +
'<div (touchstart)="pointerStart($event)" (touchend)="pointerEnd($event)" (mousedown)="pointerStart($event)" (mouseup)="pointerEnd($event)" class="input-cover" tappable *ngIf="_useAssist"></div>', '<div (touchstart)="pointerStart($event)" (touchend)="pointerEnd($event)" (mousedown)="pointerStart($event)" (mouseup)="pointerEnd($event)" class="input-cover" tappable *ngIf="_useAssist"></div>',
@ -120,7 +120,7 @@ export class TextInput extends Ion implements IonicFormInput {
@Optional() private _content: Content, @Optional() private _content: Content,
@Optional() private _item: Item, @Optional() private _item: Item,
@Optional() nav: NavController, @Optional() nav: NavController,
@Optional() ngControl: NgControl, @Optional() public ngControl: NgControl,
private _dom: DomController private _dom: DomController
) { ) {
super(config, elementRef, renderer, 'input'); super(config, elementRef, renderer, 'input');
@ -212,7 +212,7 @@ export class TextInput extends Ion implements IonicFormInput {
*/ */
@Input() @Input()
get disabled() { get disabled() {
return this._disabled; return this.ngControl ? this.ngControl.disabled : this._disabled;
} }
set disabled(val: boolean) { set disabled(val: boolean) {
this.setDisabled(this._disabled = isTrueProperty(val)); this.setDisabled(this._disabled = isTrueProperty(val));
@ -293,6 +293,7 @@ export class TextInput extends Ion implements IonicFormInput {
setNativeInput(nativeInput: NativeInput) { setNativeInput(nativeInput: NativeInput) {
this._native = nativeInput; this._native = nativeInput;
nativeInput.setValue(this._value); nativeInput.setValue(this._value);
nativeInput.isDisabled(this.disabled);
if (this._item && this._item.labelId !== null) { if (this._item && this._item.labelId !== null) {
nativeInput.labelledBy(this._item.labelId); nativeInput.labelledBy(this._item.labelId);

View File

@ -18,11 +18,6 @@ export class E2EPage {
comments: '' comments: ''
}; };
user = {
username: 'asdf',
password: '82'
};
submitted: boolean = false; submitted: boolean = false;
isTextAreaDisabled: boolean; isTextAreaDisabled: boolean;
@ -39,8 +34,10 @@ export class E2EPage {
}); });
this.userForm = fb.group({ this.userForm = fb.group({
username: ['', Validators.required], email: ['', Validators.required],
password: ['', Validators.required], username: [{value: 'administrator', disabled: true}, Validators.required],
password: [{value: 'password', disabled: false}, Validators.required],
comments: [{value: 'Comments are disabled', disabled: true}, Validators.required]
}); });
} }
@ -52,7 +49,7 @@ export class E2EPage {
} }
} }
submit(ev: UIEvent, value: any) { submit(ev: UIEvent, value?: any) {
console.log('Submitted', value); console.log('Submitted', value);
this.submitted = true; this.submitted = true;
} }
@ -61,6 +58,14 @@ export class E2EPage {
this.isTextAreaDisabled = !this.isTextAreaDisabled; this.isTextAreaDisabled = !this.isTextAreaDisabled;
} }
toggleDisable() {
let userNameCtrl = this.userForm.get('username');
userNameCtrl.enabled ? userNameCtrl.disable() : userNameCtrl.enable();
let commentsCtrl = this.userForm.get('comments');
commentsCtrl.enabled ? commentsCtrl.disable() : commentsCtrl.enable();
}
} }
@Component({ @Component({

View File

@ -54,24 +54,38 @@
</ion-list> </ion-list>
</form> </form>
<form [formGroup]="userForm" (ngSubmit)="submit($event, user)" #lf="ngForm"> <form [formGroup]="userForm" (ngSubmit)="submit($event, userForm)" #lf="ngForm">
<ion-list> <ion-list>
<ion-list-header>
Form w/ disabled inputs
</ion-list-header>
<ion-item>
<ion-label floating>Email</ion-label>
<ion-input type="email" formControlName="email"></ion-input>
</ion-item>
<ion-item> <ion-item>
<ion-label floating>Username</ion-label> <ion-label floating>Username</ion-label>
<ion-input [(ngModel)]="user.username" formControlName="username" required></ion-input> <ion-input formControlName="username"></ion-input>
</ion-item> </ion-item>
<ion-item> <ion-item>
<ion-label floating>Password</ion-label> <ion-label floating>Password</ion-label>
<ion-input type="password" [(ngModel)]="user.password" formControlName="password" required></ion-input> <ion-input type="password" formControlName="password"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Comments</ion-label>
<ion-textarea formControlName="comments"></ion-textarea>
</ion-item> </ion-item>
<div padding-left padding-right> <div padding-left padding-right>
<button ion-button block type="submit">Login</button> <button ion-button block type="submit">Login</button>
<button ion-button block type="button" color="secondary" (click)="toggleDisable()">Disable (toggle)</button>
</div> </div>
<div padding-left> <div padding-left>
<b>Valid form?:</b> {{ lf.form.valid }}<br> <b>Valid form?:</b> {{ lf.form.valid }}<br>
<b>Submitted form?:</b> {{ submitted }}<br> <b>Submitted form?:</b> {{ submitted }}<br>
<b>Username:</b> {{ user.username }}<br> <b>Email:</b> {{ userForm.controls.email.value }}<br>
<b>Password:</b> {{ user.password }}<br> <b>Username:</b> {{ userForm.controls.username.value }}<br>
<b>Password:</b> {{ userForm.controls.password.value }}<br>
<b>Comments:</b> {{ userForm.controls.comments.value }}<br>
</div> </div>
</ion-list> </ion-list>
</form> </form>