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,13 @@ export function guessValidValueByRegExp(
return newPossibleValue.match(maskRegExp) ? newPossibleValue : validatedValuePart;
}, '');

if (newFrom > validatedValue.length) {
newFrom = validatedValue.length;
}

if (newTo > validatedValue.length) {
newTo = validatedValue.length;
}

return {value: validatedValue, selection: [newFrom, newTo]};
nsbarsukov marked this conversation as resolved.
Show resolved Hide resolved
}
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 " => Textfield\'s value is "11.22 EUR"', () => {
AndreiBelokopytov marked this conversation as resolved.
Show resolved Hide resolved
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 " => Textfield\'s value is "11.22 EUR"', () => {
AndreiBelokopytov marked this conversation as resolved.
Show resolved Hide resolved
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
13 changes: 11 additions & 2 deletions projects/kit/src/lib/utils/extract-affixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ export function extractAffixes(
const [extractedPrefix = ''] = value.match(prefixRegExp) ?? [];
const [extractedPostfix = ''] = value.match(postfixRegExp) ?? [];

const cleanValue = value.replace(prefixRegExp, '').replace(postfixRegExp, '');
if (extractedPrefix || extractedPostfix) {
return {
extractedPrefix,
extractedPostfix,
cleanValue: value.slice(
extractedPrefix.length,
extractedPostfix.length ? -extractedPostfix.length : Infinity,
),
};
}

return {extractedPrefix, extractedPostfix, cleanValue};
return {extractedPrefix, extractedPostfix, cleanValue: value};
nsbarsukov marked this conversation as resolved.
Show resolved Hide resolved
}