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): move caret after attempt to erase fixed character in a mask with Placeholder #1307

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,57 @@ describe('Placeholder | US phone', () => {
.blur()
.should('have.value', '');
});

describe('caret navigation on attempt to erase fixed character', () => {
beforeEach(() => {
cy.get('@input')
.type('2125552')
.should('have.value', '+1 (212) 555-2___')
.should('have.prop', 'selectionStart', '+1 (212) 555-2'.length)
.should('have.prop', 'selectionEnd', '+1 (212) 555-2'.length);
});

it('+1 (212) 555-|2___ => Backspace => +1 (212) 555|-2___', () => {
cy.get('@input')
.type('{leftArrow}{backspace}')
.should('have.value', '+1 (212) 555-2___')
.should('have.prop', 'selectionStart', '+1 (212) 555'.length)
.should('have.prop', 'selectionEnd', '+1 (212) 555'.length);
});

it('+1 (212) 555|-2___ => Delete => +1 (212) 555-|2___', () => {
cy.get('@input')
.type('{leftArrow}'.repeat(2))
.should('have.prop', 'selectionStart', '+1 (212) 555'.length)
.should('have.prop', 'selectionEnd', '+1 (212) 555'.length)
.type('{del}')
.should('have.value', '+1 (212) 555-2___')
.should('have.prop', 'selectionStart', '+1 (212) 555-'.length)
.should('have.prop', 'selectionEnd', '+1 (212) 555-'.length);
});

it('+1 (212) |555-2___ => Backspace x2 => +1 (212|) 555-2___', () => {
cy.get('@input')
.type('{leftArrow}'.repeat('555-2'.length))
.type('{backspace}')
.should('have.value', '+1 (212) 555-2___')
.should('have.prop', 'selectionStart', '+1 (212)'.length)
.should('have.prop', 'selectionEnd', '+1 (212)'.length)
.type('{backspace}')
.should('have.value', '+1 (212) 555-2___')
.should('have.prop', 'selectionStart', '+1 (212'.length)
.should('have.prop', 'selectionEnd', '+1 (212'.length);
});

it('+1 (212|) 555-2___ => Delete => +1 (212) |555-2___', () => {
cy.get('@input')
.type('{leftArrow}'.repeat(') 555-2'.length))
.should('have.prop', 'selectionStart', '+1 (212'.length)
.should('have.prop', 'selectionEnd', '+1 (212'.length)
.type('{del}')
.should('have.value', '+1 (212) 555-2___')
.should('have.prop', 'selectionStart', '+1 (212) '.length)
.should('have.prop', 'selectionEnd', '+1 (212) '.length);
});
});
});
30 changes: 22 additions & 8 deletions projects/kit/src/lib/processors/with-placeholder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {MaskitoOptions} from '@maskito/core';
import type {MaskitoOptions, MaskitoPreprocessor} from '@maskito/core';
import {maskitoUpdateElement} from '@maskito/core';

import {maskitoCaretGuard, maskitoEventHandler} from '../plugins';
Expand All @@ -10,6 +10,7 @@ export function maskitoWithPlaceholder(
removePlaceholder: (value: string) => string;
} {
let lastClearValue = '';
let action: Parameters<MaskitoPreprocessor>[1] = 'validation';
const removePlaceholder = (value: string): string => {
for (let i = value.length - 1; i >= lastClearValue.length; i--) {
if (value[i] !== placeholder[i]) {
Expand Down Expand Up @@ -52,7 +53,8 @@ export function maskitoWithPlaceholder(
plugins,
removePlaceholder,
preprocessors: [
({elementState, data}) => {
({elementState, data}, actionType) => {
action = actionType;
const {value, selection} = elementState;

return {
Expand All @@ -76,12 +78,24 @@ export function maskitoWithPlaceholder(
* For example, developer wants to remove manually placeholder (+ do something else with value) on blur.
* Without this condition, placeholder will be unexpectedly added again.
*/
return value !== initialElementState.value && (focused || !focusedOnly)
? {
value: value + placeholder.slice(value.length),
selection,
}
: {value, selection};
const newValue =
value !== initialElementState.value && (focused || !focusedOnly)
? value + placeholder.slice(value.length)
: value;

if (
newValue === initialElementState.value &&
action === 'deleteBackward'
) {
const [caretIndex] = initialElementState.selection;

return {
value: newValue,
selection: [caretIndex, caretIndex],
};
}

return {value: newValue, selection};
},
],
};
Expand Down
Loading