2020-09-29 22:06:01 +08:00
|
|
|
import { reactive, provide, VNodeTypes, PropType, defineComponent, App } from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-09-29 22:06:01 +08:00
|
|
|
import { getComponentFromSetup } from '../_util/props-util';
|
|
|
|
import defaultRenderEmpty, { RenderEmptyHandler } from './renderEmpty';
|
|
|
|
import { CSPConfig } from './context';
|
|
|
|
import LocaleProvider, { Locale, ANT_MARK } from '../locale-provider';
|
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2019-01-02 20:13:25 +08:00
|
|
|
|
2020-09-29 22:06:01 +08:00
|
|
|
export type SizeType = 'small' | 'middle' | 'large' | undefined;
|
|
|
|
|
|
|
|
export const configConsumerProps = [
|
|
|
|
'getTargetContainer',
|
|
|
|
'getPopupContainer',
|
|
|
|
'rootPrefixCls',
|
|
|
|
'getPrefixCls',
|
|
|
|
'renderEmpty',
|
|
|
|
'csp',
|
|
|
|
'autoInsertSpaceInButton',
|
|
|
|
'locale',
|
|
|
|
'pageHeader',
|
|
|
|
];
|
|
|
|
|
|
|
|
export interface ConfigProviderProps {
|
|
|
|
getTargetContainer?: () => HTMLElement;
|
|
|
|
getPopupContainer?: (triggerNode: HTMLElement) => HTMLElement;
|
|
|
|
prefixCls?: string;
|
|
|
|
children?: VNodeTypes;
|
|
|
|
renderEmpty?: RenderEmptyHandler;
|
|
|
|
csp?: CSPConfig;
|
|
|
|
autoInsertSpaceInButton?: boolean;
|
|
|
|
input?: {
|
|
|
|
autoComplete?: string;
|
|
|
|
};
|
|
|
|
locale?: Locale;
|
|
|
|
pageHeader?: {
|
|
|
|
ghost: boolean;
|
|
|
|
};
|
|
|
|
componentSize?: SizeType;
|
|
|
|
direction?: 'ltr' | 'rtl';
|
|
|
|
space?: {
|
|
|
|
size?: SizeType | number;
|
|
|
|
};
|
|
|
|
virtual?: boolean;
|
|
|
|
dropdownMatchSelectWidth?: boolean;
|
2019-03-15 15:12:28 +08:00
|
|
|
}
|
|
|
|
|
2020-09-29 22:06:01 +08:00
|
|
|
const ConfigProvider = defineComponent({
|
2019-01-02 20:13:25 +08:00
|
|
|
name: 'AConfigProvider',
|
|
|
|
props: {
|
2020-09-29 22:06:01 +08:00
|
|
|
getTargetContainer: {
|
|
|
|
type: Function as PropType<() => HTMLElement>,
|
|
|
|
},
|
|
|
|
getPopupContainer: {
|
|
|
|
type: Function as PropType<(triggerNode: HTMLElement) => HTMLElement>,
|
|
|
|
},
|
2019-03-08 13:48:55 +08:00
|
|
|
prefixCls: PropTypes.string,
|
2020-09-29 22:06:01 +08:00
|
|
|
getPrefixCls: {
|
|
|
|
type: Function as PropType<(suffixCls?: string, customizePrefixCls?: string) => string>,
|
|
|
|
},
|
|
|
|
renderEmpty: {
|
|
|
|
type: Function as PropType<RenderEmptyHandler>,
|
|
|
|
},
|
|
|
|
csp: {
|
|
|
|
type: Object as PropType<CSPConfig>,
|
|
|
|
},
|
2019-03-08 13:48:55 +08:00
|
|
|
autoInsertSpaceInButton: PropTypes.bool,
|
2020-09-29 22:06:01 +08:00
|
|
|
locale: {
|
|
|
|
type: Object as PropType<Locale>,
|
|
|
|
},
|
|
|
|
pageHeader: {
|
|
|
|
type: Object as PropType<{ ghost: boolean }>,
|
2019-03-14 21:21:58 +08:00
|
|
|
},
|
2020-09-29 22:06:01 +08:00
|
|
|
componentSize: {
|
|
|
|
type: Object as PropType<SizeType>,
|
|
|
|
},
|
|
|
|
direction: {
|
|
|
|
type: String as PropType<'ltr' | 'rtl'>,
|
|
|
|
},
|
|
|
|
space: {
|
|
|
|
type: [String, Number] as PropType<SizeType | number>,
|
|
|
|
},
|
|
|
|
virtual: PropTypes.bool,
|
|
|
|
dropdownMatchSelectWidth: PropTypes.bool,
|
|
|
|
},
|
|
|
|
setup(props, { slots }) {
|
|
|
|
const getPrefixCls = (suffixCls?: string, customizePrefixCls?: string) => {
|
|
|
|
const { prefixCls = 'ant' } = props;
|
2019-03-08 13:48:55 +08:00
|
|
|
if (customizePrefixCls) return customizePrefixCls;
|
|
|
|
return suffixCls ? `${prefixCls}-${suffixCls}` : prefixCls;
|
2020-09-29 22:06:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const renderEmptyComponent = (name?: string) => {
|
|
|
|
const renderEmpty = (getComponentFromSetup(props, slots, 'renderEmpty') ||
|
|
|
|
defaultRenderEmpty) as RenderEmptyHandler;
|
|
|
|
return renderEmpty(name);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getPrefixClsWrapper = (suffixCls: string, customizePrefixCls?: string) => {
|
|
|
|
const { prefixCls } = props;
|
|
|
|
|
|
|
|
if (customizePrefixCls) return customizePrefixCls;
|
|
|
|
|
|
|
|
const mergedPrefixCls = prefixCls || getPrefixCls('');
|
|
|
|
|
|
|
|
return suffixCls ? `${mergedPrefixCls}-${suffixCls}` : mergedPrefixCls;
|
|
|
|
};
|
|
|
|
|
|
|
|
const configProvider = reactive({
|
|
|
|
...props,
|
|
|
|
getPrefixCls: getPrefixClsWrapper,
|
|
|
|
renderEmpty: renderEmptyComponent,
|
|
|
|
});
|
|
|
|
|
|
|
|
provide('configProvider', configProvider);
|
|
|
|
|
|
|
|
const renderProvider = (legacyLocale: Locale) => {
|
2020-03-07 19:45:13 +08:00
|
|
|
return (
|
2020-09-29 22:06:01 +08:00
|
|
|
<LocaleProvider locale={props.locale || legacyLocale} _ANT_MARK__={ANT_MARK}>
|
|
|
|
{slots.default?.()}
|
2020-03-07 19:45:13 +08:00
|
|
|
</LocaleProvider>
|
|
|
|
);
|
2020-09-29 22:06:01 +08:00
|
|
|
};
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2020-09-29 22:06:01 +08:00
|
|
|
return () => (
|
|
|
|
<LocaleReceiver children={(_, __, legacyLocale) => renderProvider(legacyLocale as Locale)} />
|
|
|
|
);
|
2019-03-08 13:48:55 +08:00
|
|
|
},
|
2020-09-29 22:06:01 +08:00
|
|
|
});
|
2019-03-08 13:48:55 +08:00
|
|
|
|
|
|
|
export const ConfigConsumerProps = {
|
2020-08-23 16:45:55 +08:00
|
|
|
getPrefixCls: (suffixCls: string, customizePrefixCls?: string) => {
|
2019-03-08 13:48:55 +08:00
|
|
|
if (customizePrefixCls) return customizePrefixCls;
|
|
|
|
return `ant-${suffixCls}`;
|
2019-01-02 20:13:25 +08:00
|
|
|
},
|
2019-03-14 21:21:58 +08:00
|
|
|
renderEmpty: defaultRenderEmpty,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2019-01-02 20:13:25 +08:00
|
|
|
|
|
|
|
/* istanbul ignore next */
|
2020-09-29 22:06:01 +08:00
|
|
|
ConfigProvider.install = function(app: App) {
|
2020-06-02 23:21:24 +08:00
|
|
|
app.component(ConfigProvider.name, ConfigProvider);
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2019-01-02 20:13:25 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
export default ConfigProvider;
|