mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-13 15:49:10 +08:00
d689c7b855
* feat: remove locale-provider * chore: fix lint * Empty-Commit * test: remove test * Revert "test: remove test" This reverts commit 4f6b59883f6bf078f21a10898ef913c9e2adb913. * chore: 阶段性兼容 * fix lint * fix: fix lint * fix: add index * docs: update docs * docs: update * docs: update * docs: update
65 lines
2.2 KiB
TypeScript
65 lines
2.2 KiB
TypeScript
import * as React from 'react';
|
|
import type { Locale } from '.';
|
|
import type { LocaleContextProps } from './context';
|
|
import LocaleContext from './context';
|
|
import defaultLocaleData from '../locale/en_US';
|
|
|
|
export type LocaleComponentName = Exclude<keyof Locale, 'locale'>;
|
|
|
|
export interface LocaleReceiverProps<C extends LocaleComponentName = LocaleComponentName> {
|
|
componentName?: C;
|
|
defaultLocale?: Locale[C] | (() => Locale[C]);
|
|
children: (
|
|
locale: NonNullable<Locale[C]>,
|
|
localeCode: string,
|
|
fullLocale: Locale,
|
|
) => React.ReactElement;
|
|
}
|
|
|
|
const LocaleReceiver = <C extends LocaleComponentName = LocaleComponentName>(
|
|
props: LocaleReceiverProps<C>,
|
|
) => {
|
|
const { componentName = 'global' as C, defaultLocale, children } = props;
|
|
const antLocale = React.useContext<LocaleContextProps | undefined>(LocaleContext);
|
|
|
|
const getLocale = React.useMemo<NonNullable<Locale[C]>>(() => {
|
|
const locale = defaultLocale || defaultLocaleData[componentName];
|
|
const localeFromContext = antLocale?.[componentName] ?? {};
|
|
return {
|
|
...(locale instanceof Function ? locale() : locale),
|
|
...(localeFromContext || {}),
|
|
};
|
|
}, [componentName, defaultLocale, antLocale]);
|
|
|
|
const getLocaleCode = React.useMemo<string>(() => {
|
|
const localeCode = antLocale && antLocale.locale;
|
|
// Had use LocaleProvide but didn't set locale
|
|
if (antLocale && antLocale.exist && !localeCode) {
|
|
return defaultLocaleData.locale;
|
|
}
|
|
return localeCode!;
|
|
}, [antLocale]);
|
|
|
|
return children(getLocale, getLocaleCode, antLocale!);
|
|
};
|
|
|
|
export default LocaleReceiver;
|
|
|
|
export const useLocaleReceiver = <C extends LocaleComponentName = LocaleComponentName>(
|
|
componentName: C,
|
|
defaultLocale?: Locale[C] | (() => Locale[C]),
|
|
): [Locale[C]] => {
|
|
const antLocale = React.useContext<LocaleContextProps | undefined>(LocaleContext);
|
|
|
|
const getLocale = React.useMemo<NonNullable<Locale[C]>>(() => {
|
|
const locale = defaultLocale || defaultLocaleData[componentName];
|
|
const localeFromContext = antLocale?.[componentName] ?? {};
|
|
return {
|
|
...(typeof locale === 'function' ? locale() : locale),
|
|
...(localeFromContext || {}),
|
|
};
|
|
}, [componentName, defaultLocale, antLocale]);
|
|
|
|
return [getLocale];
|
|
};
|