1.0.5 • Published 8 months ago

input-pincode v1.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

Input Pincode Web Component

input code image.

The input-pincode is a customizable web component designed to capture multi-character input such as a PIN code. It supports various customization options like length, only-numbers mode, initial focus, and error handling.

Keyboard Navigation

Backspace: Deletes the content of the current input and moves to the previous input. Arrow Keys: Navigate between the input fields. Enter: Triggers the onEnter event if the pincode is completed.

Pasting Functionality

You can paste a pincode (e.g., 1234) directly into the component, and the input fields will automatically be populated.

Props

PropTypeDefaultDescription
lengthNumber4The number of input fields, i.e., the number of characters the PIN accepts.
initializeInputFocusBooleantrueAutomatically sets focus on the first input when the component is mounted.
onlyNumbersBooleantrueRestricts the input fields to accept only numeric characters.
disabledBooleanfalseDisables the pincode input fields.
hasErrorBooleanfalseIndicates whether the pincode is in an error state; when true, it displays an error style.
placeholderString''Placeholder character for each input field, displayed as a single character in each field.

Installation

Install via npm or yarn:

npm install input-pincode

Or with yarn:

yarn add input-pincode

Usage

Include the web component in your HTML or React project:

HTML

<input-pincode length="4" only-numbers initialize-input-focus></input-pincode>

REACT

import { InputPincode } from 'input-pincode/react';

<InputPincode length={4} onlyNumbers={true} initializeInputFocus={true} />

Events

Change: Dispatched when the value of the pincode changes. The event details include the isCompleted and value properties:

Enter: Fired when the Enter key is pressed, dispatching the pincode value and its completion status.

element.addEventListener('Change', (e) => {
    const { isCompleted, value } = e.detail;
    console.log('Pincode is complete:', isCompleted);
    console.log('Pincode value:', value);
});

API

General CSS Customization

You can customize the appearance of the entire component by modifying the default variables. These variables affect the overall look of the pincode input fields, regardless of their state.

In addition to the default variables, you can customize the appearance of the component for specific states: valid, has-error, and disabled. Each state has a corresponding variable suffix, such as --valid, --has-error, or --disabled, allowing for more granular control over the styles.

List of CSS Variables

VariableDefault ValueDescription
--wc-input-pincode-container--gapnoneGap between input fields.
--wc-input-pincode-container--justify-contentspace-betweenJustify content setting for the input fields container.
--wc-input-pincode--padding0Padding inside the pincode input field.
--wc-input-pincode--margin0%Margin around the pincode input field.
--wc-input-pincode--font-weight600Font weight of the text inside the input.
--wc-input-pincode--outline0pxOutline width of the input when focused.
--wc-input-pincode--border-width1.5pxWidth of the border surrounding the input.
--wc-input-pincode--border-stylesolidBorder style of the input.
--wc-input-pincode--border-radius0.5remBorder radius of the input field.
--wc-input-pincode--border-color#d9d9d9Border color of the input field.
--wc-input-pincode--height3.81remHeight of each pincode input field.
--wc-input-pincode--width3.63remWidth of each pincode input field.
--wc-input-pincode--text-color#21272aText color of the input content.
--wc-input-pincode--backgroundtransparentBackground color of the input field.
--wc-input-pincode--font-size2remFont size of the text inside the input.
--wc-input-pincode--font-family'Montserrat', sans-serifFont family of the text inside the input.
--wc-input-pincode--line-height2.5remLine height of the text inside the input.

State-Specific Variables

Valid State (--valid)

VariableDefault ValueDescription
--wc-input-pincode--border-color--valid#0079c8Border color when the input is valid.
--wc-input-pincode--padding--validSame as defaultPadding inside the input field when valid.
--wc-input-pincode--font-size--validSame as defaultFont size when the input is valid.

... etc

Error State (--has-error)

VariableDefault ValueDescription
--wc-input-pincode--border-color--has-error#d60023Border color when the input is in error state.
--wc-input-pincode--padding--has-errorSame as defaultPadding inside the input field when there is an error.
--wc-input-pincode--background--has-errorSame as defaultBackground color when the input is in error state.

...etc

Disabled State (--disabled)

VariableDefault ValueDescription
--wc-input-pincode--border-color--disabled#d9d9d9Border color when the input is disabled.
--wc-input-pincode--background--disabled#f8f8f8Background color when the input is disabled.

...etc

Usage Example

Default Customization

input-pincode {
    --wc-input-pincode--border-color: #0000ff;
    --wc-input-pincode--font-size: 1.5rem;
}

State-Specific Customization

input-pincode {
    --wc-input-pincode--border-color--valid: #00ff00;
    --wc-input-pincode--border-color--has-error: #ff0000;
}

With these options, you can fully customize the appearance and behavior of the input-pincode component based on its state (valid, error, disabled), or modify the default styles for a more consistent look.

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.1

9 months ago

1.0.0

9 months ago