Skip to content

Commit

Permalink
fix click outside
Browse files Browse the repository at this point in the history
  • Loading branch information
foyarash committed Dec 11, 2024
1 parent cba1d26 commit e742cb0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 7 additions & 3 deletions packages/next-admin/src/components/inputs/Selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type SelectorProps = {
export const Selector = forwardRef<HTMLDivElement, SelectorProps>(
({ open, name, onChange, options, selectedOptions }, ref) => {
const currentQuery = useRef("");
const searchInput = createRef<HTMLInputElement>();
const searchInput = useRef<HTMLInputElement>(null);
const { t } = useI18n();
const {
allOptions,
Expand All @@ -45,7 +45,6 @@ export const Selector = forwardRef<HTMLDivElement, SelectorProps>(
});

const containerRef = useRef<HTMLDivElement>(null);
useImperativeHandle(ref, () => containerRef.current!);

useEffect(() => {
if (open && searchInput.current) {
Expand Down Expand Up @@ -121,7 +120,12 @@ export const Selector = forwardRef<HTMLDivElement, SelectorProps>(
leave="transition-all ease-linear"
leaveFrom="opacity-100 translate-y-0"
leaveTo="opacity-0 -translate-y-1"
ref={containerRef}
ref={(r) => {
containerRef.current = r as HTMLDivElement;
if (ref && typeof ref === "object") {
ref.current = r as HTMLDivElement;
}
}}
onScroll={onScroll}
>
<div className="relative flex flex-col">
Expand Down
10 changes: 9 additions & 1 deletion packages/next-admin/src/hooks/useCloseOnOutsideClick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ const useClickOutside = <E extends HTMLElement = HTMLElement>(
}
};

document.addEventListener("click", handleClickOutside);
setTimeout(() => {
/**
* There is a weird behavior on Page Router where this triggers
* just when a selector is opened. I suppose this is because the click event is not finished
* dispatching when we click on a trigger, but this is strange that
* this does not occur in App Router
*/
document.addEventListener("click", handleClickOutside);
});
return () => {
document.removeEventListener("click", handleClickOutside);
};
Expand Down

0 comments on commit e742cb0

Please sign in to comment.