-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
ConfigReceiver.tsx
49 lines (43 loc) · 1.89 KB
/
ConfigReceiver.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import React, { Context } from 'react';
import hoistNonReactStatic from 'hoist-non-react-statics';
import { ConfigContext, LocaleContext } from '../config-provider/ConfigProvider';
import defaultLocaleData from '../config-provider/locale/zh_CN';
const defaultConfigData = {
locale: defaultLocaleData,
};
type GetContextInnerType<T extends Context<any>> = T extends Context<infer R> ? R : never;
type nameType = keyof Omit<GetContextInnerType<typeof LocaleContext>, 'locale'>;
type ComponentType = new (...args: any[]) => any;
const ConfigReceiverWrapper = (name?: nameType, defaultConfig?: typeof defaultConfigData) => {
return function InnerWrapper<T extends ComponentType>(WrappedComponent: T) {
const ConfigReceiver = (props: any) => {
return (
<ConfigContext.Consumer>
{
({ locale }) => {
const globalLocale = (locale.locale) ? locale : (defaultConfig?.locale || defaultConfigData?.locale);
const componentLocale = globalLocale[name || WrappedComponent.name as nameType];
const localeCode = globalLocale.locale;
const { forwardedRef, ...rest } = props;
return (
<WrappedComponent
{...rest}
ref={forwardedRef}
locale={componentLocale}
localeCode={localeCode}
/>
);
}
}
</ConfigContext.Consumer>
);
};
const forwardRef = (props: InstanceType<T>['props'], ref: React.Ref<InstanceType<T>>) => {
return <ConfigReceiver {...props} forwardedRef={ref} />;
};
const ConfigReceiverWithRef = React.forwardRef<InstanceType<T>, InstanceType<T>['props']>(forwardRef);
hoistNonReactStatic(ConfigReceiverWithRef, WrappedComponent);
return ConfigReceiverWithRef as (T & typeof ConfigReceiverWithRef);
};
};
export default ConfigReceiverWrapper;