[ACS-3701] Add auto focus directive (#7921)

* [ACS-3701] Add auto focus directive

* [ACS-3701] Linter fix

* [ACS-3701] Another lint fixes
This commit is contained in:
MichalKinas 2022-11-02 15:28:04 +01:00 committed by GitHub
parent 711eaa34a4
commit e45c6891e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 113 additions and 4 deletions

View File

@ -0,0 +1,20 @@
---
Title: Auto Focus directive
Added: v5.1.0
Status: Active
Last reviewed: 2022-10-25
---
# [Auto Focus directive](../../../lib/content-services/src/lib/directives/auto-focus.directive.ts "Defined in auto-focus.directive.ts")
Automatically focuses HTML element after content is initialized.
## Basic Usage
```html
<button adf-auto-focus>...</button>
```
## Class members
### Properties

View File

@ -26,7 +26,7 @@
</button> </button>
</div> </div>
<div> <div>
<button mat-button cdkFocusInitial (click)="onCancel()" id="aspect-list-dialog-actions-cancel" <button mat-button adf-auto-focus (click)="onCancel()" id="aspect-list-dialog-actions-cancel"
data-automation-id="aspect-list-dialog-actions-cancel">{{ data-automation-id="aspect-list-dialog-actions-cancel">{{
'ADF-ASPECT-LIST.DIALOG.CANCEL' | translate }} 'ADF-ASPECT-LIST.DIALOG.CANCEL' | translate }}
</button> </button>

View File

@ -28,6 +28,7 @@ import { AspectListDialogComponent } from './aspect-list-dialog.component';
import { MatButtonModule } from '@angular/material/button'; import { MatButtonModule } from '@angular/material/button';
import { MatTooltipModule } from '@angular/material/tooltip'; import { MatTooltipModule } from '@angular/material/tooltip';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { ContentDirectiveModule } from '../directives/content-directive.module';
@NgModule({ @NgModule({
imports: [ imports: [
@ -40,7 +41,8 @@ import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
MatDialogModule, MatDialogModule,
MatButtonModule, MatButtonModule,
MatTooltipModule, MatTooltipModule,
MatProgressSpinnerModule MatProgressSpinnerModule,
ContentDirectiveModule
], ],
exports: [ exports: [
AspectListComponent, AspectListComponent,

View File

@ -0,0 +1,52 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component } from '@angular/core';
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { AutoFocusDirective } from './auto-focus.directive';
import { By } from '@angular/platform-browser';
import { TranslateModule } from '@ngx-translate/core';
@Component({
template: `<div tabindex="0" adf-auto-focus> Test</div>`
})
class AutoFoucsTestComponent {}
describe('AutoFocusDirective', () => {
let fixture: ComponentFixture<AutoFoucsTestComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot()
],
declarations: [
AutoFocusDirective,
AutoFoucsTestComponent
]
});
fixture = TestBed.createComponent(AutoFoucsTestComponent);
});
it('should focus the element after content is initialized', fakeAsync(() => {
const autoFocusElement = fixture.debugElement.query(By.directive(AutoFocusDirective)).nativeElement;
spyOn(autoFocusElement, 'focus');
fixture.detectChanges();
tick(200);
expect(autoFocusElement.focus).toHaveBeenCalled();
}));
});

View File

@ -0,0 +1,31 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AfterContentInit, Directive, ElementRef } from '@angular/core';
@Directive({
selector: '[adf-auto-focus]'
})
export class AutoFocusDirective implements AfterContentInit {
public constructor(private el: ElementRef) {}
public ngAfterContentInit() {
setTimeout(() => {
this.el.nativeElement.focus();
}, 100);
}
}

View File

@ -22,6 +22,7 @@ import { TranslateModule } from '@ngx-translate/core';
import { NodeLockDirective } from './node-lock.directive'; import { NodeLockDirective } from './node-lock.directive';
import { NodeCounterComponent, NodeCounterDirective } from './node-counter.directive'; import { NodeCounterComponent, NodeCounterDirective } from './node-counter.directive';
import { AutoFocusDirective } from './auto-focus.directive';
@NgModule({ @NgModule({
imports: [ imports: [
@ -32,11 +33,13 @@ import { NodeCounterComponent, NodeCounterDirective } from './node-counter.direc
declarations: [ declarations: [
NodeLockDirective, NodeLockDirective,
NodeCounterDirective, NodeCounterDirective,
NodeCounterComponent NodeCounterComponent,
AutoFocusDirective
], ],
exports: [ exports: [
NodeLockDirective, NodeLockDirective,
NodeCounterDirective NodeCounterDirective,
AutoFocusDirective
] ]
}) })
export class ContentDirectiveModule { export class ContentDirectiveModule {

View File

@ -18,3 +18,4 @@
export * from './content-directive.module'; export * from './content-directive.module';
export * from './node-lock.directive'; export * from './node-lock.directive';
export * from './node-counter.directive'; export * from './node-counter.directive';
export * from './auto-focus.directive';