Skip to content

Commit b173581

Browse files
authored
Add autofocus to search, checkbox & button (#23)
* Add autofocus option to search component * Add autofocus directive to help with autofocussing components
1 parent d2a56f8 commit b173581

File tree

11 files changed

+43
-9
lines changed

11 files changed

+43
-9
lines changed

projects/angular-components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@frankframework/angular-components",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"description": "A collection of reusable components designed for use in Frank!Framework projects",
55
"main": "",
66
"author": "Vivy Booman",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { AutoFocusDirective } from './auto-focus.directive';
2+
3+
describe('AutoFocusDirective', () => {
4+
it('should create an instance', () => {
5+
const directive = new AutoFocusDirective();
6+
expect(directive).toBeTruthy();
7+
});
8+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Directive, ElementRef, inject, AfterViewInit, Input, booleanAttribute } from '@angular/core';
2+
3+
@Directive({
4+
selector: '[ffAutoFocus]',
5+
})
6+
export class AutoFocusDirective implements AfterViewInit {
7+
@Input({ transform: booleanAttribute }) ffAutoFocus: boolean = false;
8+
private element: ElementRef = inject(ElementRef);
9+
10+
ngAfterViewInit(): void {
11+
if (this.ffAutoFocus) {
12+
this.element.nativeElement.focus();
13+
}
14+
}
15+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<button [disabled]="disabled" (click)="toggle()" [ngClass]="{ active: active }">
1+
<button [disabled]="disabled" (click)="toggle()" [ngClass]="{ active: active }" [ffAutoFocus]="autofocus">
22
<ng-content></ng-content>
33
</button>

projects/angular-components/src/lib/button/button.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { CommonModule } from '@angular/common';
22
import { booleanAttribute, Component, EventEmitter, Input, Output } from '@angular/core';
3+
import { AutoFocusDirective } from '../auto-focus.directive';
34

45
@Component({
56
selector: 'ff-button',
67
standalone: true,
7-
imports: [CommonModule],
8+
imports: [CommonModule, AutoFocusDirective],
89
templateUrl: './button.component.html',
910
styleUrl: './button.component.scss',
1011
})
1112
export class ButtonComponent {
1213
@Input({ transform: booleanAttribute }) disabled: boolean = false;
1314
@Input({ transform: booleanAttribute }) toggleable: boolean = false;
1415
@Input({ transform: booleanAttribute }) active: boolean = false;
16+
@Input({ transform: booleanAttribute }) autofocus: boolean = false;
1517
@Output() activeChange: EventEmitter<boolean> = new EventEmitter<boolean>();
1618

1719
protected toggle(): void {

projects/angular-components/src/lib/checkbox/checkbox.component.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<label (click)="_onClick($event)">
22
<span class="content"><ng-content></ng-content></span>
3-
<input type="checkbox" [disabled]="disabled" [(ngModel)]="checked" (ngModelChange)="_onChange(this.checked)" />
3+
<input
4+
type="checkbox"
5+
[disabled]="disabled"
6+
[(ngModel)]="checked"
7+
(ngModelChange)="_onChange(this.checked)"
8+
[ffAutoFocus]="autofocus"
9+
/>
410
<span class="check-box"
511
><ff-icon-check width="18" height="18" [colour]="colour" class="check-mark"></ff-icon-check
612
></span>

projects/angular-components/src/lib/checkbox/checkbox.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { booleanAttribute, ChangeDetectionStrategy, Component, forwardRef, Input
22
import { IconCheckComponent } from '../icons/icon-check/icon-check.component';
33
import { ControlValueAccessor, FormsModule, NG_VALUE_ACCESSOR } from '@angular/forms';
44
import { noop } from 'rxjs';
5+
import { AutoFocusDirective } from '../auto-focus.directive';
56

67
export const FF_CHECKBOX_CONTROL_VALUE_ACCESSOR = {
78
provide: NG_VALUE_ACCESSOR,
@@ -12,7 +13,7 @@ export const FF_CHECKBOX_CONTROL_VALUE_ACCESSOR = {
1213
@Component({
1314
selector: 'ff-checkbox',
1415
standalone: true,
15-
imports: [FormsModule, IconCheckComponent],
16+
imports: [FormsModule, IconCheckComponent, AutoFocusDirective],
1617
templateUrl: './checkbox.component.html',
1718
styleUrl: './checkbox.component.scss',
1819
changeDetection: ChangeDetectionStrategy.OnPush,
@@ -21,6 +22,7 @@ export const FF_CHECKBOX_CONTROL_VALUE_ACCESSOR = {
2122
export class CheckboxComponent implements ControlValueAccessor {
2223
@Input({ transform: booleanAttribute }) disabled: boolean = false;
2324
@Input({ transform: booleanAttribute }) checked: boolean = false;
25+
@Input({ transform: booleanAttribute }) autofocus: boolean = false;
2426
@Input() colour: string = '#000';
2527
// @Input() backgroundColour: string = '#FDC300';
2628

projects/angular-components/src/lib/search/search.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
(blur)="_onBlur()"
99
(input)="_onInputEvent()"
1010
(change)="_onInteractionEvent($event)"
11+
[ffAutoFocus]="autofocus"
1112
#input
1213
/>
1314
@if (focusKeyEnabled && !disabled) {

projects/angular-components/src/lib/search/search.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { IconMagnifierComponent } from '../icons/icon-magnifier/icon-magnifier.c
1717
import { debounceTime, noop, Subject } from 'rxjs';
1818
import { NgClass } from '@angular/common';
1919
import { FocusOnKeyUtil } from '../utils/focus-on-key.util';
20+
import { AutoFocusDirective } from '../auto-focus.directive';
2021

2122
export const SEARCH_CONTROL_VALUE_ACCESSOR = {
2223
provide: NG_VALUE_ACCESSOR,
@@ -27,7 +28,7 @@ export const SEARCH_CONTROL_VALUE_ACCESSOR = {
2728
@Component({
2829
selector: 'ff-search',
2930
standalone: true,
30-
imports: [FormsModule, NgClass, IconMagnifierComponent],
31+
imports: [FormsModule, NgClass, IconMagnifierComponent, AutoFocusDirective],
3132
templateUrl: './search.component.html',
3233
styleUrl: './search.component.scss',
3334
changeDetection: ChangeDetectionStrategy.OnPush,
@@ -36,6 +37,7 @@ export const SEARCH_CONTROL_VALUE_ACCESSOR = {
3637
export class SearchComponent implements OnInit, AfterViewInit, OnDestroy, ControlValueAccessor {
3738
@Input() placeholder: string = 'Search...';
3839
@Input() focusKey: string = '/';
40+
@Input({ transform: booleanAttribute }) autofocus: boolean = false;
3941
@Input({ transform: booleanAttribute }) forceFocus: boolean = false;
4042
@Input({ transform: booleanAttribute }) focusKeyEnabled: boolean = true;
4143
@Input({ transform: booleanAttribute }) slim: boolean = false;

src/app/app.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ <h2>Checkbox</h2>
2424
<li>
2525
<h2>Search</h2>
2626
<div class="components">
27-
<ff-search [(ngModel)]="searchText" style="width: 100%" forceFocus></ff-search>
27+
<ff-search [(ngModel)]="searchText" style="width: 100%" forceFocus autofocus></ff-search>
2828
<div class="separator">
2929
<ff-search placeholder="Test the focus key" focusKey="=" style="width: 100%"></ff-search>
3030
<ff-search placeholder="Disabled" style="width: 100%" disabled></ff-search>

src/app/app.component.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ export class AppComponent implements OnInit {
2828
{ name: 'something', displayName: 'Something', property: null, html: true },
2929
];
3030

31-
constructor() {}
32-
3331
ngOnInit(): void {
3432
this.datasource.data = [
3533
{

0 commit comments

Comments
 (0)