placeholder="Name..."
formControlName="bid"
[readonly]="editing"
- autofocus>
+ [autofocus]="!editing">
<span class="invalid-feedback"
*ngIf="bucketForm.showError('bid', frm, 'required')"
i18n>This field is required.</span>
<select id="owner"
name="owner"
class="form-control custom-select"
- formControlName="owner">
+ formControlName="owner"
+ [autofocus]="editing">
<option i18n
*ngIf="owners === null"
[ngValue]="null">Loading...</option>
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
+import * as _ from 'lodash';
import { ToastrModule } from 'ngx-toastr';
import { of as observableOf } from 'rxjs';
spyOn(notificationService, 'show');
});
+ it('should validate name', () => {
+ component.editing = false;
+ component.createForm();
+ const control = component.bucketForm.get('bid');
+ expect(_.isFunction(control.asyncValidator)).toBeTruthy();
+ });
+
+ it('should not validate name', () => {
+ component.editing = true;
+ component.createForm();
+ const control = component.bucketForm.get('bid');
+ expect(control.asyncValidator).toBeNull();
+ });
+
it('tests create success notification', () => {
spyOn(rgwBucketService, 'create').and.returnValue(observableOf([]));
component.editing = false;
createForm() {
this.bucketForm = this.formBuilder.group({
id: [null],
- bid: [null, [Validators.required], [this.bucketNameValidator()]],
+ bid: [null, [Validators.required], this.editing ? [] : [this.bucketNameValidator()]],
owner: [null, [Validators.required]],
'placement-target': [null, this.editing ? [] : [Validators.required]]
});
@Component({
template: `
<form>
- <input id="x" type="checkbox" autofocus />
+ <input id="x" type="checkbox" [autofocus]="edit" />
<input id="y" type="text" />
</form>
`
})
-export class CheckboxFormComponent {}
+export class CheckboxFormComponent {
+ public edit = true;
+}
+
+@Component({
+ template: `
+ <form>
+ <input id="x" type="text" [autofocus]="foo" />
+ </form>
+ `
+})
+export class TextFormComponent {
+ foo() {
+ return false;
+ }
+}
describe('AutofocusDirective', () => {
configureTestBed({
- declarations: [AutofocusDirective, CheckboxFormComponent, PasswordFormComponent]
+ declarations: [
+ AutofocusDirective,
+ CheckboxFormComponent,
+ PasswordFormComponent,
+ TextFormComponent
+ ]
});
it('should create an instance', () => {
const element = document.getElementById('x');
expect(element === document.activeElement).toBeTruthy();
});
+
+ it('should not focus the text form field', () => {
+ const fixture: ComponentFixture<TextFormComponent> = TestBed.createComponent(TextFormComponent);
+ fixture.detectChanges();
+ const focused = fixture.debugElement.query(By.css(':focus'));
+ expect(focused).toBeNull();
+ const element = document.getElementById('x');
+ expect(element !== document.activeElement).toBeTruthy();
+ });
});
-import { AfterViewInit, Directive, ElementRef } from '@angular/core';
+import { AfterViewInit, Directive, ElementRef, Input } from '@angular/core';
import * as _ from 'lodash';
selector: '[autofocus]' // tslint:disable-line
})
export class AutofocusDirective implements AfterViewInit {
+ private focus = true;
+
constructor(private elementRef: ElementRef) {}
ngAfterViewInit() {
const el: HTMLInputElement = this.elementRef.nativeElement;
- if (_.isFunction(el.focus)) {
+ if (this.focus && _.isFunction(el.focus)) {
el.focus();
}
}
+
+ @Input()
+ public set autofocus(condition: any) {
+ if (_.isBoolean(condition)) {
+ this.focus = condition;
+ } else if (_.isFunction(condition)) {
+ this.focus = condition();
+ }
+ }
}