Skip to content

Commit 90c9c6e

Browse files
committed
Release v 1.2.2: Have added the prop initialFocusField for focusing on the appropriate input when the component has been appeared.
1 parent 441f705 commit 90c9c6e

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Code/pincode input component for angular
22

3-
Code (number/chars) input component for Angular 7, 8, 9 + projects.<br />
3+
Robust and <b>tested</b> code (number/chars) input component for Angular 7, 8, 9, 10 + projects.<br />
44
Ionic 4, 5 + is supported, can be used in iOS and Android.<br />
5-
Clipboard events are supported.
5+
<b>Clipboard</b> events are supported.
66

77
Preview
88

@@ -12,9 +12,9 @@ Preview
1212

1313
## Supported platforms
1414

15-
<b>Angular</b> 7, 8, 9 +<br />
15+
<b>Angular</b> 7, 8, 9, 10 +<br />
1616
<b>Ionic</b> 4, 5 +<br />
17-
Mobile browsers and WebViews on: <b>Android and iOS</b><br />
17+
Mobile browsers and WebViews on: <b>Android</b> and <b>iOS</b><br />
1818
Desktop browsers: <b>Chrome, Firefox, Safari, Edge v.79 +</b><br />
1919
Other browsers: <b>Edge v.41 - 44</b> (without code hidden feature)
2020

@@ -116,6 +116,7 @@ Example with only bottom borders:
116116
| <b>`isNonDigitsCode`</b> | boolean | false | When `true` inputted code can contain any char and not only digits from 0 to 9. If the input parameter <b>`code`</b> contains non digits chars and `isNonDigitsCode` is `false` the value will be ignored |
117117
| <b>`isPrevFocusableAfterClearing`</b> | boolean | true | When `true` after the input value deletion the caret will be moved to the previous input immediately. If `false` then after the input value deletion the caret will stay on the current input and be moved to the previous input only if the current input is empty |
118118
| <b>`isFocusingOnLastByClickIfFilled`</b> | boolean | false | When `true` and the code is filled then the focus will be moved to the last input element when clicked |
119+
| <b>`initialFocusField`</b> | number | - | The index of the input box for initial focusing. When the component will appear the focus will be placed on the input with this index. <br/> Note: If you need to dynamically hide the component it is needed to use <b>*ngIf</b> directive instead of the `[hidden]` attribute. |
119120
| <b>`code`</b> | string / number | - | The input code value for the component. If the parameter contains non digits chars and `isNonDigitsCode` is `false` the value will be <b>ignored |
120121

121122
#### Events

angular-code-input/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.2.2 (04.08.2020)
2+
**Note:** Have added the prop initialFocusField for focusing on the appropriate input when
3+
the component has been appeared.
4+
15
# 1.2.1 (20.06.2020)
26
**Note:** Have added the prop isFocusingOnLastByClickIfFilled for focusing on the last input box
37
if the code is full, and any input box clicked.

angular-code-input/README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Code/pincode input component for angular
22

3-
Code (number/chars) input component for Angular 7, 8, 9 + projects.<br />
3+
Robust and <b>tested</b> code (number/chars) input component for Angular 7, 8, 9, 10 + projects.<br />
44
Ionic 4, 5 + is supported, can be used in iOS and Android.<br />
5-
Clipboard events are supported.
5+
<b>Clipboard</b> events are supported.
66

77
Preview
88

@@ -12,9 +12,9 @@ Preview
1212

1313
## Supported platforms
1414

15-
<b>Angular</b> 7, 8, 9 +<br />
15+
<b>Angular</b> 7, 8, 9, 10 +<br />
1616
<b>Ionic</b> 4, 5 +<br />
17-
Mobile browsers and WebViews on: <b>Android and iOS</b><br />
17+
Mobile browsers and WebViews on: <b>Android</b> and <b>iOS</b><br />
1818
Desktop browsers: <b>Chrome, Firefox, Safari, Edge v.79 +</b><br />
1919
Other browsers: <b>Edge v.41 - 44</b> (without code hidden feature)
2020

@@ -116,6 +116,7 @@ Example with only bottom borders:
116116
| <b>`isNonDigitsCode`</b> | boolean | false | When `true` inputted code can contain any char and not only digits from 0 to 9. If the input parameter <b>`code`</b> contains non digits chars and `isNonDigitsCode` is `false` the value will be ignored |
117117
| <b>`isPrevFocusableAfterClearing`</b> | boolean | true | When `true` after the input value deletion the caret will be moved to the previous input immediately. If `false` then after the input value deletion the caret will stay on the current input and be moved to the previous input only if the current input is empty |
118118
| <b>`isFocusingOnLastByClickIfFilled`</b> | boolean | false | When `true` and the code is filled then the focus will be moved to the last input element when clicked |
119+
| <b>`initialFocusField`</b> | number | - | The index of the input box for initial focusing. When the component will appear the focus will be placed on the input with this index. <br/> Note: If you need to dynamically hide the component it is needed to use <b>*ngIf</b> directive instead of the `[hidden]` attribute. |
119120
| <b>`code`</b> | string / number | - | The input code value for the component. If the parameter contains non digits chars and `isNonDigitsCode` is `false` the value will be <b>ignored |
120121

121122
#### Events

angular-code-input/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-code-input",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "Code or pin code input for Angular 7, 8, 9 + / Ionic 4, 5 + projects",
55
"keywords": ["angular", "pincode", "angular-pincode", "otp", "code-input", "angular-otp", "ionic-otp", "ionic-code-input", "ionic-pincode"],
66
"author": "Alexander Dmitrenko",

angular-code-input/src/lib/code-input.component.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
AfterViewChecked,
23
AfterViewInit,
34
Component,
45
ElementRef,
@@ -22,7 +23,7 @@ enum InputState {
2223
templateUrl: 'code-input.component.html',
2324
styleUrls: ['./code-input.component.scss']
2425
})
25-
export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges {
26+
export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges, AfterViewChecked {
2627

2728
@ViewChildren('input') inputsList: QueryList<ElementRef>;
2829

@@ -32,6 +33,7 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges {
3233
@Input() readonly isPrevFocusableAfterClearing = true;
3334
@Input() readonly isFocusingOnLastByClickIfFilled = false;
3435
@Input() readonly inputType = 'tel';
36+
@Input() readonly initialFocusField?: number;
3537
@Input() readonly code?: string | number;
3638

3739
@Output() codeChanged = new EventEmitter<string>();
@@ -41,6 +43,10 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges {
4143

4244
private inputs: HTMLInputElement[] = [];
4345
private inputsStates: InputState[] = [];
46+
private state = {
47+
isFocusingAfterAppearingCompleted: false,
48+
isInitialFocusFieldEnabled: false
49+
};
4450

4551
constructor() {
4652
}
@@ -51,6 +57,7 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges {
5157

5258
ngOnInit(): void {
5359
this.placeHolders = Array(this.codeLength).fill(1);
60+
this.state.isInitialFocusFieldEnabled = !this.isEmpty(this.initialFocusField);
5461
}
5562

5663
ngAfterViewInit(): void {
@@ -63,6 +70,10 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges {
6370
this.onInputCodeChanges();
6471
}
6572

73+
ngAfterViewChecked(): void {
74+
this.focusOnInputAfterAppearing();
75+
}
76+
6677
ngOnChanges(changes: SimpleChanges): void {
6778
if (changes.code) {
6879
this.onInputCodeChanges();
@@ -224,6 +235,19 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges {
224235
});
225236
}
226237

238+
private focusOnInputAfterAppearing(): void {
239+
if (!this.state.isInitialFocusFieldEnabled) {
240+
return;
241+
}
242+
243+
if (this.state.isFocusingAfterAppearingCompleted) {
244+
return;
245+
}
246+
247+
this.inputs[this.initialFocusField].focus();
248+
this.state.isFocusingAfterAppearingCompleted = document.activeElement === this.inputs[this.initialFocusField];
249+
}
250+
227251
private emitChanges(): void {
228252
setTimeout(() => this.emitCode(), 50);
229253
}

0 commit comments

Comments
 (0)