Skip to content

Commit

Permalink
try preserve focus
Browse files Browse the repository at this point in the history
  • Loading branch information
VasilyStrelyaev committed Sep 4, 2024
1 parent a567376 commit 9b6375a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/devextreme-react/src/core/component-base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const ComponentBase = forwardRef<ComponentBaseRef, any>(
const useDeferUpdateForTemplates = useRef(false);
const guardsUpdateScheduled = useRef(false);
const childElementsDetached = useRef(false);
const isFocused = useRef(false);
const optionsManager = useRef<OptionsManager>(new OptionsManager());
const childNodes = useRef<Node[]>();

Expand Down Expand Up @@ -230,6 +231,10 @@ const ComponentBase = forwardRef<ComponentBaseRef, any>(

instance.current = new WidgetClass(el, options);

if (isFocused.current && instance.current.focus) {
instance.current.focus();
}

if (!useRequestAnimationFrameFlag) {
useDeferUpdateForTemplates.current = instance.current.option(
'integrationOptions.useDeferUpdateForTemplates',
Expand Down Expand Up @@ -312,11 +317,16 @@ const ComponentBase = forwardRef<ComponentBaseRef, any>(
if (instance.current) {
const dxRemoveArgs: DXRemoveCustomArgs = { isUnmounting: true };

isFocused.current = !!element.current?.contains(document.activeElement);
childNodes.current?.forEach((child) => child.parentNode?.removeChild(child));
childElementsDetached.current = true;

if (element.current) {
const preventFocusOut = (e: FocusEvent) => e.stopPropagation();

events.on(element.current, 'focusout', preventFocusOut);
events.triggerHandler(element.current, DX_REMOVE_EVENT, dxRemoveArgs);
events.off(element.current, 'focusout', preventFocusOut);
}

instance.current.dispose();
Expand All @@ -331,6 +341,7 @@ const ComponentBase = forwardRef<ComponentBaseRef, any>(
element.current,
optionsManager.current,
childElementsDetached.current,
isFocused.current,
]);

useLayoutEffect(() => {
Expand Down

0 comments on commit 9b6375a

Please sign in to comment.