mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-15 09:21:22 +08:00
2341a25d91
* more refactor * chore: motion support * chore: tmp test * test: Hooks * chore: static function * tmp of it * all of it * mv prefix * chore: clean up * chore: clean up * more test case * test: all base test * test: all test case * init * refactor: rm notification.open instance related code * follow up * refactor: singlton * test: notification test case * refactor to destroy * refactor: message base * test: part test case * test: more * test: more * test: all test * chore: clean up * docs: reorder * chore: fix lint * test: fix test case * chore: add act * chore: back * chore: fix style * test: notification test * test: more and more * test: fix more test * test: index * test: more & more * test: fix placement * test: fix coverage * chore: clean up * chore: bundle size * fix: 17 * chore: more * test: message * test: more test * fix: lint * test: rm class in static * chore: clean up * test: coverage * chore: fix lint
259 lines
6.0 KiB
TypeScript
Executable File
259 lines
6.0 KiB
TypeScript
Executable File
import * as React from 'react';
|
|
import { render } from 'rc-util/lib/React/render';
|
|
import useNotification, { useInternalNotification } from './useNotification';
|
|
import type { ArgsProps, NotificationInstance, GlobalConfigProps } from './interface';
|
|
import ConfigProvider, { globalConfig } from '../config-provider';
|
|
|
|
let notification: GlobalNotification | null = null;
|
|
|
|
let act: (callback: VoidFunction) => Promise<void> | void = (callback: VoidFunction) => callback();
|
|
|
|
interface GlobalNotification {
|
|
fragment: DocumentFragment;
|
|
instance?: NotificationInstance | null;
|
|
sync?: VoidFunction;
|
|
}
|
|
|
|
type Task =
|
|
| {
|
|
type: 'open';
|
|
config: ArgsProps;
|
|
}
|
|
| {
|
|
type: 'destroy';
|
|
key: React.Key;
|
|
};
|
|
|
|
let taskQueue: Task[] = [];
|
|
|
|
let defaultGlobalConfig: GlobalConfigProps = {};
|
|
|
|
function getGlobalContext() {
|
|
const {
|
|
prefixCls: globalPrefixCls,
|
|
getContainer: globalGetContainer,
|
|
rtl,
|
|
maxCount,
|
|
top,
|
|
bottom,
|
|
} = defaultGlobalConfig;
|
|
const mergedPrefixCls = globalPrefixCls ?? globalConfig().getPrefixCls('notification');
|
|
const mergedContainer = globalGetContainer?.() || document.body;
|
|
|
|
return {
|
|
prefixCls: mergedPrefixCls,
|
|
container: mergedContainer,
|
|
rtl,
|
|
maxCount,
|
|
top,
|
|
bottom,
|
|
};
|
|
}
|
|
|
|
interface GlobalHolderRef {
|
|
instance: NotificationInstance;
|
|
sync: () => void;
|
|
}
|
|
|
|
const GlobalHolder = React.forwardRef<GlobalHolderRef, {}>((_, ref) => {
|
|
const [prefixCls, setPrefixCls] = React.useState<string>();
|
|
const [container, setContainer] = React.useState<HTMLElement>();
|
|
const [maxCount, setMaxCount] = React.useState<number | undefined>();
|
|
const [rtl, setRTL] = React.useState<boolean | undefined>();
|
|
const [top, setTop] = React.useState<number | undefined>();
|
|
const [bottom, setBottom] = React.useState<number | undefined>();
|
|
|
|
const [api, holder] = useInternalNotification({
|
|
prefixCls,
|
|
getContainer: () => container!,
|
|
maxCount,
|
|
rtl,
|
|
top,
|
|
bottom,
|
|
});
|
|
|
|
const global = globalConfig();
|
|
const rootPrefixCls = global.getRootPrefixCls();
|
|
const rootIconPrefixCls = global.getIconPrefixCls();
|
|
|
|
const sync = () => {
|
|
const {
|
|
prefixCls: nextGlobalPrefixCls,
|
|
container: nextGlobalContainer,
|
|
maxCount: nextGlobalMaxCount,
|
|
rtl: nextGlobalRTL,
|
|
top: nextTop,
|
|
bottom: nextBottom,
|
|
} = getGlobalContext();
|
|
|
|
setPrefixCls(nextGlobalPrefixCls);
|
|
setContainer(nextGlobalContainer);
|
|
setMaxCount(nextGlobalMaxCount);
|
|
setRTL(nextGlobalRTL);
|
|
setTop(nextTop);
|
|
setBottom(nextBottom);
|
|
};
|
|
|
|
React.useEffect(sync, []);
|
|
|
|
React.useImperativeHandle(ref, () => {
|
|
const instance: any = { ...api };
|
|
|
|
Object.keys(instance).forEach(method => {
|
|
instance[method] = (...args: any[]) => {
|
|
sync();
|
|
return (api as any)[method](...args);
|
|
};
|
|
});
|
|
|
|
return {
|
|
instance,
|
|
sync,
|
|
};
|
|
});
|
|
|
|
return (
|
|
<ConfigProvider prefixCls={rootPrefixCls} iconPrefixCls={rootIconPrefixCls}>
|
|
{holder}
|
|
</ConfigProvider>
|
|
);
|
|
});
|
|
|
|
function flushNotice() {
|
|
if (!notification) {
|
|
const holderFragment = document.createDocumentFragment();
|
|
|
|
const newNotification: GlobalNotification = {
|
|
fragment: holderFragment,
|
|
};
|
|
|
|
notification = newNotification;
|
|
|
|
// Delay render to avoid sync issue
|
|
act(() => {
|
|
render(
|
|
<GlobalHolder
|
|
ref={node => {
|
|
const { instance, sync } = node || {};
|
|
|
|
Promise.resolve().then(() => {
|
|
if (!newNotification.instance && instance) {
|
|
newNotification.instance = instance;
|
|
newNotification.sync = sync;
|
|
flushNotice();
|
|
}
|
|
});
|
|
}}
|
|
/>,
|
|
holderFragment,
|
|
);
|
|
});
|
|
|
|
return;
|
|
}
|
|
|
|
// Notification not ready
|
|
if (notification && !notification.instance) {
|
|
return;
|
|
}
|
|
|
|
// >>> Execute task
|
|
taskQueue.forEach(task => {
|
|
// eslint-disable-next-line default-case
|
|
switch (task.type) {
|
|
case 'open': {
|
|
act(() => {
|
|
notification!.instance!.open({
|
|
...defaultGlobalConfig,
|
|
...task.config,
|
|
});
|
|
});
|
|
break;
|
|
}
|
|
|
|
case 'destroy':
|
|
act(() => {
|
|
notification?.instance!.destroy(task.key);
|
|
});
|
|
break;
|
|
}
|
|
});
|
|
|
|
// Clean up
|
|
taskQueue = [];
|
|
}
|
|
|
|
// ==============================================================================
|
|
// == Export ==
|
|
// ==============================================================================
|
|
const methods = ['success', 'info', 'warning', 'error'] as const;
|
|
type MethodType = typeof methods[number];
|
|
|
|
function setNotificationGlobalConfig(config: GlobalConfigProps) {
|
|
defaultGlobalConfig = {
|
|
...defaultGlobalConfig,
|
|
...config,
|
|
};
|
|
|
|
// Trigger sync for it
|
|
act(() => {
|
|
notification?.sync?.();
|
|
});
|
|
}
|
|
|
|
function open(config: ArgsProps) {
|
|
taskQueue.push({
|
|
type: 'open',
|
|
config,
|
|
});
|
|
flushNotice();
|
|
}
|
|
|
|
function destroy(key: React.Key) {
|
|
taskQueue.push({
|
|
type: 'destroy',
|
|
key,
|
|
});
|
|
flushNotice();
|
|
}
|
|
|
|
const baseStaticMethods: {
|
|
open: (config: ArgsProps) => void;
|
|
destroy: (key?: React.Key) => void;
|
|
config: any;
|
|
useNotification: typeof useNotification;
|
|
} = {
|
|
open,
|
|
destroy,
|
|
config: setNotificationGlobalConfig,
|
|
useNotification,
|
|
};
|
|
|
|
const staticMethods: typeof baseStaticMethods & Record<MethodType, (config: ArgsProps) => void> =
|
|
baseStaticMethods as any;
|
|
|
|
methods.forEach(type => {
|
|
staticMethods[type] = config =>
|
|
open({
|
|
...config,
|
|
type,
|
|
});
|
|
});
|
|
|
|
// ==============================================================================
|
|
// == Test ==
|
|
// ==============================================================================
|
|
const noop = () => {};
|
|
|
|
/** @private Only Work in test env */
|
|
// eslint-disable-next-line import/no-mutable-exports
|
|
export let actWrapper: (wrapper: any) => void = noop;
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
actWrapper = wrapper => {
|
|
act = wrapper;
|
|
};
|
|
}
|
|
|
|
export default staticMethods;
|