2018-11-26 12:06:42 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import createReactContext, { Context } from 'create-react-context';
|
|
|
|
|
2018-12-26 16:01:00 +08:00
|
|
|
import defaultRenderEmpty, { RenderEmptyHandler } from './renderEmpty';
|
|
|
|
|
|
|
|
export { RenderEmptyHandler };
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
export interface ConfigConsumerProps {
|
2018-11-26 12:06:42 +08:00
|
|
|
getPopupContainer?: (triggerNode?: HTMLElement) => HTMLElement;
|
2018-12-05 19:12:18 +08:00
|
|
|
rootPrefixCls?: string;
|
|
|
|
getPrefixCls: (suffixCls: string, customizePrefixCls?: string) => string;
|
2018-12-26 16:01:00 +08:00
|
|
|
renderEmpty: RenderEmptyHandler;
|
2018-11-26 12:06:42 +08:00
|
|
|
}
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
interface ConfigProviderProps {
|
|
|
|
getPopupContainer?: (triggerNode?: HTMLElement) => HTMLElement;
|
|
|
|
prefixCls?: string;
|
|
|
|
children?: React.ReactNode;
|
2018-12-26 16:01:00 +08:00
|
|
|
renderEmpty?: RenderEmptyHandler;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ConfigContext: Context<ConfigConsumerProps | null> = createReactContext({
|
2018-12-26 16:01:00 +08:00
|
|
|
// We provide a default function for Context without provider
|
2018-12-05 19:12:18 +08:00
|
|
|
getPrefixCls: (suffixCls: string, customizePrefixCls?: string) => {
|
|
|
|
if (customizePrefixCls) return customizePrefixCls;
|
|
|
|
|
|
|
|
return `ant-${suffixCls}`;
|
|
|
|
},
|
2018-12-26 16:01:00 +08:00
|
|
|
|
|
|
|
renderEmpty: defaultRenderEmpty,
|
2018-12-05 19:12:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
export const ConfigConsumer = ConfigContext.Consumer;
|
2018-11-26 12:06:42 +08:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderProvider = (context: ConfigConsumerProps) => {
|
2018-12-26 16:01:00 +08:00
|
|
|
const { children, getPopupContainer, renderEmpty } = this.props;
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
const config: ConfigConsumerProps = {
|
|
|
|
...context,
|
|
|
|
getPrefixCls: this.getPrefixCls,
|
|
|
|
};
|
2018-11-26 12:06:42 +08:00
|
|
|
|
2018-12-26 16:01:00 +08:00
|
|
|
if (getPopupContainer) {
|
|
|
|
config.getPopupContainer = getPopupContainer;
|
|
|
|
}
|
|
|
|
if (renderEmpty) {
|
|
|
|
config.renderEmpty = renderEmpty;
|
|
|
|
}
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigContext.Provider value={config}>{children}</ConfigContext.Provider>;
|
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderProvider}</ConfigConsumer>;
|
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;
|