0.0.4 • Published 11 months ago

ngx-otp-code-input v0.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
11 months ago

ngx-otp-code-input

ngx-otp-code-input is an Angular component for OTP (One-Time Password) input. This component is highly customizable, allowing for various configurations such as masking, integer-only input, autofocus, and more.

If you like the library, please consider giving it a ⭐ on GitHub.

Features

  • Customizable Length: Specify the number of OTP input fields.
  • Masking: Hide input values, useful for password-like behavior.
  • Integer Only: Restrict input to only integers.
  • Autofocus: Automatically focus on the first input field.
  • Read-only and Disabled States: Make the input fields read-only or disabled.
  • Tab Index: Control tab navigation between input fields.
  • Events: Emit events on OTP change and completion.
  • Success/Error Status Feedback: Visually indicate the result of OTP verification with customizable icons and colors.
  • Animated Transitions: Smooth transitions or animations when focusing between OTP input fields.

Installation

Install the package via npm:

  npm install ngx-otp-code-input

Usage

Import the Module

Add NgxOtpCodeInputModule to your Angular module:

import { NgxOtpCodeInputModule } from 'ngx-otp-code-input';

@NgModule({
  declarations: [
    // your components
  ],
  imports: [
    // other modules
    NgxOtpCodeInputModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Use the Component in Your Template

Add the ngx-otp-code-input component to your template:

<ngx-otp-code-input
  [length]="6"
  [mask]="false"
  [integerOnly]="false"
  [disabled]="false"
  variant="outline"
  [readonly]="false"
  [autofocus]="true"
  [animationConfig]="{ type: 'slide', duration: '1.0s' }" <!-- Optional: Configure animations -->
  [status]="otpStatus" <!-- Bind this to a variable in your component -->
  [successIcon]="'check_circle'" <!-- Optional: Bind to a custom icon for success -->
  [failureIcon]="'cancel'" <!-- Optional: Bind to a custom icon for failure -->
  [tabIndex]="true"
  (otpChange)="onOtpChange($event)"
  (otpComplete)="onOtpComplete($event)"
></ngx-otp-code-input>

Handle Events in Your Component

Add event handlers in your component:

export class AppComponent {
  otpStatus: 'success' | 'failed' | null = null;

  onOtpComplete(event: string): void {
    console.log('OTP Complete:', event);
  }

  onOtpChange(event: string): void {
    console.log('OTP Change:', event);
  }
}

Configuration

Inputs

InputTypeDefaultDescription
lengthnumber4Number of OTP input fields.
maskbooleanfalseHide the input values (like a password field).
integerOnlybooleanfalseRestrict input to only integer values.
disabledbooleanfalseDisable the input fields.
readonlybooleanfalseMake the input fields read-only.
autofocusbooleantrueAutomatically focus on the first input field.
tabIndexbooleanfalseEnable tab navigation between input fields.
inputClassstring''Custom CSS class for the input fields.
variantMatFormFieldAppearance'outline', 'fill'Appearance of the Material form field.
regexstring''Custom regex pattern for the input fields.
statusanynullVisual status of OTP verification.
successIconstring'check_circle'Custom icon for success status.
failureIconstring'error'Custom icon for failure status.
animationConfig{ type: string, duration: string }{ type: 'slide', duration: '1.0s' }Configuration for animations when focusing between input fields.

Outputs

OutputTypeDescription
otpChangeEventEmitter<string>Emits the current OTP value when it changes
otpCompleteEventEmitter<string>Emits the OTP value when all fields are filled.

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss changes. 🚀

License

MIT

0.0.3

11 months ago

0.0.4

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago