mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-13 04:53:11 +08:00

* feat: add warningContext * refactor: part refactor * chore: fix compile * chore: part of it * chore: part of it * chore: part of it * chore: fix lint * chore: fix test * chore: clean uo * chore: hide warning def tmp * chore: comment test * chore: fix lint * chore: refactor select icons * chore: fix warning message * test: update test * chore: rm dead code
61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import * as React from 'react';
|
|
import rcWarning, { resetWarned } from 'rc-util/lib/warning';
|
|
|
|
export { resetWarned };
|
|
export function noop() {}
|
|
|
|
type Warning = (valid: boolean, component: string, message?: string) => void;
|
|
|
|
// 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();
|
|
}
|
|
};
|
|
}
|
|
|
|
type TypeWarning = (
|
|
valid: boolean,
|
|
component: string,
|
|
/**
|
|
* - 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;
|
|
|
|
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
|
|
*/
|
|
export const devUseWarning: () => TypeWarning =
|
|
process.env.NODE_ENV !== 'production'
|
|
? () => {
|
|
const { deprecated } = React.useContext(WarningContext);
|
|
|
|
const typeWarning: TypeWarning = (valid, component, type, message) => {
|
|
if (deprecated !== false || type !== 'deprecated') {
|
|
warning(valid, component, message);
|
|
}
|
|
};
|
|
|
|
return typeWarning;
|
|
}
|
|
: () => noop;
|
|
|
|
export default warning;
|