Skip to content

Commit

Permalink
add "enterClicked" option method
Browse files Browse the repository at this point in the history
  • Loading branch information
HichemTab-tech committed Jul 22, 2023
1 parent e275a0b commit 8b16266
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,14 @@ $('#otp_target').otpdesigner({
In these examples, the OTP designer is initialized inside the otp_target element with different configurations. The first example demonstrates the basic usage without custom options, while the second example shows a customized OTP input with larger input fields and a length of 8 digits. The third example demonstrates using options as an object to customize the OTP input.
## Options

| **Option** | **Type** | **Default** | **Description** |
|---------------------------|----------|--------------|----------------------------------------------------------------------------------|
| **`length`** | Integer | 6 | The number of OTP input fields. |
| **`onluNumbers`** | Boolean | false | Allow only numeric input. |
| **`inputsClasses`** | String | "" | Additional CSS classes to apply to the OTP input fields. |
| **`inputsParentClasses`** | String | "" | Additional CSS classes to apply to the parent container of the OTP input fields. |
| **`typingDone`** | Function | (code) => {} | A callback function executed when the user completes typing the OTP. |
| **Option** | **Type** | **Default** | **Description** |
|---------------------------|----------|-------------|--------------------------------------------------------------------------------------|
| **`length`** | Integer | 6 | The number of OTP input fields. |
| **`onluNumbers`** | Boolean | false | Allow only numeric input. |
| **`inputsClasses`** | String | "" | Additional CSS classes to apply to the OTP input fields. |
| **`inputsParentClasses`** | String | "" | Additional CSS classes to apply to the parent container of the OTP input fields. |
| **`typingDone`** | Function | null | A callback function executed when the user completes typing the OTP. |
| **`enterClicked`** | Function | null | A callback function executed when the user click on Enter key when he's done typing. |

## Methods
The OTP Designer jQuery Plugin provides the following method:
Expand Down
25 changes: 22 additions & 3 deletions dist/otpdesigner.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,8 @@ const otpdesigner = function (options = {}, ...args) {
onlyNumbers: false,
inputsClasses: '',
inputsParentClasses: '',
typingDone : function () {}
enterClicked: null,
typingDone : null
},
options
);
Expand Down Expand Up @@ -633,6 +634,14 @@ const otpdesigner = function (options = {}, ...args) {
if (event.key === "Backspace") {
$inputs[i].value = "";
if (i !== 0) $inputs[i - 1].focus();
} else if (event.key === "Enter" && i === $inputs.length - 1 && $inputs[i].value !== "") {
event.preventDefault();
collectOtpCode(data, false);
if (settings.enterClicked != null) {
settings.enterClicked();
}
loseFocus(data);
return;
} else {
if (event.keyCode > 95 && event.keyCode < 106) {
$inputs[i].value = event.key;
Expand Down Expand Up @@ -679,15 +688,25 @@ let stringToBool = function (s) {
return ['true', 'TRUE', '1'].includes(s.toString());
}

let collectOtpCode = (data) => {
let collectOtpCode = (data, typingDone = true) => {
let $inputs = $('#otp_' + data.idSuffix).find('.otp-input');
let code = '';
$inputs.each(function (i, e) {
code += $(e).val().trim();
});
$('#otp_hidden_' + data.idSuffix).val(code);
if (code.length === $inputs.length) data.settings.typingDone(code);
if (code.length === $inputs.length && typingDone) {
if (data.settings.typingDone != null) {
data.settings.typingDone(code);
}
loseFocus(data);
}
}

let loseFocus = (data) => {
if (data.settings.enterClicked != null) return;
$('.otp-input:focus').blur();
};
;// CONCATENATED MODULE: ./index.js


Expand Down
2 changes: 1 addition & 1 deletion dist/otpdesigner.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8b16266

Please sign in to comment.