Skip to content

Commit

Permalink
Fixes infinite loop bug + bumps version
Browse files Browse the repository at this point in the history
  • Loading branch information
inoda committed Jan 8, 2020
1 parent 5fbe152 commit c5e25ca
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.0.3 (Jan 08, 2020)

### Bug Fixes
* Fixes an infinite loop when retroactively adding input

## 2.0.2 (Feb 27, 2019)

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "react-masked-field",
"description": "A masked field component built in React",
"author": "Rylan Collins <rylan@gusto.com>",
"version": "2.0.2",
"version": "2.0.3",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
27 changes: 27 additions & 0 deletions spec/MaskedField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,33 @@ describe('MaskedField', () => {
});
});
});

describe('all characters except first are added, and then first character is added', () => {
beforeEach(() => {
setSelection(1, 1);
simulateKeyPress('2');
setSelection(2, 2);
simulateKeyPress('2');
setSelection(3, 3);
simulateKeyPress('2');
setSelection(4, 4);
simulateKeyPress('2');
setSelection(5, 5);
simulateKeyPress('2');
setSelection(6, 6);
simulateKeyPress('2');
setSelection(7, 7);
simulateKeyPress('2');
});

it('inserts characters correctly and does not infinitely loop', () => {
expect(getFieldValue()).toEqual('_2/22/2222');

setSelection(0, 0);
simulateKeyPress('2');
expect(getFieldValue()).toEqual('2_/22/2222');
});
});
});

describe('pressing the backspace key', () => {
Expand Down
6 changes: 4 additions & 2 deletions src/AlwaysMaskedField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,12 @@ class AlwaysMaskedField extends React.Component<AlwaysMaskedFieldProps, MaskedFi
if (c === this.buffer[bufferIdx]) {
bufferIdx += 1;
} else if (pattern.test(c)) {
while (this.buffer[bufferIdx] !== '_') {
while (this.buffer[bufferIdx] !== '_' && bufferIdx < this.buffer.length) {
bufferIdx += 1;
}
this.buffer[bufferIdx] = c;
if (this.buffer[bufferIdx] !== undefined) {
this.buffer[bufferIdx] = c;
}
break;
} else if (this.cursorPos > lastPatternIdx) {
this.cursorPos -= 1;
Expand Down

0 comments on commit c5e25ca

Please sign in to comment.