2021-06-26 01:35:40 +00:00
|
|
|
import type { ComputedRef, UnwrapRef } from 'vue';
|
|
|
|
import { computed, inject, provide } from 'vue';
|
|
|
|
import type { ConfigProviderProps, SizeType } from '../../config-provider';
|
|
|
|
import { defaultConfigProvider } from '../../config-provider';
|
2021-06-07 09:35:03 +00:00
|
|
|
|
|
|
|
const sizeProvider = Symbol('SizeProvider');
|
|
|
|
|
|
|
|
const useProvideSize = <T = SizeType>(props: Record<any, any>): ComputedRef<T> => {
|
|
|
|
const configProvider = inject<UnwrapRef<ConfigProviderProps>>(
|
|
|
|
'configProvider',
|
|
|
|
defaultConfigProvider,
|
|
|
|
);
|
|
|
|
const size = computed<T>(() => props.size || configProvider.componentSize);
|
|
|
|
provide(sizeProvider, size);
|
|
|
|
return size;
|
|
|
|
};
|
|
|
|
|
|
|
|
const useInjectSize = <T = SizeType>(props?: Record<any, any>): ComputedRef<T> => {
|
|
|
|
const size: ComputedRef<T> = props
|
|
|
|
? computed(() => props.size)
|
|
|
|
: inject(
|
|
|
|
sizeProvider,
|
2021-06-23 15:08:16 +00:00
|
|
|
computed(() => 'default' as unknown as T),
|
2021-06-07 09:35:03 +00:00
|
|
|
);
|
|
|
|
return size;
|
|
|
|
};
|
|
|
|
|
|
|
|
export { useInjectSize, sizeProvider, useProvideSize };
|
|
|
|
|
|
|
|
export default useProvideSize;
|