diff --git a/projects/core/src/lib/classes/mask-model/utils/guess-valid-value-by-reg-exp.ts b/projects/core/src/lib/classes/mask-model/utils/guess-valid-value-by-reg-exp.ts index 169c63827..845bd7432 100644 --- a/projects/core/src/lib/classes/mask-model/utils/guess-valid-value-by-reg-exp.ts +++ b/projects/core/src/lib/classes/mask-model/utils/guess-valid-value-by-reg-exp.ts @@ -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), + ], + }; } diff --git a/projects/demo-integrations/src/tests/component-testing/number/mirrored-value-postfix.cy.ts b/projects/demo-integrations/src/tests/component-testing/number/mirrored-value-postfix.cy.ts new file mode 100644 index 000000000..5cc067588 --- /dev/null +++ b/projects/demo-integrations/src/tests/component-testing/number/mirrored-value-postfix.cy.ts @@ -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); + }); +}); diff --git a/projects/kit/src/lib/processors/postfix-postprocessor.ts b/projects/kit/src/lib/processors/postfix-postprocessor.ts index 4595d8f0f..61d1b62b8 100644 --- a/projects/kit/src/lib/processors/postfix-postprocessor.ts +++ b/projects/kit/src/lib/processors/postfix-postprocessor.ts @@ -26,7 +26,7 @@ export function maskitoPostfixPostprocessorGenerator( '', ); const postfixWasModified = - initialElementState.selection[1] >= initialValueBeforePostfix.length; + initialElementState.selection[1] > initialValueBeforePostfix.length; const alreadyExistedValueBeforePostfix = findCommonBeginningSubstr( initialValueBeforePostfix, value, diff --git a/projects/kit/src/lib/utils/extract-affixes.ts b/projects/kit/src/lib/utils/extract-affixes.ts index 4acfacd31..86f03c919 100644 --- a/projects/kit/src/lib/utils/extract-affixes.ts +++ b/projects/kit/src/lib/utils/extract-affixes.ts @@ -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, + }; }