Skip to content
21 changes: 19 additions & 2 deletions packages/mui-material/src/InputBase/InputBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,31 @@ const InputBase = React.forwardRef(function InputBase(inProps, ref) {
}, []);

const handleClick = (event) => {
if (inputRef.current && event.currentTarget === event.target) {
inputRef.current.focus();
const input = inputRef.current;

if (
input &&
(input instanceof HTMLInputElement || input instanceof HTMLTextAreaElement) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not use instanceof as it's inconsistent across window contexts

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!
I’ve removed the instanceof check and replaced it with a safer, window-agnostic guard (checking for .setSelectionRange and .focus).

The updated approach no longer depends on instanceof and works correctly across window contexts.

event.currentTarget === event.target
) {
const pos =
typeof input.selectionStart === 'number' ? input.selectionStart : input.value.length;

input.focus();

requestAnimationFrame(() => {
const restorePos =
pos === 0 && event.target === event.currentTarget ? input.value.length : pos;

input.setSelectionRange(restorePos, restorePos);
});
}

if (onClick) {
onClick(event);
}
};

let InputComponent = inputComponent;
let inputProps = inputPropsProp;

Expand Down
Loading