Skip to content

Commit

Permalink
Fix max length #11
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen77sk committed Apr 21, 2023
1 parent 5cc4d61 commit 154eb86
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions projects/ngx-touch-keyboard/src/lib/ngx-touch-keyboard.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ export class NgxTouchKeyboardComponent {
@Inject(LOCALE_ID) private _defaultLocale: string
) {}

// -----------------------------------------------------------------------------------------------------
// @ Accessors
// -----------------------------------------------------------------------------------------------------

/**
* Getter for maxLength
*/
get maxLength(): number {
return this._activeInputElement?.maxLength ?? -1;
}

// -----------------------------------------------------------------------------------------------------
// @ Decorated methods
// -----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -240,9 +251,6 @@ export class NgxTouchKeyboardComponent {
return;
}

const inputMaxLength = this._activeInputElement?.maxLength ?? 0;
const inputType = this._activeInputElement?.type ?? '';

const commonParams: [number, number, boolean] = [
this._caretPosition || 0,
this._caretPositionEnd || 0,
Expand All @@ -254,27 +262,19 @@ export class NgxTouchKeyboardComponent {
if (!this.isStandardButton(button)) {
// Handel BACKSPACE
if (button === fnButton.BACKSPACE) {
if (output.length > 0) {
output = this._removeAt(output, ...commonParams);
}
output = this._removeAt(output, ...commonParams);
}
// Handel SPACE
else if (button === fnButton.SPACE) {
if (output.length < inputMaxLength) {
output = this._addStringAt(output, ' ', ...commonParams);
}
output = this._addStringAt(output, ' ', ...commonParams);
}
// Handel TAB
else if (button === fnButton.TAB) {
if (output.length < inputMaxLength) {
output = this._addStringAt(output, '\t', ...commonParams);
}
output = this._addStringAt(output, '\t', ...commonParams);
}
// Handel ENTER
else if (button === fnButton.ENTER) {
if (output.length < inputMaxLength) {
output = this._addStringAt(output, '\n', ...commonParams);
}
output = this._addStringAt(output, '\n', ...commonParams);
}
// Handel LAYOUT
else {
Expand Down Expand Up @@ -523,6 +523,10 @@ export class NgxTouchKeyboardComponent {
positionEnd = source.length,
moveCaret = false
) {
if (this.maxLength !== -1 && source.length >= this.maxLength) {
return source;
}

let output;

if (!position && position !== 0) {
Expand Down

0 comments on commit 154eb86

Please sign in to comment.