Skip to content

Commit

Permalink
popover 의 flag 속성을 추가했다
Browse files Browse the repository at this point in the history
  • Loading branch information
healtheloper committed Sep 22, 2023
1 parent 96e96de commit b0ed64c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/co-design-core/src/components/Popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export interface PopoverProps extends CoComponentProps<PopoverStylesNames>, Omit
*/
opened?: boolean;

/**
* Popover 의 open 상태를 자식 컴포넌트가 제어할 때 사용합니다.
* 만약 자식 컴포넌트에게 제어권을 주고, 강제로 open 상태를 toggle 하고 싶다면 flag 를 사용합니다.
*/
flag?: boolean;

/** Popover 컴포넌트에 포함시킬 요소를 넣습니다. */
content: React.ReactNode;

Expand Down Expand Up @@ -84,6 +90,7 @@ const getPositionStyle = (placement: PopoverPlacement, target?: HTMLElement) =>
export const Popover = ({
children,
opened,
flag,
content,
contentPadding = 'medium',
withArrow = true,
Expand Down Expand Up @@ -115,6 +122,7 @@ export const Popover = ({
!targetRef.current.contains(e.target as HTMLElement) &&
!balloonRef.current.contains(e.target as HTMLElement)
) {
console.log('hhh');
setCurrentOpened(false);
}
});
Expand All @@ -133,8 +141,8 @@ export const Popover = ({
const handleBlur = trigger === 'focus' ? () => setCurrentOpened(false) : undefined;

useUpdateEffect(() => {
setCurrentOpened(opened);
}, [opened]);
setCurrentOpened((prev) => !prev);
}, [flag]);

useUpdateEffect(() => {
if (currentOpened) onOpen?.();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Modal } from '../../Modal';
import { Center } from '../../Center';
import { Popover } from '../Popover';
import { useToggle } from '@co-design/hooks';
import { Menu } from '../../Menu';

export default {
title: '@co-design/core/Popover',
Expand Down Expand Up @@ -106,3 +107,47 @@ export const InModal = {
);
},
};

export const OpenByChildren = {
render: (props) => {
return (
<Center style={{ width: 500, height: 500 }}>
<Popover placement="bottom" content={<Content />} {...props}>
<button>Popover</button>
</Popover>
</Center>
);
},
};

export const OpenByChildrenWithFlag = {
render: (props) => {
const [flag, toggleFlag] = useToggle(false);

return (
<Center style={{ width: 500, height: 500 }}>
<Popover
flag={flag}
placement="bottom"
content={
<Menu>
<Menu.Item>Not close</Menu.Item>
<Menu.Item>Not close</Menu.Item>
<Menu.Item
onClick={() => {
toggleFlag();
}}
>
If click me, Popover close
</Menu.Item>
<Menu.Item>Not close</Menu.Item>
</Menu>
}
{...props}
>
<button>Popover</button>
</Popover>
</Center>
);
},
};

0 comments on commit b0ed64c

Please sign in to comment.