Skip to content

Commit

Permalink
Fix #1879: Input focus() should not scroll unless autoFocus=true (#2787)
Browse files Browse the repository at this point in the history
* Fix #1879: Input focus() should not scroll unless autoFocus=true

* Fix #1879: Input focus() should not scroll unless autoFocus=true

* Fix #1879: Input focus() should not scroll unless autoFocus=true

* Fix #1879: Input focus() should not scroll unless autoFocus=true

* Fix #1879: Input focus() should not scroll unless autoFocus=true
  • Loading branch information
melloware authored Jul 6, 2022
1 parent 899422f commit 41b4279
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 37 deletions.
10 changes: 5 additions & 5 deletions components/lib/autocomplete/AutoComplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const AutoComplete = React.memo(React.forwardRef((props, ref) => {
}

if (!preventInputFocus) {
inputRef.current.focus();
DomHandler.focus(inputRef);
hide();
}
}
Expand Down Expand Up @@ -200,7 +200,7 @@ export const AutoComplete = React.memo(React.forwardRef((props, ref) => {

const onDropdownClick = (event) => {
if (props.dropdownAutoFocus) {
inputRef.current.focus();
DomHandler.focus(inputRef, props.dropdownAutoFocus);
}

if (props.dropdownMode === 'blank')
Expand Down Expand Up @@ -383,7 +383,7 @@ export const AutoComplete = React.memo(React.forwardRef((props, ref) => {
}

const onMultiContainerClick = (event) => {
inputRef.current.focus();
DomHandler.focus(inputRef);

props.onClick && props.onClick(event);
}
Expand Down Expand Up @@ -427,8 +427,8 @@ export const AutoComplete = React.memo(React.forwardRef((props, ref) => {
setIdState(UniqueComponentId());
}

if (props.autoFocus && inputRef.current) {
inputRef.current.focus();
if (props.autoFocus) {
DomHandler.focus(inputRef, props.autoFocus);
}
});

Expand Down
2 changes: 1 addition & 1 deletion components/lib/calendar/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const Calendar = React.memo(React.forwardRef((props, ref) => {
const reFocusInputField = () => {
if (!props.inline && inputRef.current) {
ignoreFocusFunctionality.current = true;
inputRef.current.focus();
DomHandler.focus(inputRef);
}
}

Expand Down
6 changes: 3 additions & 3 deletions components/lib/cascadeselect/CascadeSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const CascadeSelect = React.memo(React.forwardRef((props, ref) => {

updateSelectionPath();
hide();
inputRef.current.focus();
DomHandler.focus(inputRef);
}

const onOptionGroupSelect = (event) => {
Expand Down Expand Up @@ -96,7 +96,7 @@ export const CascadeSelect = React.memo(React.forwardRef((props, ref) => {
}

if (!overlayRef.current || !overlayRef.current.contains(event.target)) {
inputRef.current.focus();
DomHandler.focus(inputRef);
overlayVisibleState ? hide() : show();
}
}
Expand Down Expand Up @@ -154,7 +154,7 @@ export const CascadeSelect = React.memo(React.forwardRef((props, ref) => {
const hide = () => {
props.onBeforeHide && props.onBeforeHide();
setOverlayVisibleState(false);
inputRef.current.focus();
DomHandler.focus(inputRef);
}

const onOverlayEnter = () => {
Expand Down
4 changes: 2 additions & 2 deletions components/lib/checkbox/Checkbox.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { useUpdateEffect } from '../hooks/Hooks';
import { Tooltip } from '../tooltip/Tooltip';
import { classNames, IconUtils, ObjectUtils } from '../utils/Utils';
import { classNames, DomHandler, IconUtils, ObjectUtils } from '../utils/Utils';

export const Checkbox = React.memo(React.forwardRef((props, ref) => {
const [focusedState, setFocusedState] = React.useState(false);
Expand Down Expand Up @@ -29,7 +29,7 @@ export const Checkbox = React.memo(React.forwardRef((props, ref) => {
});

inputRef.current.checked = !checked;
inputRef.current.focus();
DomHandler.focus(inputRef);
}
}

Expand Down
4 changes: 2 additions & 2 deletions components/lib/chips/Chips.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { KeyFilter } from '../keyfilter/KeyFilter';
import { Tooltip } from '../tooltip/Tooltip';
import { classNames, ObjectUtils } from '../utils/Utils';
import { classNames, DomHandler, ObjectUtils } from '../utils/Utils';

export const Chips = React.memo(React.forwardRef((props, ref) => {
const [focusedState, setFocusedState] = React.useState(false);
Expand Down Expand Up @@ -66,7 +66,7 @@ export const Chips = React.memo(React.forwardRef((props, ref) => {
}

const onWrapperClick = () => {
inputRef.current.focus();
DomHandler.focus(inputRef);
}

const onKeyDown = (event) => {
Expand Down
4 changes: 2 additions & 2 deletions components/lib/datatable/RowRadioButton.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { classNames } from '../utils/Utils';
import { classNames, DomHandler } from '../utils/Utils';

export const RowRadioButton = React.memo((props) => {
const [focusedState, setFocusedState] = React.useState(false);
Expand All @@ -17,7 +17,7 @@ export const RowRadioButton = React.memo((props) => {
if (!props.disabled) {
props.onChange(event);

inputRef.current.focus();
DomHandler.focus(inputRef);
}
}

Expand Down
8 changes: 4 additions & 4 deletions components/lib/dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const Dropdown = React.memo(React.forwardRef((props, ref) => {
return;
}
else if (!overlayRef.current || !(overlayRef.current && overlayRef.current.contains(event.target))) {
focusInputRef.current.focus();
DomHandler.focus(focusInputRef);
overlayVisibleState ? hide() : show();
}
}
Expand Down Expand Up @@ -345,7 +345,7 @@ export const Dropdown = React.memo(React.forwardRef((props, ref) => {

if (!option.disabled) {
selectItem(event);
focusInputRef.current.focus();
DomHandler.focus(focusInputRef);
}

hide();
Expand Down Expand Up @@ -549,8 +549,8 @@ export const Dropdown = React.memo(React.forwardRef((props, ref) => {
}, [inputRef, props.inputRef]);

useMountEffect(() => {
if (props.autoFocus && focusInputRef.current) {
focusInputRef.current.focus();
if (props.autoFocus) {
DomHandler.focus(focusInputRef, props.autoFocus);
}
});

Expand Down
6 changes: 3 additions & 3 deletions components/lib/dropdown/DropdownPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { localeOption } from '../api/Api';
import { CSSTransition } from '../csstransition/CSSTransition';
import { Portal } from '../portal/Portal';
import { classNames, ObjectUtils } from '../utils/Utils';
import { classNames, DomHandler, ObjectUtils } from '../utils/Utils';
import { VirtualScroller } from '../virtualscroller/VirtualScroller';
import { DropdownItem } from './DropdownItem';

Expand All @@ -25,7 +25,7 @@ export const DropdownPanel = React.memo(React.forwardRef((props, ref) => {
const onEntered = () => {
props.onEntered(() => {
if (props.filter && props.filterInputAutoFocus) {
filterInputRef.current.focus();
DomHandler.focus(filterInputRef, false);
}
});
}
Expand Down Expand Up @@ -101,7 +101,7 @@ export const DropdownPanel = React.memo(React.forwardRef((props, ref) => {

const createFilterClearIcon = () => {
if (props.showFilterClear && props.filterValue) {
return <i className="p-dropdown-filter-clear-icon pi pi-times" onClick={() => props.onFilterClearIconClick(() => filterInputRef.current.focus())}></i>
return <i className="p-dropdown-filter-clear-icon pi pi-times" onClick={() => props.onFilterClearIconClick(() => DomHandler.focus(filterInputRef))}></i>
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions components/lib/inputnumber/InputNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export const InputNumber = React.memo(React.forwardRef((props, ref) => {

const onUpButtonMouseDown = (event) => {
if (!props.disabled && !props.readOnly) {
props.autoFocus && inputRef.current.focus();
props.autoFocus && DomHandler.focus(inputRef, props.autoFocus);
repeat(event, null, 1);
event.preventDefault();
}
Expand Down Expand Up @@ -252,7 +252,7 @@ export const InputNumber = React.memo(React.forwardRef((props, ref) => {

const onDownButtonMouseDown = (event) => {
if (!props.disabled && !props.readOnly) {
props.autoFocus && inputRef.current.focus();
props.autoFocus && DomHandler.focus(inputRef, props.autoFocus);
repeat(event, null, -1);

event.preventDefault();
Expand Down
4 changes: 2 additions & 2 deletions components/lib/inputswitch/InputSwitch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { Tooltip } from '../tooltip/Tooltip';
import { classNames, ObjectUtils } from '../utils/Utils';
import { classNames, DomHandler, ObjectUtils } from '../utils/Utils';

export const InputSwitch = React.memo(React.forwardRef((props, ref) => {
const [focusedState, setFocusedState] = React.useState(false);
Expand All @@ -14,7 +14,7 @@ export const InputSwitch = React.memo(React.forwardRef((props, ref) => {
}

toggle(event);
inputRef.current.focus();
DomHandler.focus(inputRef);

event.preventDefault();
}
Expand Down
2 changes: 1 addition & 1 deletion components/lib/mention/Mention.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export const Mention = React.memo(React.forwardRef((props, ref) => {
}

const onItemClick = (event, suggestion) => {
inputRef.current.focus();
DomHandler.focus(inputRef);
selectItem(event, suggestion);
}

Expand Down
7 changes: 3 additions & 4 deletions components/lib/multiselect/MultiSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const MultiSelect = React.memo(React.forwardRef((props, ref) => {
//escape
case 27:
hide();
inputRef.current.focus();
DomHandler.focus(inputRef);
break;

default:
Expand All @@ -105,8 +105,7 @@ export const MultiSelect = React.memo(React.forwardRef((props, ref) => {
const onClick = (event) => {
if (!props.disabled && !isPanelClicked(event) && !DomHandler.hasClass(event.target, 'p-multiselect-token-icon') && !isClearClicked(event)) {
overlayVisibleState ? hide() : show();
inputRef.current.focus();

DomHandler.focus(inputRef);
event.preventDefault();
}
}
Expand Down Expand Up @@ -267,7 +266,7 @@ export const MultiSelect = React.memo(React.forwardRef((props, ref) => {

const onCloseClick = (event) => {
hide();
inputRef.current.focus();
DomHandler.focus(inputRef);
event.preventDefault();
event.stopPropagation();
}
Expand Down
4 changes: 2 additions & 2 deletions components/lib/radiobutton/RadioButton.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { Tooltip } from '../tooltip/Tooltip';
import { classNames, ObjectUtils } from '../utils/Utils';
import { classNames, DomHandler, ObjectUtils } from '../utils/Utils';

export const RadioButton = React.memo(React.forwardRef((props, ref) => {
const [focusedState, setFocusedState] = React.useState(false);
Expand Down Expand Up @@ -29,7 +29,7 @@ export const RadioButton = React.memo(React.forwardRef((props, ref) => {
});

inputRef.current.checked = !props.checked;
inputRef.current.focus();
DomHandler.focus(inputRef);
}
}

Expand Down
4 changes: 2 additions & 2 deletions components/lib/terminal/Terminal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { TerminalService } from '../terminalservice/TerminalService';
import { classNames, ObjectUtils } from '../utils/Utils';
import { classNames, DomHandler, ObjectUtils } from '../utils/Utils';

export const Terminal = React.memo(React.forwardRef((props, ref) => {
const [commandTextState, setCommandTextState] = React.useState('');
Expand All @@ -12,7 +12,7 @@ export const Terminal = React.memo(React.forwardRef((props, ref) => {
const isEmitted = React.useRef(false);

const onClick = () => {
inputRef.current.focus();
DomHandler.focus(inputRef);
}

const onInputChange = (e) => {
Expand Down
4 changes: 2 additions & 2 deletions components/lib/treeselect/TreeSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const TreeSelect = React.memo(React.forwardRef((props, ref) => {

const onClick = (event) => {
if (!props.disabled && (!overlayRef.current || !overlayRef.current.contains(event.target)) && !DomHandler.hasClass(event.target, 'p-treeselect-close')) {
focusInputRef.current.focus();
DomHandler.focus(focusInputRef);
overlayVisibleState ? hide() : show();
}
}
Expand Down Expand Up @@ -170,7 +170,7 @@ export const TreeSelect = React.memo(React.forwardRef((props, ref) => {
bindOverlayListener();

if (props.filter && props.filterInputAutoFocus) {
filterInputRef.current.focus();
DomHandler.focus(filterInputRef);
}

props.onShow && props.onShow();
Expand Down
13 changes: 13 additions & 0 deletions components/lib/utils/DomHandler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ObjectUtils } from "./Utils";

export default class DomHandler {

static innerWidth(el) {
Expand Down Expand Up @@ -786,6 +788,17 @@ export default class DomHandler {
return focusableElements.length > 0 ? focusableElements[focusableElements.length - 1] : null;
}

/**
* Focus an input element if it does not already have focus.
*
* @param {MutableRefObject} inputRef the input reference
* @param {boolean} scrollTo flag to control whether to scroll to the element, false by default
*/
static focus(inputRef, scrollTo) {
const preventScroll = ObjectUtils.isEmpty(scrollTo) ? true : !scrollTo;
inputRef && inputRef.current && document.activeElement !== inputRef.current && inputRef.current.focus({ preventScroll: preventScroll });
}

static getCursorOffset(el, prevText, nextText, currentText) {
if (el) {
let style = getComputedStyle(el);
Expand Down

0 comments on commit 41b4279

Please sign in to comment.