Navigation Menu

Skip to content

Commit

Permalink
fix: useMessage() should use getPopupContainer from <ConfigProvider /…
Browse files Browse the repository at this point in the history
…>… (#31939)

* fix:useMessage() should use getPopupContainer from <ConfigProvider /> #31841

* fix: useMessage() should use getPopupContainer from <ConfigProvider /> #31841

* add test case for useMessage hook

* restore some changes

* try to restore files agains

* restore and keep the same as upstream master

* restore files

* restore some definition
  • Loading branch information
DaoxingHuang committed Sep 13, 2021
1 parent 94e3e50 commit af422c9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
36 changes: 36 additions & 0 deletions components/message/__tests__/hooks.test.js
Expand Up @@ -192,4 +192,40 @@ describe('message.hooks', () => {

mount(<Demo />);
});

it("should use ConfigProvider's getPopupContainer as message container", () => {

This comment has been minimized.

Copy link
@JounQin

JounQin Sep 13, 2021

Contributor

There is a redundant space after should.

This comment has been minimized.

Copy link
@afc163

afc163 Sep 13, 2021

Member

PR welcome

const containerId = 'container';
const getPopupContainer = () => {
const div = document.createElement('div');
div.id = containerId;
document.body.appendChild(div);
return div;
};
const Demo = () => {
const [api, holder] = message.useMessage();
return (
<ConfigProvider getPopupContainer={getPopupContainer} prefixCls="my-test">
{holder}
<button
type="button"
onClick={() => {
api.success({
content: <span className="hook-content">happy</span>,
duration: 0,
});
}}
/>
</ConfigProvider>
);
};

const wrapper = mount(<Demo />);

wrapper.find('button').simulate('click');
expect(document.querySelectorAll('.my-test-message-notice').length).toBe(1);
expect(document.querySelectorAll('.anticon-check-circle').length).toBe(1);
expect(document.querySelector('.hook-content').innerHTML).toEqual('happy');
expect(document.querySelectorAll(`#${containerId}`).length).toBe(1);
expect(wrapper.find(`#${containerId}`).children.length).toBe(1);
});
});
4 changes: 3 additions & 1 deletion components/message/hooks/useMessage.tsx
Expand Up @@ -24,6 +24,7 @@ export default function createUseMessage(
const useMessage = (): [MessageInstance, React.ReactElement] => {
// We can only get content by render
let getPrefixCls: ConfigConsumerProps['getPrefixCls'];
let getPopupContainer: ConfigConsumerProps['getPopupContainer'];

// We create a proxy to handle delay created instance
let innerInstance: RCNotificationInstance | null = null;
Expand Down Expand Up @@ -52,6 +53,7 @@ export default function createUseMessage(
...args,
prefixCls: mergedPrefixCls,
rootPrefixCls,
getPopupContainer,
},
({ prefixCls, instance }) => {
innerInstance = instance;
Expand Down Expand Up @@ -83,7 +85,7 @@ export default function createUseMessage(
hookApiRef.current,
<ConfigConsumer key="holder">
{(context: ConfigConsumerProps) => {
({ getPrefixCls } = context);
({ getPrefixCls, getPopupContainer } = context);
return holder;
}}
</ConfigConsumer>,
Expand Down
5 changes: 3 additions & 2 deletions components/message/index.tsx
Expand Up @@ -78,7 +78,7 @@ function getRCNotificationInstance(
instance: RCNotificationInstance;
}) => void,
) {
const { prefixCls: customizePrefixCls } = args;
const { prefixCls: customizePrefixCls, getPopupContainer: getContextPopupContainer } = args;
const { getPrefixCls, getRootPrefixCls, getIconPrefixCls } = globalConfig();
const prefixCls = getPrefixCls('message', customizePrefixCls || localPrefixCls);
const rootPrefixCls = getRootPrefixCls(args.rootPrefixCls, prefixCls);
Expand All @@ -93,7 +93,7 @@ function getRCNotificationInstance(
prefixCls,
transitionName: hasTransitionName ? transitionName : `${rootPrefixCls}-${transitionName}`,
style: { top: defaultTop }, // 覆盖原来的样式
getContainer,
getContainer: getContainer || getContextPopupContainer,
maxCount,
};

Expand Down Expand Up @@ -133,6 +133,7 @@ export interface ArgsProps {
type: NoticeType;
prefixCls?: string;
rootPrefixCls?: string;
getPopupContainer?: (triggerNode: HTMLElement) => HTMLElement;
onClose?: () => void;
icon?: React.ReactNode;
key?: string | number;
Expand Down

0 comments on commit af422c9

Please sign in to comment.