trigger focus on first element of extensible form

pull/9392/head
mehmet-erim 4 years ago
parent 702c51600c
commit f26c9380e0

@ -2,6 +2,7 @@
<ng-template ngSwitchCase="input"> <ng-template ngSwitchCase="input">
<ng-template [ngTemplateOutlet]="label"></ng-template> <ng-template [ngTemplateOutlet]="label"></ng-template>
<input <input
#field
[id]="prop.id" [id]="prop.id"
[formControlName]="prop.name" [formControlName]="prop.name"
[autocomplete]="prop.autocomplete" [autocomplete]="prop.autocomplete"
@ -19,6 +20,7 @@
<ng-template ngSwitchCase="checkbox"> <ng-template ngSwitchCase="checkbox">
<div class="custom-checkbox custom-control" validationTarget> <div class="custom-checkbox custom-control" validationTarget>
<input <input
#field
[id]="prop.id" [id]="prop.id"
[formControlName]="prop.name" [formControlName]="prop.name"
[abpDisabled]="disabled" [abpDisabled]="disabled"
@ -35,6 +37,7 @@
<ng-template ngSwitchCase="select"> <ng-template ngSwitchCase="select">
<ng-template [ngTemplateOutlet]="label"></ng-template> <ng-template [ngTemplateOutlet]="label"></ng-template>
<select <select
#field
[id]="prop.id" [id]="prop.id"
[formControlName]="prop.name" [formControlName]="prop.name"
[abpDisabled]="disabled" [abpDisabled]="disabled"
@ -52,6 +55,7 @@
<ng-template ngSwitchCase="multiselect"> <ng-template ngSwitchCase="multiselect">
<ng-template [ngTemplateOutlet]="label"></ng-template> <ng-template [ngTemplateOutlet]="label"></ng-template>
<select <select
#field
[id]="prop.id" [id]="prop.id"
[formControlName]="prop.name" [formControlName]="prop.name"
[abpDisabled]="disabled" [abpDisabled]="disabled"
@ -71,6 +75,7 @@
<ng-template [ngTemplateOutlet]="label"></ng-template> <ng-template [ngTemplateOutlet]="label"></ng-template>
<div #typeahead class="position-relative" validationStyle validationTarget> <div #typeahead class="position-relative" validationStyle validationTarget>
<input <input
#field
[id]="prop.id" [id]="prop.id"
[autocomplete]="prop.autocomplete" [autocomplete]="prop.autocomplete"
[abpDisabled]="disabled" [abpDisabled]="disabled"
@ -116,6 +121,7 @@
<ng-template ngSwitchCase="textarea"> <ng-template ngSwitchCase="textarea">
<ng-template [ngTemplateOutlet]="label"></ng-template> <ng-template [ngTemplateOutlet]="label"></ng-template>
<textarea <textarea
#field
[id]="prop.id" [id]="prop.id"
[formControlName]="prop.name" [formControlName]="prop.name"
[abpDisabled]="disabled" [abpDisabled]="disabled"

@ -1,13 +1,22 @@
import { ABP, AbpValidators, ConfigStateService, TrackByService } from '@abp/ng.core';
import { import {
ABP,
AbpValidators,
AutofocusDirective,
ConfigStateService,
TrackByService,
} from '@abp/ng.core';
import {
AfterViewInit,
ChangeDetectionStrategy, ChangeDetectionStrategy,
ChangeDetectorRef, ChangeDetectorRef,
Component, Component,
ElementRef,
Input, Input,
OnChanges, OnChanges,
Optional, Optional,
SimpleChanges, SimpleChanges,
SkipSelf, SkipSelf,
ViewChild,
} from '@angular/core'; } from '@angular/core';
import { import {
ControlContainer, ControlContainer,
@ -43,11 +52,15 @@ import { addTypeaheadTextSuffix } from '../../utils/typeahead.util';
{ provide: NgbTimeAdapter, useClass: TimeAdapter }, { provide: NgbTimeAdapter, useClass: TimeAdapter },
], ],
}) })
export class ExtensibleFormPropComponent implements OnChanges { export class ExtensibleFormPropComponent implements OnChanges, AfterViewInit {
@Input() data: PropData; @Input() data: PropData;
@Input() prop: FormProp; @Input() prop: FormProp;
@Input() first: boolean;
@ViewChild('field') private fieldRef: ElementRef<HTMLElement>;
asterisk = ''; asterisk = '';
options$: Observable<ABP.Option<any>[]> = of([]); options$: Observable<ABP.Option<any>[]> = of([]);
@ -116,6 +129,13 @@ export class ExtensibleFormPropComponent implements OnChanges {
this.asterisk = this.validators.some(isRequired) ? '*' : ''; this.asterisk = this.validators.some(isRequired) ? '*' : '';
} }
ngAfterViewInit() {
if (this.first && this.fieldRef) {
const autofocusDirective = new AutofocusDirective(this.fieldRef);
autofocusDirective.ngAfterViewInit();
}
}
getComponent(prop: FormProp): string { getComponent(prop: FormProp): string {
switch (prop.type) { switch (prop.type) {
case ePropType.Boolean: case ePropType.Boolean:

@ -1,6 +1,6 @@
<ng-container *ngIf="form"> <ng-container *ngIf="form">
<ng-container *abpPropData="let data; fromList: propList; withRecord: record"> <ng-container *abpPropData="let data; fromList: propList; withRecord: record">
<ng-container *ngFor="let prop of propList; trackBy: track.by('name')"> <ng-container *ngFor="let prop of propList; let first = first; trackBy: track.by('name')">
<ng-container *ngIf="prop.visible(data)"> <ng-container *ngIf="prop.visible(data)">
<ng-container <ng-container
[formGroupName]="extraPropertiesKey" [formGroupName]="extraPropertiesKey"
@ -14,6 +14,7 @@
*ngIf="form.get(prop.name)" *ngIf="form.get(prop.name)"
[prop]="prop" [prop]="prop"
[data]="data" [data]="data"
[first]="first"
></abp-extensible-form-prop> ></abp-extensible-form-prop>
</ng-template> </ng-template>
</ng-container> </ng-container>

Loading…
Cancel
Save