Skip to content

Commit

Permalink
Removed extra user hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris Sekachev committed Feb 15, 2021
1 parent 302256a commit 490a5c7
Showing 1 changed file with 2 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,13 @@
//
// SPDX-License-Identifier: MIT

import React, { useEffect, useState } from 'react';
import React, { useState } from 'react';
import Popover, { PopoverProps } from 'antd/lib/popover';

type PopoverTypeListener = (activePopover: string | null) => void;

let listeners: PopoverTypeListener[] = [];
function updateActivePopoverType(activePopover: string | null): void {
for (const listener of listeners) {
listener(activePopover);
}
}

function subscribePopoverUpdate(onUpdate: PopoverTypeListener): void {
listeners.push(onUpdate);
}

function unsubscribePopoverUpdate(onUpdate: PopoverTypeListener): void {
listeners = listeners.filter((listener: PopoverTypeListener) => listener !== onUpdate);
}

function useCurrentActivePopover(): string | null {
const [activePopover, setActivePopover] = useState<string | null>(null);

useEffect(() => {
const listener: PopoverTypeListener = (newActivePopover: string | null) => {
// updating the state leads to rerender of dependent components
setActivePopover(newActivePopover);
};

// subscribe on mount and unsubscribe on unmount
subscribePopoverUpdate(listener);
return () => unsubscribePopoverUpdate(listener);
}, []);

return activePopover;
}

export default function withVisibilityHandling(WrappedComponent: typeof Popover, popoverType: string) {
return (props: PopoverProps): JSX.Element => {
const [initialized, setInitialized] = useState<boolean>(false);
const [visible, setVisible] = useState<boolean>(false);
const currentActivePopover = useCurrentActivePopover();
const { overlayClassName, ...rest } = props;
const overlayClassNames = typeof overlayClassName === 'string' ? overlayClassName.split(/\s+/) : [];

Expand All @@ -56,15 +21,14 @@ export default function withVisibilityHandling(WrappedComponent: typeof Popover,

const callback = (event: Event): void => {
if ((event as AnimationEvent).animationName === 'antZoomBigIn') {
updateActivePopoverType(popoverType);
setVisible(true);
}
};

return (
<WrappedComponent
{...rest}
trigger={visible && currentActivePopover === popoverType ? 'click' : 'hover'}
trigger={visible ? 'click' : 'hover'}
overlayClassName={overlayClassNames.join(' ').trim()}
onVisibleChange={(_visible: boolean) => {
if (!_visible) {
Expand Down

0 comments on commit 490a5c7

Please sign in to comment.