Skip to content

Commit

Permalink
Fix primefaces#5537 InputNumber text focus
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Jan 9, 2024
1 parent 05e6120 commit 66a9482
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions components/lib/inputnumber/InputNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const InputNumber = React.memo(
const inputRef = React.useRef(null);
const timer = React.useRef(null);
const lastValue = React.useRef(null);

const numberFormat = React.useRef(null);
const groupChar = React.useRef(null);
const prefixChar = React.useRef(null);
Expand All @@ -43,6 +42,7 @@ export const InputNumber = React.memo(
const _suffix = React.useRef(null);
const _prefix = React.useRef(null);
const _index = React.useRef(null);
const isFocusedByClick = React.useRef(false);

const _locale = props.locale || (context && context.locale) || PrimeReact.locale;
const stacked = props.showButtons && props.buttonLayout === 'stacked';
Expand Down Expand Up @@ -726,6 +726,10 @@ export const InputNumber = React.memo(
return index || 0;
};

const onInputPointerDown = () => {
isFocusedByClick.current = true;
};

const onInputClick = () => {
initCursor();
};
Expand Down Expand Up @@ -959,17 +963,22 @@ export const InputNumber = React.memo(
setFocusedState(true);
props.onFocus && props.onFocus(event);

if ((props.suffix || props.currency || props.prefix) && inputRef.current) {
// GitHub #1866 Cursor must be placed before/after symbol or arrow keys don't work
const selectionEnd = initCursor();
if ((props.suffix || props.currency || props.prefix) && inputRef.current && !isFocusedByClick.current) {
// GitHub #1866,#5537
let inputValue = inputRef.current.value;
let prefixLength = (prefixChar.current || '').length;
let suffixLength = (suffixChar.current || '').length;
let end = inputValue.length === 0 ? 0 : inputValue.length - suffixLength;

inputRef.current.setSelectionRange(selectionEnd, selectionEnd);
inputRef.current.setSelectionRange(prefixLength, end);
}
};

const onInputBlur = (event) => {
setFocusedState(false);

isFocusedByClick.current = false;

if (inputRef.current) {
let currentValue = inputRef.current.value;

Expand Down Expand Up @@ -1066,13 +1075,14 @@ export const InputNumber = React.memo(
readOnly={props.readOnly}
name={props.name}
autoFocus={props.autoFocus}
onKeyDown={onInputKeyDown}
onKeyPress={onInputKeyUp}
onInput={onInput}
onClick={onInputClick}
onBlur={onInputBlur}
onClick={onInputClick}
onFocus={onInputFocus}
onInput={onInput}
onKeyDown={onInputKeyDown}
onKeyPress={onInputKeyUp}
onPaste={onPaste}
onPointerDown={onInputPointerDown}
min={props.min}
max={props.max}
aria-valuemin={props.min}
Expand Down

0 comments on commit 66a9482

Please sign in to comment.