2019-08-05 18:38:10 +08:00
|
|
|
// TODO: remove this lint
|
|
|
|
// SFC has specified a displayName, but not worked.
|
|
|
|
/* eslint-disable react/display-name */
|
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';
|
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';
|
2019-10-08 14:45:17 +08:00
|
|
|
import { ConfigConsumer, ConfigContext, CSPConfig, ConfigConsumerProps } from './context';
|
2018-12-26 16:01:00 +08:00
|
|
|
|
2019-10-08 15:04:00 +08:00
|
|
|
export { RenderEmptyHandler, ConfigContext, ConfigConsumer, CSPConfig, ConfigConsumerProps };
|
2018-11-26 12:06:42 +08:00
|
|
|
|
2019-02-19 11:42:05 +08:00
|
|
|
export const configConsumerProps = [
|
|
|
|
'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 {
|
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;
|
|
|
|
};
|
2019-07-24 10:34:55 +08:00
|
|
|
locale?: Locale;
|
2019-10-15 11:46:12 +08:00
|
|
|
pageHeader?: {
|
|
|
|
ghost: boolean;
|
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class ConfigProvider extends React.Component<ConfigProviderProps> {
|
|
|
|
getPrefixCls = (suffixCls: string, customizePrefixCls?: string) => {
|
|
|
|
const { prefixCls = 'ant' } = this.props;
|
|
|
|
|
|
|
|
if (customizePrefixCls) return customizePrefixCls;
|
|
|
|
|
|
|
|
return suffixCls ? `${prefixCls}-${suffixCls}` : prefixCls;
|
2018-11-26 12:06:42 +08:00
|
|
|
};
|
|
|
|
|
2019-08-06 00:27:47 +08:00
|
|
|
renderProvider = (context: ConfigConsumerProps, legacyLocale: Locale) => {
|
2019-07-03 20:14:39 +08:00
|
|
|
const {
|
|
|
|
children,
|
|
|
|
getPopupContainer,
|
|
|
|
renderEmpty,
|
|
|
|
csp,
|
|
|
|
autoInsertSpaceInButton,
|
|
|
|
form,
|
2019-07-24 10:34:55 +08:00
|
|
|
locale,
|
2019-10-15 11:46:12 +08:00
|
|
|
pageHeader,
|
2019-07-03 20:14:39 +08:00
|
|
|
} = this.props;
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
const config: ConfigConsumerProps = {
|
|
|
|
...context,
|
|
|
|
getPrefixCls: this.getPrefixCls,
|
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,
|
2018-12-05 19:12:18 +08:00
|
|
|
};
|
2018-11-26 12:06:42 +08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
let childNode = children;
|
|
|
|
|
|
|
|
// Additional Form provider
|
|
|
|
if (form && form.validateMessages) {
|
|
|
|
childNode = (
|
|
|
|
<RcFormProvider validateMessages={form.validateMessages}>{children}</RcFormProvider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-24 10:34:55 +08:00
|
|
|
return (
|
|
|
|
<ConfigContext.Provider value={config}>
|
2019-08-06 00:27:47 +08:00
|
|
|
<LocaleProvider locale={locale || legacyLocale} _ANT_MARK__={ANT_MARK}>
|
2019-08-06 17:46:53 +08:00
|
|
|
{childNode}
|
2019-07-24 10:34:55 +08:00
|
|
|
</LocaleProvider>
|
|
|
|
</ConfigContext.Provider>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2019-08-06 00:27:47 +08:00
|
|
|
return (
|
|
|
|
<LocaleReceiver>
|
|
|
|
{(_, __, legacyLocale) => (
|
|
|
|
<ConfigConsumer>
|
|
|
|
{context => this.renderProvider(context, legacyLocale as Locale)}
|
|
|
|
</ConfigConsumer>
|
|
|
|
)}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
|
|
|
}
|
2018-11-26 12:06:42 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
export default ConfigProvider;
|