Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kit): Number + postfix (with leading space) adds unnecessary spaces on paste value with trailing spaces #1865

Merged
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,
};
}