Skip to content

Commit d41af63

Browse files
committed
[fields] Fix input focus when input is not on main document
1 parent 9139d0b commit d41af63

File tree

1 file changed

+4
-3
lines changed
  • packages/x-date-pickers/src/internals/hooks/useField

1 file changed

+4
-3
lines changed

packages/x-date-pickers/src/internals/hooks/useField/useField.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ export const useField = <
426426
// On multi input range pickers we want to update selection range only for the active input
427427
// This helps to avoid the focus jumping on Safari https://github.com/mui/mui-x/issues/9003
428428
// because WebKit implements the `setSelectionRange` based on the spec: https://bugs.webkit.org/show_bug.cgi?id=224425
429-
if (inputRef.current === getActiveElement(document)) {
429+
if (inputRef.current === getActiveElement(inputRef.current.ownerDocument)) {
430430
inputRef.current.setSelectionRange(selectionStart, selectionEnd);
431431
}
432432
// Even reading this variable seems to do the trick, but also setting it just to make use of it
@@ -459,7 +459,7 @@ export const useField = <
459459

460460
React.useEffect(() => {
461461
// Select the right section when focused on mount (`autoFocus = true` on the input)
462-
if (inputRef.current && inputRef.current === document.activeElement) {
462+
if (inputRef.current && inputRef.current === inputRef.current.ownerDocument.activeElement) {
463463
setSelectedSections('all');
464464
}
465465

@@ -495,7 +495,8 @@ export const useField = <
495495
return 'numeric';
496496
}, [selectedSectionIndexes, state.sections]);
497497

498-
const inputHasFocus = inputRef.current && inputRef.current === getActiveElement(document);
498+
const inputHasFocus =
499+
inputRef.current && inputRef.current === getActiveElement(inputRef.current.ownerDocument);
499500
const areAllSectionsEmpty = valueManager.areValuesEqual(
500501
utils,
501502
state.value,

0 commit comments

Comments
 (0)