Skip to content

Commit

Permalink
fix bug that made it impossible to enter the value 0 for currencies w…
Browse files Browse the repository at this point in the history
…ithout decimal places
  • Loading branch information
konstantin-lukas committed Oct 2, 2023
1 parent 76bbdb7 commit d003fa2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "intl-currency-input",
"version": "2.0.0",
"version": "2.0.1",
"description": "This library allows you to create input fields for currencies. Version 2 is completely overhauled, written in TypeScript and includes test cases.",
"main": "index.js",
"module": "index.mjs",
Expand Down Expand Up @@ -44,7 +44,7 @@
"webpack-dev-server": "^4.15.1"
},
"dependencies": {
"moneydew": "^1.0.0"
"moneydew": "^1.0.1"
},
"files": [
"index.d.ts",
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export default class IntlCurrencyInput {
const posMatch: boolean = this._posPrefixPattern.test(this._input.value) && this._posSuffixPattern.test(this._input.value);
const negMatch: boolean = this._negPrefixPattern.test(this._input.value) && this._negSuffixPattern.test(this._input.value);


if (posMatch || negMatch) {
const negativePriority = this._formatter.positiveSign === '';
const matchedPrefix = negativePriority ?
Expand Down Expand Up @@ -138,7 +137,7 @@ export default class IntlCurrencyInput {
numberRegexPattern += '[1-9]\\d*' + esc(this._formatter.decimalSeparator) + '\\d{0,' + this._money.floatingPointPrecision + '})';
}
} else {
numberRegexPattern += '[1-9]\\d*';
numberRegexPattern += '(0|[1-9]\\d*)';
}
numberRegexPattern += '$';

Expand All @@ -159,6 +158,8 @@ export default class IntlCurrencyInput {
rawValue = rawValue.substring(1);
}



if (valuePattern.test(rawValue)) {
const groupSize = this._formatter.groupSize;
const separator = this._formatter.groupSeparator;
Expand Down Expand Up @@ -201,6 +202,7 @@ export default class IntlCurrencyInput {
inputRejected = true;
}


/* istanbul ignore next */
let caretPos = this._input.selectionStart === null ? 0 : this._input.selectionStart;
const oldCaretPos = caretPos;
Expand Down
17 changes: 17 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,23 @@ describe('CurrencyInput', () => {

});

it('should be usable for percentages', async () => {
input.format({
displayOrder: DisplayOrder.NAME_SIGN_NUMBER_SYMBOL,
currencyName: '',
currencySymbol: '%',
groupSeparator: '',
decimalSeparator: '.'
});
input.setValue('20');
await userEvent.type(inputElement, '{backspace}', {
initialSelectionStart: 1,
initialSelectionEnd: 1,
});
expect(input.getValue()).toBe('0');
expect(inputElement.value).toBe('0%');
});

it('should insert a zero when all numbers before decimal point are deleted', async () => {
input.setValue('123.00');

Expand Down

0 comments on commit d003fa2

Please sign in to comment.