Skip to content

Commit

Permalink
fix: pure render should also check latest popup element (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Jun 22, 2021
1 parent 9c7a949 commit 3ad6556
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/generate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ export default function generateSelector<
}

useSelectTriggerControl(
[containerRef.current, triggerRef.current && triggerRef.current.getPopupElement()],
() => [containerRef.current, triggerRef.current?.getPopupElement()],
triggerOpen,
onToggleOpen,
);
Expand Down Expand Up @@ -897,8 +897,7 @@ export default function generateSelector<

const onInternalMouseDown: React.MouseEventHandler<HTMLDivElement> = (event, ...restArgs) => {
const { target } = event;
const popupElement: HTMLDivElement =
triggerRef.current && triggerRef.current.getPopupElement();
const popupElement: HTMLDivElement = triggerRef.current?.getPopupElement();

// We should give focus back to selector if clicked item is not focusable
if (popupElement && popupElement.contains(target as HTMLElement)) {
Expand Down
7 changes: 4 additions & 3 deletions src/hooks/useSelectTriggerControl.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as React from 'react';

export default function useSelectTriggerControl(
elements: (HTMLElement | undefined)[],
elements: () => (HTMLElement | undefined)[],
open: boolean,
triggerOpen: (open: boolean) => void,
) {
const propsRef = React.useRef(null);
propsRef.current = {
elements: elements.filter(e => e),
open,
triggerOpen,
};
Expand All @@ -22,7 +21,9 @@ export default function useSelectTriggerControl(

if (
propsRef.current.open &&
propsRef.current.elements.every(element => !element.contains(target) && element !== target)
elements()
.filter((element) => element)
.every((element) => !element.contains(target) && element !== target)
) {
// Should trigger close
propsRef.current.triggerOpen(false);
Expand Down

1 comment on commit 3ad6556

@vercel
Copy link

@vercel vercel bot commented on 3ad6556 Jun 22, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.