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

68 lines
1.9 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 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;
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;
}
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) => {
2018-12-26 16:01:00 +08:00
const { children, getPopupContainer, renderEmpty } = this.props;
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>;
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderProvider}</ConfigConsumer>;
}
}
2018-11-26 12:06:42 +08:00
2018-12-07 20:02:01 +08:00
export default ConfigProvider;