2021-09-25 16:51:32 +08:00
|
|
|
import type { PropType, ExtractPropTypes, UnwrapRef, App, Plugin, WatchStopHandle } from 'vue';
|
|
|
|
import { reactive, provide, defineComponent, watch, ref, unref, watchEffect } from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2021-09-25 16:51:32 +08:00
|
|
|
import defaultRenderEmpty from './renderEmpty';
|
|
|
|
import type { RenderEmptyHandler } from './renderEmpty';
|
2021-06-26 09:35:40 +08:00
|
|
|
import type { Locale } from '../locale-provider';
|
|
|
|
import LocaleProvider, { ANT_MARK } from '../locale-provider';
|
|
|
|
import type { TransformCellTextProps } from '../table/interface';
|
2020-03-07 19:45:13 +08:00
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2021-06-26 09:35:40 +08:00
|
|
|
import type { RequiredMark } from '../form/Form';
|
2021-09-25 16:51:32 +08:00
|
|
|
import type { MaybeRef } from '../_util/type';
|
2019-01-02 20:13:25 +08:00
|
|
|
|
2020-09-29 22:06:01 +08:00
|
|
|
export type SizeType = 'small' | 'middle' | 'large' | undefined;
|
|
|
|
|
2020-09-30 10:47:18 +08:00
|
|
|
export interface CSPConfig {
|
|
|
|
nonce?: string;
|
|
|
|
}
|
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
export type { RenderEmptyHandler };
|
2020-10-20 17:03:58 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
export type Direction = 'ltr' | 'rtl';
|
|
|
|
|
2020-09-30 10:47:18 +08:00
|
|
|
export interface ConfigConsumerProps {
|
|
|
|
getTargetContainer?: () => HTMLElement;
|
|
|
|
getPopupContainer?: (triggerNode: HTMLElement) => HTMLElement;
|
|
|
|
rootPrefixCls?: string;
|
|
|
|
getPrefixCls: (suffixCls?: string, customizePrefixCls?: string) => string;
|
|
|
|
renderEmpty: RenderEmptyHandler;
|
2020-11-19 22:02:00 +08:00
|
|
|
transformCellText?: (tableProps: TransformCellTextProps) => any;
|
2020-09-30 10:47:18 +08:00
|
|
|
csp?: CSPConfig;
|
|
|
|
autoInsertSpaceInButton?: boolean;
|
|
|
|
input?: {
|
|
|
|
autoComplete?: string;
|
|
|
|
};
|
|
|
|
locale?: Locale;
|
|
|
|
pageHeader?: {
|
|
|
|
ghost: boolean;
|
|
|
|
};
|
2021-06-07 17:35:03 +08:00
|
|
|
componentSize?: SizeType;
|
2020-09-30 10:47:18 +08:00
|
|
|
direction?: 'ltr' | 'rtl';
|
|
|
|
space?: {
|
|
|
|
size?: SizeType | number;
|
|
|
|
};
|
|
|
|
virtual?: boolean;
|
2021-09-25 16:51:32 +08:00
|
|
|
dropdownMatchSelectWidth?: boolean | number;
|
2020-09-30 10:47:18 +08:00
|
|
|
}
|
|
|
|
|
2020-09-29 22:06:01 +08:00
|
|
|
export const configConsumerProps = [
|
|
|
|
'getTargetContainer',
|
|
|
|
'getPopupContainer',
|
|
|
|
'rootPrefixCls',
|
|
|
|
'getPrefixCls',
|
|
|
|
'renderEmpty',
|
|
|
|
'csp',
|
|
|
|
'autoInsertSpaceInButton',
|
|
|
|
'locale',
|
|
|
|
'pageHeader',
|
|
|
|
];
|
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
export const defaultPrefixCls = 'ant';
|
|
|
|
const globalPrefixCls = ref<string>();
|
|
|
|
|
|
|
|
type GlobalConfigProviderProps = {
|
|
|
|
prefixCls?: MaybeRef<ConfigProviderProps['prefixCls']>;
|
|
|
|
};
|
|
|
|
|
|
|
|
let stopWatchEffect: WatchStopHandle;
|
|
|
|
const setGlobalConfig = (params: GlobalConfigProviderProps) => {
|
|
|
|
if (stopWatchEffect) {
|
|
|
|
stopWatchEffect();
|
|
|
|
}
|
|
|
|
stopWatchEffect = watchEffect(() => {
|
|
|
|
const prefixCls = unref(params.prefixCls);
|
|
|
|
if (prefixCls !== undefined) {
|
|
|
|
globalPrefixCls.value = prefixCls;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function getGlobalPrefixCls() {
|
|
|
|
return globalPrefixCls.value || defaultPrefixCls;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const globalConfig = () => ({
|
|
|
|
getPrefixCls: (suffixCls?: string, customizePrefixCls?: string) => {
|
|
|
|
if (customizePrefixCls) return customizePrefixCls;
|
|
|
|
return suffixCls ? `${getGlobalPrefixCls()}-${suffixCls}` : getGlobalPrefixCls();
|
|
|
|
},
|
|
|
|
getRootPrefixCls: (rootPrefixCls?: string, customizePrefixCls?: string) => {
|
|
|
|
// Customize rootPrefixCls is first priority
|
|
|
|
if (rootPrefixCls) {
|
|
|
|
return rootPrefixCls;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If Global prefixCls provided, use this
|
|
|
|
if (globalPrefixCls.value) {
|
|
|
|
return globalPrefixCls.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// [Legacy] If customize prefixCls provided, we cut it to get the prefixCls
|
|
|
|
if (customizePrefixCls && customizePrefixCls.includes('-')) {
|
|
|
|
return customizePrefixCls.replace(/^(.*)-[^-]*$/, '$1');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to default prefixCls
|
|
|
|
return getGlobalPrefixCls();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
export const configProviderProps = {
|
|
|
|
getTargetContainer: {
|
|
|
|
type: Function as PropType<() => HTMLElement>,
|
|
|
|
},
|
|
|
|
getPopupContainer: {
|
|
|
|
type: Function as PropType<(triggerNode: HTMLElement) => HTMLElement>,
|
|
|
|
},
|
|
|
|
prefixCls: String,
|
|
|
|
getPrefixCls: {
|
|
|
|
type: Function as PropType<(suffixCls?: string, customizePrefixCls?: string) => string>,
|
|
|
|
},
|
|
|
|
renderEmpty: {
|
|
|
|
type: Function as PropType<RenderEmptyHandler>,
|
|
|
|
},
|
|
|
|
transformCellText: {
|
|
|
|
type: Function as PropType<(tableProps: TransformCellTextProps) => any>,
|
|
|
|
},
|
|
|
|
csp: {
|
|
|
|
type: Object as PropType<CSPConfig>,
|
|
|
|
},
|
|
|
|
autoInsertSpaceInButton: PropTypes.looseBool,
|
|
|
|
locale: {
|
|
|
|
type: Object as PropType<Locale>,
|
|
|
|
},
|
|
|
|
pageHeader: {
|
|
|
|
type: Object as PropType<{ ghost: boolean }>,
|
|
|
|
},
|
|
|
|
componentSize: {
|
|
|
|
type: String as PropType<SizeType>,
|
|
|
|
},
|
|
|
|
direction: {
|
|
|
|
type: String as PropType<'ltr' | 'rtl'>,
|
|
|
|
},
|
|
|
|
space: {
|
|
|
|
type: Object as PropType<{ size: SizeType | number }>,
|
|
|
|
},
|
|
|
|
virtual: PropTypes.looseBool,
|
2021-09-25 16:51:32 +08:00
|
|
|
dropdownMatchSelectWidth: { type: [Number, Boolean], default: true },
|
2021-06-07 17:35:03 +08:00
|
|
|
form: {
|
|
|
|
type: Object as PropType<{ requiredMark?: RequiredMark }>,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export type ConfigProviderProps = Partial<ExtractPropTypes<typeof configProviderProps>>;
|
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',
|
2021-09-25 16:51:32 +08:00
|
|
|
inheritAttrs: false,
|
2021-06-07 17:35:03 +08:00
|
|
|
props: configProviderProps,
|
2020-09-29 22:06:01 +08:00
|
|
|
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) => {
|
2020-10-12 15:39:11 +08:00
|
|
|
const renderEmpty = (props.renderEmpty ||
|
|
|
|
slots.renderEmpty ||
|
2020-09-29 22:06:01 +08:00
|
|
|
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,
|
|
|
|
});
|
2021-06-18 16:41:46 +08:00
|
|
|
Object.keys(props).forEach(key => {
|
|
|
|
watch(
|
|
|
|
() => props[key],
|
|
|
|
() => {
|
|
|
|
configProvider[key] = props[key];
|
|
|
|
},
|
|
|
|
);
|
2020-10-24 20:34:00 +08:00
|
|
|
});
|
|
|
|
|
2020-09-29 22:06:01 +08:00
|
|
|
provide('configProvider', configProvider);
|
|
|
|
|
|
|
|
const renderProvider = (legacyLocale: Locale) => {
|
2020-03-07 19:45:13 +08:00
|
|
|
return (
|
2020-10-13 18:04:02 +08:00
|
|
|
<LocaleProvider locale={props.locale || legacyLocale} ANT_MARK__={ANT_MARK}>
|
2020-09-29 22:06:01 +08:00
|
|
|
{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
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
export const defaultConfigProvider: UnwrapRef<ConfigProviderProps> = reactive({
|
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;
|
2021-06-07 17:35:03 +08:00
|
|
|
return suffixCls ? `ant-${suffixCls}` : 'ant';
|
2019-01-02 20:13:25 +08:00
|
|
|
},
|
2019-03-14 21:21:58 +08:00
|
|
|
renderEmpty: defaultRenderEmpty,
|
2021-06-07 17:35:03 +08:00
|
|
|
direction: 'ltr',
|
|
|
|
});
|
2019-01-02 20:13:25 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
ConfigProvider.config = setGlobalConfig;
|
|
|
|
ConfigProvider.install = function (app: App) {
|
|
|
|
app.component(ConfigProvider.name, ConfigProvider);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ConfigProvider as typeof ConfigProvider &
|
|
|
|
Plugin & {
|
|
|
|
readonly config: typeof setGlobalConfig;
|
|
|
|
};
|