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
|
|
|
|
2019-01-03 17:10:04 +08:00
|
|
|
// =========================== withConfigConsumer ===========================
|
|
|
|
// We need define many types here. So let's put in the block region
|
|
|
|
type IReactComponent<P = any> =
|
|
|
|
| React.StatelessComponent<P>
|
|
|
|
| React.ComponentClass<P>
|
|
|
|
| React.ClassicComponentClass<P>;
|
|
|
|
|
|
|
|
interface BasicExportProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ConsumerConfig {
|
|
|
|
prefixCls: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function withConfigConsumer<ExportProps extends BasicExportProps>(config: ConsumerConfig) {
|
|
|
|
return function(Component: IReactComponent): React.SFC<ExportProps> {
|
|
|
|
// Wrap with ConfigConsumer. Since we need compatible with react 15, be care when using ref methods
|
|
|
|
return (props: ExportProps) => (
|
|
|
|
<ConfigConsumer>
|
|
|
|
{(configProps: ConfigConsumerProps) => {
|
|
|
|
const { prefixCls: basicPrefixCls } = config;
|
|
|
|
const { getPrefixCls } = configProps;
|
|
|
|
const { prefixCls: customizePrefixCls } = props;
|
|
|
|
const prefixCls = getPrefixCls(basicPrefixCls, customizePrefixCls);
|
|
|
|
return <Component {...configProps} {...props} prefixCls={prefixCls} />;
|
|
|
|
}}
|
|
|
|
</ConfigConsumer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
export default ConfigProvider;
|