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

Fixed inputNumber with numeric prefix is not working as expected #15398

Merged
Merged
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
32 changes: 25 additions & 7 deletions src/app/components/inputnumber/inputnumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import {
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR, NgControl } from '@angular/forms';
import { PrimeTemplate, SharedModule } from 'primeng/api';
import { AutoFocusModule } from 'primeng/autofocus';
import { ButtonModule } from 'primeng/button';
import { DomHandler } from 'primeng/dom';
import { AngleDownIcon } from 'primeng/icons/angledown';
import { AngleUpIcon } from 'primeng/icons/angleup';
import { TimesIcon } from 'primeng/icons/times';
import { InputTextModule } from 'primeng/inputtext';
import { Nullable } from 'primeng/ts-helpers';
import { AutoFocusModule } from 'primeng/autofocus';
import { InputNumberInputEvent } from './inputnumber.interface';

export const INPUTNUMBER_VALUE_ACCESSOR: any = {
Expand Down Expand Up @@ -616,11 +616,14 @@ export class InputNumber implements OnInit, AfterContentInit, OnChanges, Control
}

getPrefixExpression(): RegExp {

if (this.prefix) {
this.prefixChar = this.prefix;

} else {
const formatter = new Intl.NumberFormat(this.locale, { style: this.mode, currency: this.currency, currencyDisplay: this.currencyDisplay });
this.prefixChar = formatter.format(1).split('1')[0];

}

return new RegExp(`${this.escapeRegExp(this.prefixChar || '')}`, 'g');
Expand All @@ -647,11 +650,12 @@ export class InputNumber implements OnInit, AfterContentInit, OnChanges, Control
if (this.format) {
let formatter = new Intl.NumberFormat(this.locale, this.getOptions());
let formattedValue = formatter.format(value);
if (this.prefix) {

if (this.prefix && value != this.prefix) {
formattedValue = this.prefix + formattedValue;
}

if (this.suffix) {
if (this.suffix && value != this.suffix) {
formattedValue = formattedValue + this.suffix;
}

Expand All @@ -665,12 +669,16 @@ export class InputNumber implements OnInit, AfterContentInit, OnChanges, Control
}

parseValue(text: any) {
const suffixRegex = new RegExp(this._suffix, '');
const prefixRegex = new RegExp(this._prefix, '');
const currencyRegex = new RegExp(this._currency, '');

let filteredText = text
.replace(this._suffix as RegExp, '')
.replace(this._prefix as RegExp, '')
.replace(suffixRegex, '')
.replace(prefixRegex, '')
.trim()
.replace(/\s/g, '')
.replace(this._currency as RegExp, '')
.replace(currencyRegex, '')
.replace(this._group, '')
.replace(this._minusSign, '-')
.replace(this._decimal, '.')
Expand Down Expand Up @@ -867,7 +875,14 @@ export class InputNumber implements OnInit, AfterContentInit, OnChanges, Control
case 'Backspace': {
event.preventDefault();


if (selectionStart === selectionEnd) {

if((selectionStart == 1 && this.prefix)|| (selectionStart == inputValue.length && this.suffix)){

break;
}

const deleteChar = inputValue.charAt(selectionStart - 1);
const { decimalCharIndex, decimalCharIndexWithoutPrefix } = this.getDecimalCharIndexes(inputValue);

Expand Down Expand Up @@ -911,6 +926,10 @@ export class InputNumber implements OnInit, AfterContentInit, OnChanges, Control
event.preventDefault();

if (selectionStart === selectionEnd) {

if((selectionStart == 0 && this.prefix)|| (selectionStart == inputValue.length-1 && this.suffix)){
break;
}
const deleteChar = inputValue.charAt(selectionStart);
const { decimalCharIndex, decimalCharIndexWithoutPrefix } = this.getDecimalCharIndexes(inputValue);

Expand Down Expand Up @@ -985,7 +1004,6 @@ export class InputNumber implements OnInit, AfterContentInit, OnChanges, Control
char = this._decimalChar;
code = char.charCodeAt(0);
}

const newValue = this.parseValue(this.input.nativeElement.value + char);
const newValueStr = newValue != null ? newValue.toString() : '';

Expand Down
Loading