mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-19 03:54:28 +08:00
288b10bd10
* chore: rename useLocaleReceiver => useLocale * refactoring: use hook replace LocaleReceiver * fix * update snap * fix * update snap * fix * rename
25 lines
897 B
TypeScript
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;
|