2018-11-26 12:06:42 +08:00
|
|
|
import * as React from 'react';
|
2019-07-03 20:14:39 +08:00
|
|
|
import { FormProvider as RcFormProvider } from 'rc-field-form';
|
|
|
|
import { ValidateMessages } from 'rc-field-form/lib/interface';
|
2021-01-13 17:05:05 +08:00
|
|
|
import useMemo from 'rc-util/lib/hooks/useMemo';
|
2019-10-08 14:45:17 +08:00
|
|
|
import { RenderEmptyHandler } from './renderEmpty';
|
2019-07-24 10:34:55 +08:00
|
|
|
import LocaleProvider, { Locale, ANT_MARK } from '../locale-provider';
|
2019-08-06 00:27:47 +08:00
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2020-11-03 16:22:18 +08:00
|
|
|
import {
|
|
|
|
ConfigConsumer,
|
|
|
|
ConfigContext,
|
|
|
|
CSPConfig,
|
|
|
|
DirectionType,
|
|
|
|
ConfigConsumerProps,
|
|
|
|
} from './context';
|
2020-01-03 13:38:16 +08:00
|
|
|
import { SizeType, SizeContextProvider } from './SizeContext';
|
2020-04-29 23:18:22 +08:00
|
|
|
import message from '../message';
|
|
|
|
import notification from '../notification';
|
2020-10-24 14:27:49 +08:00
|
|
|
import { RequiredMark } from '../form/Form';
|
2018-12-26 16:01:00 +08:00
|
|
|
|
2020-11-03 16:22:18 +08:00
|
|
|
export {
|
|
|
|
RenderEmptyHandler,
|
|
|
|
ConfigContext,
|
|
|
|
ConfigConsumer,
|
|
|
|
CSPConfig,
|
|
|
|
DirectionType,
|
|
|
|
ConfigConsumerProps,
|
|
|
|
};
|
2018-11-26 12:06:42 +08:00
|
|
|
|
2019-02-19 11:42:05 +08:00
|
|
|
export const configConsumerProps = [
|
2020-04-29 20:22:16 +08:00
|
|
|
'getTargetContainer',
|
2019-02-19 11:42:05 +08:00
|
|
|
'getPopupContainer',
|
|
|
|
'rootPrefixCls',
|
|
|
|
'getPrefixCls',
|
|
|
|
'renderEmpty',
|
|
|
|
'csp',
|
|
|
|
'autoInsertSpaceInButton',
|
2019-07-24 10:34:55 +08:00
|
|
|
'locale',
|
2019-10-15 11:46:12 +08:00
|
|
|
'pageHeader',
|
2019-02-19 11:42:05 +08:00
|
|
|
];
|
|
|
|
|
2019-03-16 09:48:21 +08:00
|
|
|
export interface ConfigProviderProps {
|
2020-04-29 20:22:16 +08:00
|
|
|
getTargetContainer?: () => HTMLElement;
|
2019-04-28 11:47:22 +08:00
|
|
|
getPopupContainer?: (triggerNode: HTMLElement) => HTMLElement;
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
children?: React.ReactNode;
|
2018-12-26 16:01:00 +08:00
|
|
|
renderEmpty?: RenderEmptyHandler;
|
2019-01-09 20:15:37 +08:00
|
|
|
csp?: CSPConfig;
|
2019-01-10 11:47:11 +08:00
|
|
|
autoInsertSpaceInButton?: boolean;
|
2019-07-03 20:14:39 +08:00
|
|
|
form?: {
|
|
|
|
validateMessages?: ValidateMessages;
|
2020-10-24 14:27:49 +08:00
|
|
|
requiredMark?: RequiredMark;
|
2019-07-03 20:14:39 +08:00
|
|
|
};
|
2020-04-22 10:38:43 +08:00
|
|
|
input?: {
|
|
|
|
autoComplete?: string;
|
|
|
|
};
|
2019-07-24 10:34:55 +08:00
|
|
|
locale?: Locale;
|
2019-10-15 11:46:12 +08:00
|
|
|
pageHeader?: {
|
|
|
|
ghost: boolean;
|
|
|
|
};
|
2020-01-03 13:38:16 +08:00
|
|
|
componentSize?: SizeType;
|
2020-11-03 16:22:18 +08:00
|
|
|
direction?: DirectionType;
|
2020-03-22 11:38:02 +08:00
|
|
|
space?: {
|
|
|
|
size?: SizeType | number;
|
|
|
|
};
|
2020-05-05 15:00:43 +08:00
|
|
|
virtual?: boolean;
|
|
|
|
dropdownMatchSelectWidth?: boolean;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
|
|
|
|
2021-01-12 10:40:15 +08:00
|
|
|
interface ProviderChildrenProps extends ConfigProviderProps {
|
|
|
|
parentContext: ConfigConsumerProps;
|
|
|
|
legacyLocale: Locale;
|
|
|
|
}
|
2021-01-09 15:22:08 +08:00
|
|
|
|
2021-01-12 10:40:15 +08:00
|
|
|
const ProviderChildren: React.FC<ProviderChildrenProps> = props => {
|
|
|
|
const {
|
|
|
|
children,
|
|
|
|
getTargetContainer,
|
|
|
|
getPopupContainer,
|
|
|
|
renderEmpty,
|
|
|
|
csp,
|
|
|
|
autoInsertSpaceInButton,
|
|
|
|
form,
|
|
|
|
input,
|
|
|
|
locale,
|
|
|
|
pageHeader,
|
|
|
|
componentSize,
|
|
|
|
direction,
|
|
|
|
space,
|
|
|
|
virtual,
|
|
|
|
dropdownMatchSelectWidth,
|
|
|
|
legacyLocale,
|
|
|
|
parentContext,
|
|
|
|
} = props;
|
2020-04-29 23:18:22 +08:00
|
|
|
|
2021-01-13 13:02:56 +08:00
|
|
|
const getPrefixCls = React.useCallback(
|
|
|
|
(suffixCls: string, customizePrefixCls?: string) => {
|
|
|
|
const { prefixCls } = props;
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2021-01-13 13:02:56 +08:00
|
|
|
if (customizePrefixCls) return customizePrefixCls;
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2021-01-13 13:02:56 +08:00
|
|
|
const mergedPrefixCls = prefixCls || parentContext.getPrefixCls('');
|
2020-04-21 11:16:33 +08:00
|
|
|
|
2021-01-13 13:02:56 +08:00
|
|
|
return suffixCls ? `${mergedPrefixCls}-${suffixCls}` : mergedPrefixCls;
|
|
|
|
},
|
|
|
|
[parentContext.getPrefixCls],
|
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2021-01-12 10:40:15 +08:00
|
|
|
const getConfig = (): ConfigConsumerProps => {
|
|
|
|
const config = {
|
|
|
|
...parentContext,
|
2019-01-09 20:15:37 +08:00
|
|
|
csp,
|
2019-01-10 11:47:11 +08:00
|
|
|
autoInsertSpaceInButton,
|
2019-11-15 14:35:25 +08:00
|
|
|
locale: locale || legacyLocale,
|
2020-01-02 19:10:16 +08:00
|
|
|
direction,
|
2020-03-22 11:38:02 +08:00
|
|
|
space,
|
2020-05-05 15:00:43 +08:00
|
|
|
virtual,
|
|
|
|
dropdownMatchSelectWidth,
|
2021-01-12 10:40:15 +08:00
|
|
|
getPrefixCls,
|
2018-12-05 19:12:18 +08:00
|
|
|
};
|
2020-04-29 20:22:16 +08:00
|
|
|
if (getTargetContainer) {
|
|
|
|
config.getTargetContainer = getTargetContainer;
|
|
|
|
}
|
|
|
|
|
2018-12-26 16:01:00 +08:00
|
|
|
if (getPopupContainer) {
|
|
|
|
config.getPopupContainer = getPopupContainer;
|
|
|
|
}
|
2019-10-15 11:46:12 +08:00
|
|
|
|
2018-12-26 16:01:00 +08:00
|
|
|
if (renderEmpty) {
|
|
|
|
config.renderEmpty = renderEmpty;
|
|
|
|
}
|
|
|
|
|
2019-10-15 11:46:12 +08:00
|
|
|
if (pageHeader) {
|
|
|
|
config.pageHeader = pageHeader;
|
|
|
|
}
|
|
|
|
|
2020-04-22 10:38:43 +08:00
|
|
|
if (input) {
|
|
|
|
config.input = input;
|
|
|
|
}
|
|
|
|
|
2020-10-24 14:27:49 +08:00
|
|
|
if (form) {
|
|
|
|
config.form = form;
|
|
|
|
}
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2021-01-12 10:40:15 +08:00
|
|
|
return config;
|
|
|
|
};
|
2020-04-18 13:18:51 +08:00
|
|
|
|
2021-01-12 21:02:53 +08:00
|
|
|
const config = getConfig();
|
|
|
|
|
2021-01-12 10:40:15 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/27617
|
2021-01-13 17:05:05 +08:00
|
|
|
const memoedConfig = useMemo(
|
2021-01-13 13:02:56 +08:00
|
|
|
() => config,
|
2021-01-13 17:05:05 +08:00
|
|
|
config,
|
|
|
|
(prevConfig: Record<string, any>, currentConfig) => {
|
|
|
|
const prevKeys = Object.keys(prevConfig);
|
|
|
|
const currentKeys = Object.keys(currentConfig);
|
|
|
|
return (
|
|
|
|
prevKeys.length !== currentKeys.length ||
|
|
|
|
prevKeys.some(key => prevConfig[key] !== currentConfig[key])
|
|
|
|
);
|
|
|
|
},
|
2021-01-12 21:02:53 +08:00
|
|
|
);
|
2021-01-12 10:40:15 +08:00
|
|
|
|
|
|
|
let childNode = children;
|
|
|
|
// Additional Form provider
|
|
|
|
let validateMessages: ValidateMessages = {};
|
|
|
|
|
|
|
|
if (locale && locale.Form && locale.Form.defaultValidateMessages) {
|
|
|
|
validateMessages = locale.Form.defaultValidateMessages;
|
|
|
|
}
|
|
|
|
if (form && form.validateMessages) {
|
|
|
|
validateMessages = { ...validateMessages, ...form.validateMessages };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(validateMessages).length > 0) {
|
|
|
|
childNode = <RcFormProvider validateMessages={validateMessages}>{children}</RcFormProvider>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const childrenWithLocale =
|
|
|
|
locale === undefined ? (
|
|
|
|
childNode
|
|
|
|
) : (
|
2021-01-12 21:02:53 +08:00
|
|
|
<LocaleProvider locale={locale} _ANT_MARK__={ANT_MARK}>
|
2021-01-12 10:40:15 +08:00
|
|
|
{childNode}
|
|
|
|
</LocaleProvider>
|
|
|
|
);
|
2020-04-18 13:18:51 +08:00
|
|
|
|
2021-01-12 10:40:15 +08:00
|
|
|
return (
|
|
|
|
<SizeContextProvider size={componentSize}>
|
|
|
|
<ConfigContext.Provider value={memoedConfig}>{childrenWithLocale}</ConfigContext.Provider>
|
|
|
|
</SizeContextProvider>
|
|
|
|
);
|
|
|
|
};
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2021-01-12 10:40:15 +08:00
|
|
|
const ConfigProvider: React.FC<ConfigProviderProps> & {
|
|
|
|
ConfigContext: typeof ConfigContext;
|
|
|
|
} = props => {
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (props.direction) {
|
|
|
|
message.config({
|
|
|
|
rtl: props.direction === 'rtl',
|
|
|
|
});
|
|
|
|
notification.config({
|
|
|
|
rtl: props.direction === 'rtl',
|
|
|
|
});
|
2021-01-09 15:22:08 +08:00
|
|
|
}
|
2021-01-12 10:40:15 +08:00
|
|
|
}, [props.direction]);
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-04-29 23:18:22 +08:00
|
|
|
return (
|
|
|
|
<LocaleReceiver>
|
|
|
|
{(_, __, legacyLocale) => (
|
|
|
|
<ConfigConsumer>
|
2021-01-12 10:40:15 +08:00
|
|
|
{context => (
|
|
|
|
<ProviderChildren
|
|
|
|
parentContext={context}
|
|
|
|
legacyLocale={legacyLocale as Locale}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
)}
|
2020-04-29 23:18:22 +08:00
|
|
|
</ConfigConsumer>
|
|
|
|
)}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
|
|
|
};
|
2018-11-26 12:06:42 +08:00
|
|
|
|
2020-09-11 21:24:26 +08:00
|
|
|
/** @private internal usage. do not use in your production */
|
|
|
|
ConfigProvider.ConfigContext = ConfigContext;
|
2018-12-07 20:02:01 +08:00
|
|
|
export default ConfigProvider;
|