2023-09-11 17:28:04 +08:00
|
|
|
import * as React from 'react';
|
2022-05-10 15:43:29 +08:00
|
|
|
import rcWarning, { resetWarned } from 'rc-util/lib/warning';
|
|
|
|
|
|
|
|
export { resetWarned };
|
|
|
|
export function noop() {}
|
|
|
|
|
2022-08-21 23:25:00 +08:00
|
|
|
type Warning = (valid: boolean, component: string, message?: string) => void;
|
2022-05-10 15:43:29 +08:00
|
|
|
|
|
|
|
// eslint-disable-next-line import/no-mutable-exports
|
|
|
|
let warning: Warning = noop;
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
warning = (valid, component, message) => {
|
|
|
|
rcWarning(valid, `[antd: ${component}] ${message}`);
|
|
|
|
|
|
|
|
// StrictMode will inject console which will not throw warning in React 17.
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
resetWarned();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
type BaseTypeWarning = (
|
2023-09-11 17:28:04 +08:00
|
|
|
valid: boolean,
|
|
|
|
/**
|
|
|
|
* - deprecated: Some API will be removed in future but still support now.
|
|
|
|
* - usage: Some API usage is not correct.
|
|
|
|
* - breaking: Breaking change like API is removed.
|
|
|
|
*/
|
|
|
|
type: 'deprecated' | 'usage' | 'breaking',
|
|
|
|
message?: string,
|
|
|
|
) => void;
|
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
type TypeWarning = BaseTypeWarning & {
|
|
|
|
deprecated: (valid: boolean, oldProp: string, newProp: string, message?: string) => void;
|
|
|
|
};
|
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
export interface WarningContextProps {
|
|
|
|
deprecated?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const WarningContext = React.createContext<WarningContextProps>({});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a hook but we not named as `useWarning`
|
|
|
|
* since this is only used in development.
|
|
|
|
* We should always wrap this in `if (process.env.NODE_ENV !== 'production')` condition
|
|
|
|
*/
|
2023-09-13 22:07:33 +08:00
|
|
|
export const devUseWarning: (component: string) => TypeWarning =
|
2023-09-11 17:28:04 +08:00
|
|
|
process.env.NODE_ENV !== 'production'
|
2023-09-13 22:07:33 +08:00
|
|
|
? (component: string) => {
|
2023-09-11 17:28:04 +08:00
|
|
|
const { deprecated } = React.useContext(WarningContext);
|
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
const typeWarning: TypeWarning = (valid, type, message) => {
|
2023-09-11 17:28:04 +08:00
|
|
|
if (deprecated !== false || type !== 'deprecated') {
|
|
|
|
warning(valid, component, message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
typeWarning.deprecated = (valid, oldProp, newProp, message) => {
|
|
|
|
typeWarning(
|
|
|
|
valid,
|
|
|
|
'deprecated',
|
|
|
|
`\`${oldProp}\` is deprecated. Please use \`${newProp}\` instead.${
|
|
|
|
message ? ` ${message}` : ''
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
return typeWarning;
|
|
|
|
}
|
2023-09-13 22:07:33 +08:00
|
|
|
: () => {
|
|
|
|
const noopWarning: TypeWarning = () => {};
|
|
|
|
|
|
|
|
noopWarning.deprecated = noop;
|
|
|
|
|
|
|
|
return noopWarning;
|
|
|
|
};
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2022-05-10 15:43:29 +08:00
|
|
|
export default warning;
|