2019-10-21 12:01:33 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import defaultRenderEmpty, { RenderEmptyHandler } from './renderEmpty';
|
|
|
|
import { Locale } from '../locale-provider';
|
2020-03-22 11:38:02 +08:00
|
|
|
import { SizeType } from './SizeContext';
|
2020-10-24 14:27:49 +08:00
|
|
|
import { RequiredMark } from '../form/Form';
|
2019-10-21 12:01:33 +08:00
|
|
|
|
|
|
|
export interface CSPConfig {
|
|
|
|
nonce?: string;
|
|
|
|
}
|
|
|
|
|
2020-11-03 16:22:18 +08:00
|
|
|
export type DirectionType = 'ltr' | 'rtl' | undefined;
|
|
|
|
|
2019-10-21 12:01:33 +08:00
|
|
|
export interface ConfigConsumerProps {
|
2020-04-29 20:22:16 +08:00
|
|
|
getTargetContainer?: () => HTMLElement;
|
2019-10-21 12:01:33 +08:00
|
|
|
getPopupContainer?: (triggerNode: HTMLElement) => HTMLElement;
|
|
|
|
rootPrefixCls?: string;
|
2021-01-19 17:33:05 +08:00
|
|
|
iconPrefixCls?: string;
|
2020-08-03 11:41:08 +08:00
|
|
|
getPrefixCls: (suffixCls?: string, customizePrefixCls?: string) => string;
|
2019-10-21 12:01:33 +08:00
|
|
|
renderEmpty: RenderEmptyHandler;
|
|
|
|
csp?: CSPConfig;
|
|
|
|
autoInsertSpaceInButton?: boolean;
|
2020-04-22 10:38:43 +08:00
|
|
|
input?: {
|
|
|
|
autoComplete?: string;
|
|
|
|
};
|
2019-10-21 12:01:33 +08:00
|
|
|
locale?: Locale;
|
|
|
|
pageHeader?: {
|
|
|
|
ghost: boolean;
|
|
|
|
};
|
2020-11-03 16:22:18 +08:00
|
|
|
direction?: DirectionType;
|
2020-03-22 11:38:02 +08:00
|
|
|
space?: {
|
|
|
|
size?: SizeType | number;
|
|
|
|
};
|
2020-05-05 15:00:43 +08:00
|
|
|
virtual?: boolean;
|
|
|
|
dropdownMatchSelectWidth?: boolean;
|
2020-10-24 14:27:49 +08:00
|
|
|
form?: {
|
|
|
|
requiredMark?: RequiredMark;
|
|
|
|
};
|
2019-10-21 12:01:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-09 15:22:08 +08:00
|
|
|
const defaultGetPrefixCls = (suffixCls?: string, customizePrefixCls?: string) => {
|
|
|
|
if (customizePrefixCls) return customizePrefixCls;
|
|
|
|
|
|
|
|
return suffixCls ? `ant-${suffixCls}` : 'ant';
|
|
|
|
};
|
|
|
|
|
2019-10-30 16:28:28 +08:00
|
|
|
export const ConfigContext = React.createContext<ConfigConsumerProps>({
|
2019-10-21 12:01:33 +08:00
|
|
|
// We provide a default function for Context without provider
|
2021-01-09 15:22:08 +08:00
|
|
|
getPrefixCls: defaultGetPrefixCls,
|
2019-10-21 12:01:33 +08:00
|
|
|
|
|
|
|
renderEmpty: defaultRenderEmpty,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const ConfigConsumer = ConfigContext.Consumer;
|
|
|
|
|
|
|
|
// =========================== withConfigConsumer ===========================
|
|
|
|
// We need define many types here. So let's put in the block region
|
|
|
|
type IReactComponent<P = any> =
|
2020-06-10 11:12:31 +08:00
|
|
|
| React.FC<P>
|
2019-10-21 12:01:33 +08:00
|
|
|
| React.ComponentClass<P>
|
|
|
|
| React.ClassicComponentClass<P>;
|
|
|
|
|
|
|
|
interface BasicExportProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ConsumerConfig {
|
|
|
|
prefixCls: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ConstructorProps {
|
|
|
|
displayName?: string;
|
|
|
|
}
|
2020-10-24 14:47:44 +08:00
|
|
|
|
|
|
|
/** @deprecated Use hooks instead. This is a legacy function */
|
2019-10-21 12:01:33 +08:00
|
|
|
export function withConfigConsumer<ExportProps extends BasicExportProps>(config: ConsumerConfig) {
|
|
|
|
return function withConfigConsumerFunc<ComponentDef>(
|
|
|
|
Component: IReactComponent,
|
2020-03-28 11:56:57 +08:00
|
|
|
): React.FC<ExportProps> & ComponentDef {
|
2019-10-21 12:01:33 +08:00
|
|
|
// Wrap with ConfigConsumer. Since we need compatible with react 15, be care when using ref methods
|
|
|
|
const SFC = ((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>
|
2020-03-28 11:56:57 +08:00
|
|
|
)) as React.FC<ExportProps> & ComponentDef;
|
2019-10-21 12:01:33 +08:00
|
|
|
|
|
|
|
const cons: ConstructorProps = Component.constructor as ConstructorProps;
|
|
|
|
const name = (cons && cons.displayName) || Component.name || 'Component';
|
|
|
|
|
|
|
|
SFC.displayName = `withConfigConsumer(${name})`;
|
|
|
|
|
|
|
|
return SFC;
|
|
|
|
};
|
|
|
|
}
|