You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
8.1 KiB

feat: prepend use-client directive for with Next.js App Router (#43573) * fix: prepend use-client directive for with Next.js App Router * Update components/affix/index.tsx Co-authored-by: MadCcc <1075746765@qq.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/badge/index.tsx Co-authored-by: MadCcc <1075746765@qq.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/index.tsx Co-authored-by: MadCcc <1075746765@qq.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/cascader/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/list/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/mentions/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/mentions/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/mentions/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/mentions/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/qrcode/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/spin/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/select/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/spin/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/spin/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/steps/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/time-picker/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/transfer/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/tree-select/index.tsx Signed-off-by: lijianan <574980606@qq.com> --------- Signed-off-by: lijianan <574980606@qq.com> Co-authored-by: MadCcc <1075746765@qq.com>
1 year ago
'use client';
import { render } from 'rc-util/lib/React/render';
import * as React from 'react';
import ConfigProvider, { globalConfig, warnContext } from '../config-provider';
import PurePanel from './PurePanel';
import type {
ArgsProps,
ConfigOptions,
MessageInstance,
MessageType,
NoticeType,
TypeOpen,
} from './interface';
import useMessage, { useInternalMessage } from './useMessage';
import { wrapPromiseFn } from './util';
export { ArgsProps };
let message: GlobalMessage | null = null;
let act: (callback: VoidFunction) => Promise<void> | void = (callback) => callback();
interface GlobalMessage {
fragment: DocumentFragment;
instance?: MessageInstance | null;
sync?: VoidFunction;
}
interface OpenTask {
type: 'open';
config: ArgsProps;
resolve: VoidFunction;
setCloseFn: (closeFn: VoidFunction) => void;
skipped?: boolean;
}
interface TypeTask {
type: NoticeType;
args: Parameters<TypeOpen>;
resolve: VoidFunction;
setCloseFn: (closeFn: VoidFunction) => void;
skipped?: boolean;
}
type Task =
| OpenTask
| TypeTask
| {
type: 'destroy';
key: React.Key;
skipped?: boolean;
};
let taskQueue: Task[] = [];
let defaultGlobalConfig: ConfigOptions = {};
function getGlobalContext() {
const {
prefixCls: globalPrefixCls,
getContainer: globalGetContainer,
duration,
rtl,
maxCount,
top,
} = defaultGlobalConfig;
const mergedPrefixCls = globalPrefixCls ?? globalConfig().getPrefixCls('message');
const mergedContainer = globalGetContainer?.() || document.body;
return {
prefixCls: mergedPrefixCls,
container: mergedContainer,
duration,
rtl,
maxCount,
top,
};
}
interface GlobalHolderRef {
instance: MessageInstance;
sync: () => void;
}
const GlobalHolder = React.forwardRef<GlobalHolderRef, {}>((_, ref) => {
const initializeMessageConfig: () => ConfigOptions = () => {
const { prefixCls, container, maxCount, duration, rtl, top } = getGlobalContext();
return {
prefixCls,
getContainer: () => container!,
maxCount,
duration,
rtl,
top,
};
};
const [messageConfig, setMessageConfig] = React.useState<ConfigOptions>(initializeMessageConfig);
const [api, holder] = useInternalMessage(messageConfig);
const global = globalConfig();
const rootPrefixCls = global.getRootPrefixCls();
const rootIconPrefixCls = global.getIconPrefixCls();
const theme = global.getTheme();
const sync = () => {
setMessageConfig(initializeMessageConfig);
};
React.useEffect(sync, []);
React.useImperativeHandle(ref, () => {
const instance: MessageInstance = { ...api };
Object.keys(instance).forEach((method: keyof MessageInstance) => {
instance[method] = (...args: any[]) => {
sync();
return (api as any)[method](...args);
};
});
return {
instance,
sync,
};
});
return (
<ConfigProvider prefixCls={rootPrefixCls} iconPrefixCls={rootIconPrefixCls} theme={theme}>
{holder}
</ConfigProvider>
);
});
function flushNotice() {
if (!message) {
const holderFragment = document.createDocumentFragment();
const newMessage: GlobalMessage = {
fragment: holderFragment,
};
message = newMessage;
// Delay render to avoid sync issue
act(() => {
render(
<GlobalHolder
ref={(node) => {
const { instance, sync } = node || {};
// React 18 test env will throw if call immediately in ref
Promise.resolve().then(() => {
if (!newMessage.instance && instance) {
newMessage.instance = instance;
newMessage.sync = sync;
flushNotice();
}
});
}}
/>,
holderFragment,
);
});
return;
}
// Notification not ready
if (!message.instance) {
return;
}
// >>> Execute task
taskQueue.forEach((task) => {
const { type, skipped } = task;
// Only `skipped` when user call notice but cancel it immediately
// and instance not ready
if (!skipped) {
switch (type) {
case 'open': {
act(() => {
const closeFn = message!.instance!.open({
...defaultGlobalConfig,
...task.config,
});
closeFn?.then(task.resolve);
task.setCloseFn(closeFn);
});
break;
}
case 'destroy':
act(() => {
message?.instance!.destroy(task.key);
});
break;
// Other type open
default: {
act(() => {
const closeFn = message!.instance![type](...task.args);
closeFn?.then(task.resolve);
task.setCloseFn(closeFn);
});
}
}
}
});
// Clean up
taskQueue = [];
}
// ==============================================================================
// == Export ==
// ==============================================================================
function setMessageGlobalConfig(config: ConfigOptions) {
defaultGlobalConfig = {
...defaultGlobalConfig,
...config,
};
// Trigger sync for it
act(() => {
message?.sync?.();
});
}
function open(config: ArgsProps): MessageType {
const result = wrapPromiseFn((resolve) => {
let closeFn: VoidFunction;
const task: OpenTask = {
type: 'open',
config,
resolve,
setCloseFn: (fn) => {
closeFn = fn;
},
};
taskQueue.push(task);
return () => {
if (closeFn) {
act(() => {
closeFn();
});
} else {
task.skipped = true;
}
};
});
flushNotice();