ant-design/components/locale/useLocale.ts
lijianan 288b10bd10
refactoring: use hook replace LocaleReceiver (#40870)
* chore: rename useLocaleReceiver => useLocale

* refactoring: use hook replace LocaleReceiver

* fix

* update snap

* fix

* update snap

* fix

* rename
2023-02-22 18:18:26 +08:00

25 lines
897 B
TypeScript

import * as React from 'react';
import type { Locale } from '.';
import type { LocaleContextProps } from './context';
import LocaleContext from './context';
import defaultLocaleData from './en_US';
export type LocaleComponentName = Exclude<keyof Locale, 'locale'>;
const useLocale = <C extends LocaleComponentName = LocaleComponentName>(
componentName: C,
defaultLocale?: Locale[C] | (() => Locale[C]),
): Locale[C] => {
const antLocale = React.useContext<LocaleContextProps | undefined>(LocaleContext);
return React.useMemo<NonNullable<Locale[C]>>(() => {
const locale = defaultLocale || defaultLocaleData[componentName];
const localeFromContext = antLocale?.[componentName] ?? {};
return {
...(typeof locale === 'function' ? locale() : locale),
...(localeFromContext || {}),
};
}, [componentName, defaultLocale, antLocale]);
};
export default useLocale;