Skip to content

Commit

Permalink
fix(CreateNumberMask.js): allow full select delete
Browse files Browse the repository at this point in the history
fix #43
  • Loading branch information
scottmcpherson committed Jun 26, 2018
1 parent 31b214d commit 8aa92c9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 65 deletions.
19 changes: 9 additions & 10 deletions examples/package-lock.json

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

45 changes: 20 additions & 25 deletions package-lock.json

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

20 changes: 0 additions & 20 deletions src/__tests__/createNumberMask.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,6 @@ describe('Number mask', () => {
);
});

it('should not update the stored value if the input is invalid', () => {
const prefix = 'prefix 1@,.';
const changedPrefix = prefix.substring(0, prefix.length - 1);
const suffix = '1@,. suffix';
const changedSuffix = prefix.substring(1, suffix.length);

const updatedValue = '1,2345';
const previousValue = 1234;

const mask = createNumberMask({ prefix, suffix });

expect(mask.normalize(updatedValue, previousValue)).toBe(previousValue);
expect(
mask.normalize(`${changedPrefix}${updatedValue}${suffix}`, previousValue),
).toBe(previousValue);
expect(
mask.normalize(`${prefix}${updatedValue}${changedSuffix}`, previousValue),
).toBe(previousValue);
});

it('should ignore any non-alphanumeric characters inputted', () => {
const prefix = 'p';
const suffix = 's';
Expand Down
16 changes: 6 additions & 10 deletions src/createNumberMask.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ export default options => {
const prefixRegex = new RegExp(`^[-|+]? ?${escapedPrefix}`);
const suffixRegex = new RegExp(`${escapedSuffix}$`);

// If the prefix or the suffix have been modified, do nothing
if (
previousValue &&
(!prefixRegex.test(updatedValue) || !suffixRegex.test(updatedValue))
) {
return previousValue;
}

// Checks if we need to negate the value
let multiplier = 1;
if (allowNegative) {
Expand Down Expand Up @@ -123,14 +115,18 @@ export default options => {
};

const manageCaretPosition = event => {
if (event.target) {
const { target, type } = event;

if (type === 'click' || type === 'mousedown') return;

if (target) {
if (event.persist) {
event.persist();
}

// This timeout is needed to get updated values
setTimeout(() => {
const caretPos = event.target.value.length - suffix.length;
const caretPos = target.value.length - suffix.length;
event.target.setSelectionRange(caretPos, caretPos);
});
}
Expand Down

0 comments on commit 8aa92c9

Please sign in to comment.