2021-06-07 17:35:03 +08:00
|
|
|
import {
|
|
|
|
reactive,
|
|
|
|
provide,
|
|
|
|
PropType,
|
|
|
|
defineComponent,
|
|
|
|
watch,
|
|
|
|
ExtractPropTypes,
|
|
|
|
UnwrapRef,
|
|
|
|
} 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 defaultRenderEmpty, { RenderEmptyHandler } from './renderEmpty';
|
|
|
|
import LocaleProvider, { Locale, ANT_MARK } from '../locale-provider';
|
2020-11-19 22:02:00 +08:00
|
|
|
import { TransformCellTextProps } from '../table/interface';
|
2020-03-07 19:45:13 +08:00
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2020-11-01 15:03:33 +08:00
|
|
|
import { withInstall } from '../_util/type';
|
2021-06-07 17:35:03 +08:00
|
|
|
import { RequiredMark } from '../form/Form';
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-20 17:03:58 +08:00
|
|
|
export { RenderEmptyHandler };
|
|
|
|
|
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;
|
|
|
|
dropdownMatchSelectWidth?: boolean;
|
|
|
|
}
|
|
|
|
|
2020-09-29 22:06:01 +08:00
|
|
|
export const configConsumerProps = [
|
|
|
|
'getTargetContainer',
|
|
|
|
'getPopupContainer',
|
|
|
|
'rootPrefixCls',
|
|
|
|
'getPrefixCls',
|
|
|
|
'renderEmpty',
|
|
|
|
'csp',
|
|
|
|
'autoInsertSpaceInButton',
|
|
|
|
'locale',
|
|
|
|
'pageHeader',
|
|
|
|
];
|
|
|
|
|
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,
|
|
|
|
dropdownMatchSelectWidth: PropTypes.looseBool,
|
|
|
|
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-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
|
|
|
|
2020-11-01 15:03:33 +08:00
|
|
|
export default withInstall(ConfigProvider);
|