Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Popover 의 flag 속성을 추가했다 #35

Merged
merged 2 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 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 @@ -133,8 +140,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>
);
},
};