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

feat: handle masking in textfield component when user deletes some characters from middle of the text #650

Open
gmukul01 opened this issue Apr 25, 2022 · 0 comments
Labels
feature request New feature request

Comments

@gmukul01
Copy link
Collaborator

gmukul01 commented Apr 25, 2022

Description

Handle masking in textfield component when user deletes some characters from middle of the text

Solution you'd like

Provide a clear and concise description of the feature behaviour.

Alternatives you've considered

Provide a clear and concise description of any alternative solutions or features you've considered.

Additional context

I was trying this solution but it needs more refactoring and other edge cases to be covered

//TODO: Handle the case when the user deletes the character in the middle of the mask
    const cursorPosition = selectionStart || 0;

    event.target.value = maskedValue;

    value.match(/[^a-zA-Z0-9]/) &&
        cursorPosition > 0 &&
        cursorPosition < maskedValue.length &&
        !/[^a-zA-Z0-9]/.test(maskedValue[cursorPosition - 1]) &&
        event?.target?.setSelectionRange(cursorPosition, cursorPosition);

    if (
        value.match(/[^a-zA-Z0-9]/) &&
        cursorPosition > 0 &&
        cursorPosition < maskedValue.length &&
        /[^a-zA-Z0-9]/.test(maskedValue[cursorPosition - 1])
    ) {
        const indexOf = maskedValue.substr(cursorPosition - 1).match(/[a-zA-Z0-9]/);
        if ((indexOf?.index || 0) > 0) {
            const position = (indexOf?.input || '').length === (indexOf?.index || 0) + 1 ? indexOf?.index || 0 : (indexOf?.index || 0) - 1;
            event?.target?.setSelectionRange(cursorPosition + position, cursorPosition + position);
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature request
Projects
None yet
Development

No branches or pull requests

1 participant