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(InputMasked): on custom mask – avoid interaction stall after focus #2270

Merged
merged 1 commit into from
May 2, 2023
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 @@ -426,7 +426,7 @@ const useCallEvent = ({ setLocalValue }) => {
const cleaned_value = cleanedValue // Deprecated

if (name === 'on_change' && numberValue === 0) {
correctCaretPosition(event.target, maskParams)
correctCaretPosition(event.target, maskParams, props)
}

const result = dispatchCustomElementEvent(props, name, {
Expand All @@ -446,7 +446,7 @@ const useCallEvent = ({ setLocalValue }) => {
!props.selectall
) {
// Also correct here, because of additional click inside the field
correctCaretPosition(event.target, maskParams)
correctCaretPosition(event.target, maskParams, props)
}

return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,20 @@ export const correctNumberValue = ({
* @param {Element} element Input Element
* @param {Object} maskParams Mask parameters, containing eventually suffix or prefix
*/
export const correctCaretPosition = (element, maskParams) => {
export const correctCaretPosition = (element, maskParams, props) => {
const correction = () => {
try {
const suffix = maskParams?.suffix
const prefix = maskParams?.prefix

if (suffix || prefix) {
const start = element.selectionStart
const end = element.selectionEnd
const start = element.selectionStart
const end = element.selectionEnd

if (start !== end) {
return // stop here
}
if (start !== end) {
return // stop here
}

if (suffix || prefix) {
const suffixStart = element.value.indexOf(suffix)
const suffixEnd = suffixStart + suffix?.length
let pos = undefined
Expand Down Expand Up @@ -223,6 +223,29 @@ export const correctCaretPosition = (element, maskParams) => {
if (!isNaN(parseFloat(pos))) {
safeSetSelection(element, pos)
}
} else if (props?.mask && element.value.length === end) {
const chars = element.value.split('')

for (let l = chars.length, i = l - 1; i >= 0; i--) {
const char = chars[i]
const mask = props.mask[i]
if (
char &&
char !== invisibleSpace &&
mask instanceof RegExp &&
mask.test(char)
) {
for (let n = i + 1; n < l; n++) {
const mask = props.mask[n]
if (mask?.test?.(mask) === false) {
safeSetSelection(element, n)
break
}
}

break
}
}
}
} catch (e) {
warn(e)
Expand Down