Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): handle the full-width characters and numbers input #203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions demo/demo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { CurrencyMaskInputMode } from '../src/currency-mask.config';
class="form-control"
currencyMask
formControlName="value"
inputmode="decimal"
[placeholder]="'R$ 0,00'"
[options]="ngxCurrencyOptions"
/>
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

4 changes: 1 addition & 3 deletions src/currency-mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ export class CurrencyMaskDirective implements AfterViewInit, ControlValueAccesso

@HostListener("input", ["$event"])
handleInput(event: any) {
if (this.isChromeAndroid()) {
!this.isReadOnly() && this.inputHandler.handleInput(event);
}
!this.isReadOnly() && this.inputHandler.handleInput(event);
}

@HostListener("keydown", ["$event"])
Expand Down
4 changes: 2 additions & 2 deletions src/input.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export class InputManager {
}

get canInputMoreNumbers(): boolean {
let onlyNumbers = this.rawValue.replace(/[^0-9\u0660-\u0669\u06F0-\u06F9]/g, "");
let onlyNumbers = this.rawValue.replace(/[^0-9\u0660-\u0669\u06F0-\u06F9\uFF10-\uFF19]/g, "");
let haventReachedMaxLength = !(onlyNumbers.length >= this.htmlInputElement.maxLength && this.htmlInputElement.maxLength >= 0);
let selectionStart = this.inputSelection.selectionStart;
let selectionEnd = this.inputSelection.selectionEnd;
let haveNumberSelected = !!(selectionStart != selectionEnd &&
this.htmlInputElement.value.substring(selectionStart, selectionEnd).match(/[^0-9\u0660-\u0669\u06F0-\u06F9]/));
this.htmlInputElement.value.substring(selectionStart, selectionEnd).match(/[^0-9\u0660-\u0669\u06F0-\u06F9\uFF10-\uFF19]/));
let startWithZero = (this.htmlInputElement.value.substring(0, 1) == "0");
return haventReachedMaxLength || haveNumberSelected || startWithZero;
}
Expand Down
21 changes: 17 additions & 4 deletions src/input.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { InputManager } from "./input.manager";
import { CurrencyMaskConfig, CurrencyMaskInputMode } from "./currency-mask.config";

export class InputService {
private SINGLE_DIGIT_REGEX: RegExp = new RegExp(/^[0-9\u0660-\u0669\u06F0-\u06F9]$/);
private ONLY_NUMBERS_REGEX: RegExp = new RegExp(/[^0-9\u0660-\u0669\u06F0-\u06F9]/g);
private SINGLE_DIGIT_REGEX: RegExp = new RegExp(/^[0-9\u0660-\u0669\u06F0-\u06F9\uFF10-\uFF19]$/);
private ONLY_NUMBERS_REGEX: RegExp = new RegExp(/[^0-9\u0660-\u0669\u06F0-\u06F9\uFF10-\uFF19]/g);

PER_AR_NUMBER: Map<string, string> = new Map<string, string>();

Expand All @@ -29,6 +29,18 @@ export class InputService {
this.PER_AR_NUMBER.set("\u0667", "7");
this.PER_AR_NUMBER.set("\u0668", "8");
this.PER_AR_NUMBER.set("\u0669", "9");


this.PER_AR_NUMBER.set("\uFF10", "0");
this.PER_AR_NUMBER.set("\uFF11", "1");
this.PER_AR_NUMBER.set("\uFF12", "2");
this.PER_AR_NUMBER.set("\uFF13", "3");
this.PER_AR_NUMBER.set("\uFF14", "4");
this.PER_AR_NUMBER.set("\uFF15", "5");
this.PER_AR_NUMBER.set("\uFF16", "6");
this.PER_AR_NUMBER.set("\uFF17", "7");
this.PER_AR_NUMBER.set("\uFF18", "8");
this.PER_AR_NUMBER.set("\uFF19", "9");
}

inputManager: InputManager;
Expand Down Expand Up @@ -100,14 +112,15 @@ export class InputService {
let integerPart = onlyNumbers.slice(0, onlyNumbers.length - precision)
.replace(/^\u0660*/g, "")
.replace(/^\u06F0*/g, "")
.replace(/^0*/g, "");
.replace(/^0*/g, "")
.replace(/^\uFF10*/g, "");

if (integerPart == "") {
integerPart = "0";
}
let integerValue = parseInt(integerPart);

integerPart = integerPart.replace(/\B(?=([0-9\u0660-\u0669\u06F0-\u06F9]{3})+(?![0-9\u0660-\u0669\u06F0-\u06F9]))/g, thousands);
integerPart = integerPart.replace(/\B(?=([0-9\u0660-\u0669\u06F0-\u06F9\uFF10-\uFF19]{3})+(?![0-9\u0660-\u0669\u06F0-\u06F9\uFF10-\uFF19]))/g, thousands);
if (thousands && integerPart.startsWith(thousands)) {
integerPart = integerPart.substring(1);
}
Expand Down
3 changes: 2 additions & 1 deletion webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export default {
port: 8000,
inline: true,
hot: true,
historyApiFallback: true
historyApiFallback: true,
host:'0.0.0.0'
},
plugins: [
...(IS_PROD ? [] : [
Expand Down