ant-design/components/config-provider/index.tsx

107 lines
3.2 KiB
TypeScript
Raw Normal View History

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 };
export interface CSPConfig {
nonce?: string;
}
export interface ConfigConsumerProps {
2018-11-26 12:06:42 +08:00
getPopupContainer?: (triggerNode?: HTMLElement) => HTMLElement;
rootPrefixCls?: string;
getPrefixCls: (suffixCls: string, customizePrefixCls?: string) => string;
2018-12-26 16:01:00 +08:00
renderEmpty: RenderEmptyHandler;
csp?: CSPConfig;
2018-11-26 12:06:42 +08:00
}
interface ConfigProviderProps {
getPopupContainer?: (triggerNode?: HTMLElement) => HTMLElement;
prefixCls?: string;
children?: React.ReactNode;
2018-12-26 16:01:00 +08:00
renderEmpty?: RenderEmptyHandler;
csp?: CSPConfig;
}
const ConfigContext: Context<ConfigConsumerProps | null> = createReactContext({
2018-12-26 16:01:00 +08:00
// We provide a default function for Context without provider
getPrefixCls: (suffixCls: string, customizePrefixCls?: string) => {
if (customizePrefixCls) return customizePrefixCls;
return `ant-${suffixCls}`;
},
2018-12-26 16:01:00 +08:00
renderEmpty: defaultRenderEmpty,
});
export const ConfigConsumer = ConfigContext.Consumer;
2018-11-26 12:06:42 +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
};
renderProvider = (context: ConfigConsumerProps) => {
const { children, getPopupContainer, renderEmpty, csp } = this.props;
const config: ConfigConsumerProps = {
...context,
getPrefixCls: this.getPrefixCls,
csp,
};
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>;
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderProvider}</ConfigConsumer>;
}
}
2018-11-26 12:06:42 +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 <ComponentDef>(Component: IReactComponent): React.SFC<ExportProps> & ComponentDef {
// 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>
)) as React.SFC<ExportProps> & ComponentDef;
};
}
2018-12-07 20:02:01 +08:00
export default ConfigProvider;