Skip to content

Commit

Permalink
fix(kit): Number + postfix (with leading space) adds unnecessary sp…
Browse files Browse the repository at this point in the history
…aces on paste value with trailing spaces (#1865)
  • Loading branch information
AndreiBelokopytov authored Nov 29, 2024
1 parent 073f93c commit c37b1d6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@ export function guessValidValueByRegExp(
return newPossibleValue.match(maskRegExp) ? newPossibleValue : validatedValuePart;
}, '');

return {value: validatedValue, selection: [newFrom, newTo]};
return {
value: validatedValue,
selection: [
Math.min(newFrom, validatedValue.length),
Math.min(newTo, validatedValue.length),
],
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {maskitoNumberOptionsGenerator} from '@maskito/kit';

import {TestInput} from '../utils';

describe('Number | [postfix]=" EUR" (no initial value & no caret guard)', () => {
beforeEach(() => {
cy.mount(TestInput, {
componentProperties: {
initialValue: '',
maskitoOptions: maskitoNumberOptionsGenerator({
postfix: ' EUR',
precision: 2,
}),
},
});
cy.get('input').focus().should('have.value', '').as('input');
});

it('Empty input => Paste "11.22 " => 11.22 |EUR', () => {
cy.get('input')
.paste('11.22 ')
.should('have.value', '11.22 EUR')
.should('have.prop', 'selectionStart', '11.22 '.length)
.should('have.prop', 'selectionEnd', '11.22 '.length);
});

it('Empty input => Paste "11.22 " (with two trailing spaces) => 11.22 |EUR', () => {
cy.get('input')
.paste('11.22 ')
.should('have.value', '11.22 EUR')
.should('have.prop', 'selectionStart', '11.22 '.length)
.should('have.prop', 'selectionEnd', '11.22 '.length);
});
});
2 changes: 1 addition & 1 deletion projects/kit/src/lib/processors/postfix-postprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function maskitoPostfixPostprocessorGenerator(
'',
);
const postfixWasModified =
initialElementState.selection[1] >= initialValueBeforePostfix.length;
initialElementState.selection[1] > initialValueBeforePostfix.length;
const alreadyExistedValueBeforePostfix = findCommonBeginningSubstr(
initialValueBeforePostfix,
value,
Expand Down
14 changes: 11 additions & 3 deletions projects/kit/src/lib/utils/extract-affixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ export function extractAffixes(
const [extractedPrefix = ''] = value.match(prefixRegExp) ?? [];
const [extractedPostfix = ''] = value.match(postfixRegExp) ?? [];

const cleanValue = value.replace(prefixRegExp, '').replace(postfixRegExp, '');

return {extractedPrefix, extractedPostfix, cleanValue};
return {
extractedPrefix,
extractedPostfix,
cleanValue:
extractedPrefix || extractedPostfix
? value.slice(
extractedPrefix.length,
extractedPostfix.length ? -extractedPostfix.length : Infinity,
)
: value,
};
}

0 comments on commit c37b1d6

Please sign in to comment.