Skip to content

Commit

Permalink
[ISSUE - 340] - Problem with deleting prefix characters (#351)
Browse files Browse the repository at this point in the history
- FIX: Added handling for deletion in getPrefixStrippedValue in util.js
  • Loading branch information
vijay-j authored and nosir committed May 27, 2018
1 parent ad8a19a commit c9cb5a7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Cleave.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ Cleave.prototype = {
value = Util.stripDelimiters(value, pps.delimiter, pps.delimiters);

// strip prefix
value = Util.getPrefixStrippedValue(value, pps.prefix, pps.prefixLength);
value = Util.getPrefixStrippedValue(value, pps.prefix, pps.prefixLength, pps.result);

// strip non-numeric characters
value = pps.numericOnly ? Util.strip(value, /[^\d]/g) : value;
Expand Down Expand Up @@ -372,7 +372,7 @@ Cleave.prototype = {
rawValue = owner.element.value;

if (pps.rawValueTrimPrefix) {
rawValue = Util.getPrefixStrippedValue(rawValue, pps.prefix, pps.prefixLength);
rawValue = Util.getPrefixStrippedValue(rawValue, pps.prefix, pps.prefixLength, pps.result);
}

if (pps.numeral) {
Expand Down
8 changes: 4 additions & 4 deletions src/Cleave.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var cleaveReactClass = CreateReactClass({

if (newValue !== owner.properties.initValue && newValue !== owner.properties.result) {
owner.properties.initValue = newValue;
owner.onInput(newValue, true);
owner.onInput(newValue, true);
}
}

Expand Down Expand Up @@ -182,7 +182,7 @@ var cleaveReactClass = CreateReactClass({
rawValue = pps.result;

if (pps.rawValueTrimPrefix) {
rawValue = Util.getPrefixStrippedValue(rawValue, pps.prefix, pps.prefixLength);
rawValue = Util.getPrefixStrippedValue(rawValue, pps.prefix, pps.prefixLength, pps.result);
}

if (pps.numeral) {
Expand Down Expand Up @@ -255,7 +255,7 @@ var cleaveReactClass = CreateReactClass({
onInput: function (value, fromProps) {
var owner = this, pps = owner.properties;

if (Util.isAndroidBackspaceKeydown(owner.lastInputValue, owner.element.value) &&
if (Util.isAndroidBackspaceKeydown(owner.lastInputValue, owner.element.value) &&
Util.isDelimiter(pps.result.slice(-pps.delimiterLength), pps.delimiter, pps.delimiters)) {
pps.backspace = true;
}
Expand Down Expand Up @@ -298,7 +298,7 @@ var cleaveReactClass = CreateReactClass({
value = Util.stripDelimiters(value, pps.delimiter, pps.delimiters);

// strip prefix
value = Util.getPrefixStrippedValue(value, pps.prefix, pps.prefixLength);
value = Util.getPrefixStrippedValue(value, pps.prefix, pps.prefixLength, pps.result);

// strip non-numeric characters
value = pps.numericOnly ? Util.strip(value, /[^\d]/g) : value;
Expand Down
11 changes: 8 additions & 3 deletions src/utils/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,16 @@ var Util = {
// for prefix: PRE
// (PRE123, 3) -> 123
// (PR123, 3) -> 23 this happens when user hits backspace in front of "PRE"
getPrefixStrippedValue: function (value, prefix, prefixLength) {
getPrefixStrippedValue: function (value, prefix, prefixLength, prevValue) {
if (value.slice(0, prefixLength) !== prefix) {
var diffIndex = this.getFirstDiffIndex(prefix, value.slice(0, prefixLength));

value = prefix + value.slice(diffIndex, diffIndex + 1) + value.slice(prefixLength + 1);
// Check whether if it is a deletion
if (value.length < prevValue.length) {
value = value.length > prefixLength ? prevValue : prefix;
} else {
var diffIndex = this.getFirstDiffIndex(prefix, value.slice(0, prefixLength));
value = prefix + value.slice(diffIndex, diffIndex + 1) + value.slice(prefixLength + 1);
}
}

return value.slice(prefixLength);
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/util.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,25 @@
],
"expected": "123-456X789"
}
],
"getPrefixStrippedValue": [
{
"params": [
"tst123",
"test",
4,
"test123"
],
"expected": "123"
},
{
"params": [
"teast",
"test",
4,
"test"
],
"expected": "a"
}
]
}
9 changes: 9 additions & 0 deletions test/unit/Util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ describe('Util', function () {
});
});
});

describe('getPrefixStrippedValue:', function () {
_.each(json.getPrefixStrippedValue, function (data) {
var params = data.params;
it('should get prefix stripped value for: ' + params[0] + ' as: ' + data.expected, function () {
Util.getPrefixStrippedValue(params[0], params[1], params[2], params[3]).should.eql(data.expected);
});
});
})
});

0 comments on commit c9cb5a7

Please sign in to comment.