|
| 1 | +# Code/pincode input component for angular |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +Robust and <b>tested</b> code (number/chars) input component for Angular 7 - 16+ projects.<br /> |
| 9 | +Ionic 4 - 7+ is supported, can be used in iOS and Android.<br /> |
| 10 | +<b>Clipboard</b> events are supported. |
| 11 | + |
| 12 | +Star it to inspire us to build the best component! <img src="https://github.com/AlexMiniApps/angular-code-input/blob/master/star.jpg" alt="Star"/> |
| 13 | + |
| 14 | +Preview |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +## Supported platforms |
| 21 | + |
| 22 | +<b>Angular</b> 7 - 16+<br /> |
| 23 | +<b>Ionic</b> 4 - 7+<br /> |
| 24 | +Mobile browsers and WebViews on: <b>Android</b> and <b>iOS</b><br /> |
| 25 | +Desktop browsers: <b>Chrome, Firefox, Safari, Edge v.79 +</b><br /> |
| 26 | +Other browsers: <b>Edge v.41 - 44</b> (without code hidden feature) |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | + $ npm install --save angular-code-input |
| 31 | + |
| 32 | +Choose the version corresponding to your Angular version: |
| 33 | + |
| 34 | +| Angular | angular-code-input | |
| 35 | +|------------|--------------------| |
| 36 | +| 16+ | 2.x+ | |
| 37 | +| 7-15 | 1.x+ | |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | +Import `CodeInputModule` in your app module or page module: |
| 42 | + |
| 43 | +```ts |
| 44 | +import { CodeInputModule } from 'angular-code-input'; |
| 45 | + |
| 46 | +@NgModule({ |
| 47 | + imports: [ |
| 48 | + // ... |
| 49 | + CodeInputModule |
| 50 | + ] |
| 51 | +}) |
| 52 | +``` |
| 53 | + |
| 54 | +It is possible to configure the component across the app using the root config. In such case the import will look as follows: |
| 55 | +```ts |
| 56 | +import { CodeInputModule } from 'angular-code-input'; |
| 57 | + |
| 58 | +@NgModule({ |
| 59 | + imports: [ |
| 60 | + // ... |
| 61 | + CodeInputModule.forRoot({ |
| 62 | + codeLength: 6, |
| 63 | + isCharsCode: true, |
| 64 | + code: 'abcdef' |
| 65 | + }), |
| 66 | + ] |
| 67 | +}) |
| 68 | +``` |
| 69 | + |
| 70 | +Include the component on page template HTML: |
| 71 | + |
| 72 | +```html |
| 73 | + <code-input [isCodeHidden]="true" |
| 74 | + [codeLength]="5" |
| 75 | + (codeChanged)="onCodeChanged($event)" |
| 76 | + (codeCompleted)="onCodeCompleted($event)"> |
| 77 | +</code-input> |
| 78 | +``` |
| 79 | + |
| 80 | +Inside a page script: |
| 81 | + |
| 82 | +```ts |
| 83 | + // this called every time when user changed the code |
| 84 | +onCodeChanged(code: string) { |
| 85 | +} |
| 86 | + |
| 87 | +// this called only if user entered full code |
| 88 | +onCodeCompleted(code: string) { |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Configuration |
| 93 | + |
| 94 | +#### View |
| 95 | + |
| 96 | +It is possible to configure the component via CSS vars |
| 97 | +<br />or using `::ng-deep` (deprecated) angular CSS selector |
| 98 | +[::ng-deep](https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep) |
| 99 | + |
| 100 | +CSS vars: |
| 101 | + |
| 102 | +| CSS Var | Description | |
| 103 | +|--------------------------------------------------------------|--------------------------------------------------------| |
| 104 | +| `--text-security-type: disc;` | Text presentation type when the isCodeHidden is enabled | |
| 105 | +| `--item-spacing: 4px;` | Horizontal space between input items | |
| 106 | +| `--item-height: 4.375em;` | Height of input items | |
| 107 | +| `--item-border: 1px solid #dddddd;` | Border of input item for an empty value | |
| 108 | +| `--item-border-bottom: 1px solid #dddddd;` | Bottom border of input item for an empty value | |
| 109 | +| `--item-border-has-value: 1px solid #dddddd;` | Border of input item with a value | |
| 110 | +| `--item-border-bottom-has-value: 1px solid #dddddd;` | Bottom border of input item with a value | |
| 111 | +| `--item-border-focused: 1px solid #dddddd;` | Border of input item when focused | |
| 112 | +| `--item-border-bottom-focused: 1px solid #dddddd;` | Bottom border of input item when focused | |
| 113 | +| `--item-shadow-focused: 0px 1px 5px rgba(221, 221, 221, 1);` | Shadow of input item when focused | |
| 114 | +| `--item-border-radius: 5px;` | Border radius of input item | |
| 115 | +| `--item-background: transparent;` | Input item background | |
| 116 | +| `--item-font-weight: 300;` | Font weight of input item | |
| 117 | +| `--color: #171516;` | Text color of input items | |
| 118 | + |
| 119 | +Example with only bottom borders: |
| 120 | + |
| 121 | +```` |
| 122 | +/* inside page styles*/ |
| 123 | +... |
| 124 | + code-input { |
| 125 | + --item-spacing: 10px; |
| 126 | + --item-height: 3em; |
| 127 | + --item-border: none; |
| 128 | + --item-border-bottom: 2px solid #dddddd; |
| 129 | + --item-border-has-value: none; |
| 130 | + --item-border-bottom-has-value: 2px solid #888888; |
| 131 | + --item-border-focused: none; |
| 132 | + --item-border-bottom-focused: 2px solid #809070; |
| 133 | + --item-shadow-focused: none; |
| 134 | + --item-border-radius: 0px; |
| 135 | + } |
| 136 | +... |
| 137 | +```` |
| 138 | + |
| 139 | +#### Component options |
| 140 | + |
| 141 | +| Property | Type | Default | Description | |
| 142 | +|----------|:-------:|:-----:|----------| |
| 143 | +| <b>`codeLength`</b> | number | 4 | Length of input code | |
| 144 | +| <b>`inputType`</b> | string | tel | Type of the input DOM elements like `<input [type]="inputType"/>` default '`tel'` | |
| 145 | +| <b>`isCodeHidden`</b> | boolean | false | When `true` inputted code chars will be shown as asterisks (points) | |
| 146 | +| <b>`isCharsCode`</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 `isCharsCode` is `false` the value will be ignored | |
| 147 | +| <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 | |
| 148 | +| <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 | |
| 149 | +| <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 | |
| 150 | +| <b>`code`</b> | string / number | - | The input code value for the component. If the parameter contains non digits chars and `isCharsCode` is `false` the value will be <b>ignored</b> | |
| 151 | +| <b>`disabled`</b> | boolean | false | When `true` then the component will not handle user actions, like in regular html input element with the `disabled` attribute | |
| 152 | +| <b>`autocapitalize`</b> | string | - | The autocapitalize attribute is an enumerated attribute that controls whether and how text input is automatically capitalized as it is entered/edited by the user | |
| 153 | + |
| 154 | +#### Events |
| 155 | + |
| 156 | +| Event | Description | |
| 157 | +|----------|--------------------| |
| 158 | +| `codeChanged` | Will be called every time when a user changed the code | |
| 159 | +| `codeCompleted` | Will be called only if a user entered full code | |
| 160 | + |
| 161 | +## Methods |
| 162 | + |
| 163 | +For calling the component's methods it is required to access the component inside the template or page script. |
| 164 | +It can be reached as follows. |
| 165 | + |
| 166 | +Inside the page template HTML add a template ref: |
| 167 | + |
| 168 | +```html |
| 169 | +<code-input |
| 170 | + ... |
| 171 | + #codeInput |
| 172 | + ... |
| 173 | +> |
| 174 | +</code-input> |
| 175 | +``` |
| 176 | + |
| 177 | +Inside a page script attach the component: |
| 178 | + |
| 179 | +```ts |
| 180 | +... |
| 181 | +// adding to the imports |
| 182 | +import {CodeInputComponent} from 'angular-code-input'; |
| 183 | +... |
| 184 | +// adding to the page props |
| 185 | +@ViewChild('codeInput') codeInput !: CodeInputComponent; |
| 186 | +... |
| 187 | +// calling the component's methods somewhere in the page. |
| 188 | +// IMPORTANT: it will be accessible only after the view initialization! |
| 189 | +this.codeInput.reset(); |
| 190 | +``` |
| 191 | + |
| 192 | +| Method | Description | |
| 193 | +|----------------|--------------------| |
| 194 | +| <b>`focusOnField(index: number): void`</b> | Focuses the input caret on the input box with the passed index | |
| 195 | +| <b>`reset(isChangesEmitting = false): void`</b> | <p>Resets the component values in the following way:</p><p>if the `code` option is supplied then the value will be reset to the `code` option value. If the `code` option is not supplied then the component will be reset to empty values.</p><p>if the `initialFocusField` option is supplied then the caret will be focused in that filed after reset.</p><p>if the `isChangesEmitting` param is passed then changes will be emitted</p>| |
0 commit comments