From 155d9c78612eb2551f71cf5b07d7c8b5d2a44280 Mon Sep 17 00:00:00 2001 From: melloware Date: Wed, 6 Sep 2023 08:56:07 -0400 Subject: [PATCH] Fix #4882: InputMask AutoFocus --- components/lib/inputmask/InputMask.js | 27 ++++++++++++++++++--------- components/lib/inputtext/InputText.js | 2 +- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/components/lib/inputmask/InputMask.js b/components/lib/inputmask/InputMask.js index ca4463fe9a..cade99df89 100644 --- a/components/lib/inputmask/InputMask.js +++ b/components/lib/inputmask/InputMask.js @@ -1,15 +1,15 @@ import * as React from 'react'; +import { PrimeReactContext } from '../api/Api'; import { useMountEffect, useUpdateEffect } from '../hooks/Hooks'; import { InputText } from '../inputtext/InputText'; import { DomHandler, ObjectUtils, classNames } from '../utils/Utils'; import { InputMaskBase } from './InputMaskBase'; -import { PrimeReactContext } from '../api/Api'; export const InputMask = React.memo( React.forwardRef((inProps, ref) => { const context = React.useContext(PrimeReactContext); const props = InputMaskBase.getProps(inProps, context); - const elementRef = React.useRef(ref); + const elementRef = React.useRef(null); const firstNonMaskPos = React.useRef(null); const lastRequiredNonMaskPos = React.useRef(0); const tests = React.useRef([]); @@ -300,13 +300,15 @@ export const InputMask = React.memo( }; const writeBuffer = () => { - elementRef.current.value = buffer.current.join(''); + if (elementRef.current) { + elementRef.current.value = buffer.current.join(''); + } }; const checkVal = (allow) => { isValueChecked.current = true; //try to place characters where they belong - let test = elementRef.current.value, + let test = elementRef.current && elementRef.current.value, lastMatch = -1, i, c, @@ -347,7 +349,7 @@ export const InputMask = React.memo( if (props.autoClear || buffer.current.join('') === defaultBuffer.current) { // Invalid value. Remove it and replace it with the // mask, which is the default behavior. - if (elementRef.current.value) elementRef.current.value = ''; + if (elementRef.current && elementRef.current.value) elementRef.current.value = ''; clearBuffer(0, len.current); } else { // Invalid value, but we opt to show the value to the @@ -356,7 +358,10 @@ export const InputMask = React.memo( } } else { writeBuffer(); - elementRef.current.value = elementRef.current.value.substring(0, lastMatch + 1); + + if (elementRef.current) { + elementRef.current.value = elementRef.current.value.substring(0, lastMatch + 1); + } } return partialPosition.current ? i : firstNonMaskPos.current; @@ -372,9 +377,13 @@ export const InputMask = React.memo( clearTimeout(caretTimeoutId.current); let pos; - focusText.current = elementRef.current.value; + if (elementRef.current) { + focusText.current = elementRef.current.value; + } else { + focusText.current = ''; + } - pos = checkVal(); + pos = checkVal() || 0; caretTimeoutId.current = setTimeout(() => { if (elementRef.current !== document.activeElement) { @@ -390,7 +399,7 @@ export const InputMask = React.memo( } updateFilledState(); - }, 10); + }, 100); props.onFocus && props.onFocus(e); }; diff --git a/components/lib/inputtext/InputText.js b/components/lib/inputtext/InputText.js index 45de6d5ccb..c80df46eb6 100644 --- a/components/lib/inputtext/InputText.js +++ b/components/lib/inputtext/InputText.js @@ -1,10 +1,10 @@ import * as React from 'react'; import { PrimeReactContext } from '../api/Api'; +import { useHandleStyle } from '../componentbase/ComponentBase'; import { KeyFilter } from '../keyfilter/KeyFilter'; import { Tooltip } from '../tooltip/Tooltip'; import { DomHandler, ObjectUtils, mergeProps } from '../utils/Utils'; import { InputTextBase } from './InputTextBase'; -import { useHandleStyle } from '../componentbase/ComponentBase'; export const InputText = React.memo( React.forwardRef((inProps, ref) => {