Skip to content

Commit

Permalink
fix(kit): Placeholder can have now the same character as textfield'…
Browse files Browse the repository at this point in the history
…s value (#1209)
  • Loading branch information
nsbarsukov committed Apr 23, 2024
1 parent d72f225 commit ed06936
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
@@ -0,0 +1,61 @@
import type {MaskitoOptions} from '@maskito/core';
import {maskitoWithPlaceholder} from '@maskito/kit';

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

describe('Placeholder | intersects with some characters from textfield value', () => {
const maskitoOptions: MaskitoOptions = {
...maskitoWithPlaceholder('DD/MMM/YYYY', true),
mask: [
/\d/,
/\d/,
'/',
/[a-zA-Z]/i,
/[a-zA-Z]/i,
/[a-zA-Z]/i,
'/',
/\d/,
/\d/,
/\d/,
/\d/,
],
};

beforeEach(() => {
cy.mount(TestInput, {
componentProperties: {
maskitoOptions,
},
});
});

it('Empty => focus => show placeholder', () => {
cy.get('input').focus().should('have.value', 'DD/MMM/YYYY');
});

it('Empty => Type 31 => 31/MMM/YYYY', () => {
cy.get('input')
.type('31')
.should('have.value', '31/MMM/YYYY')
.should('have.prop', 'selectionStart', 2)
.should('have.prop', 'selectionEnd', 2)
.blur()
.should('have.value', '31');
});

it('31|/MMM/YYYY => Type May => 31/May|/YYYY', () => {
cy.get('input')
.type('31')
.should('have.value', '31/MMM/YYYY')
.type('M')
.should('have.value', '31/MMM/YYYY')
.should('have.prop', 'selectionStart', '31/M'.length)
.should('have.prop', 'selectionEnd', '31/M'.length)
.type('ay')
.should('have.value', '31/May/YYYY')
.should('have.prop', 'selectionStart', '31/May'.length)
.should('have.prop', 'selectionEnd', '31/May'.length)
.blur()
.should('have.value', '31/May');
});
});
14 changes: 9 additions & 5 deletions projects/kit/src/lib/processors/with-placeholder.ts
Expand Up @@ -9,14 +9,15 @@ export function maskitoWithPlaceholder(
): Pick<Required<MaskitoOptions>, 'plugins' | 'postprocessors' | 'preprocessors'> & {
removePlaceholder: (value: string) => string;
} {
let lastClearValue = '';
const removePlaceholder = (value: string): string => {
for (let i = value.length - 1; i >= 0; i--) {
for (let i = value.length - 1; i >= lastClearValue.length; i--) {
if (value[i] !== placeholder[i]) {
return value.slice(0, i + 1);
}
}

return '';
return value.slice(0, lastClearValue.length);
};
const plugins = [maskitoCaretGuard(value => [0, removePlaceholder(value).length])];

Expand Down Expand Up @@ -64,7 +65,9 @@ export function maskitoWithPlaceholder(
},
],
postprocessors: [
({value, selection}, initialElementState) =>
({value, selection}, initialElementState) => {
lastClearValue = value;

/**
* If `value` still equals to `initialElementState.value`,
* then it means that value is patched programmatically (from Maskito's plugin or externally).
Expand All @@ -73,12 +76,13 @@ 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.
*/
value !== initialElementState.value && (focused || !focusedOnly)
return value !== initialElementState.value && (focused || !focusedOnly)
? {
value: value + placeholder.slice(value.length),
selection,
}
: {value, selection},
: {value, selection};
},
],
};
}

0 comments on commit ed06936

Please sign in to comment.