Browse Source
* chore: rename useLocaleReceiver => useLocale * refactoring: use hook replace LocaleReceiver * fix * update snap * fix * update snap * fix * renamepull/40878/head
lijianan
2 years ago
committed by
GitHub
21 changed files with 609 additions and 690 deletions
@ -1,6 +1,6 @@ |
|||
// locale-provider 文件夹的移除需要修改 @ant-design/tools 和 antd-img-crop
|
|||
import LocaleReceiver from '../locale/LocaleReceiver'; |
|||
import useLocale from '../locale/useLocale'; |
|||
|
|||
export * from '../locale/LocaleReceiver'; |
|||
export * from '../locale/useLocale'; |
|||
|
|||
export default LocaleReceiver; |
|||
export default useLocale; |
|||
|
@ -1,64 +0,0 @@ |
|||
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'>; |
|||
|
|||
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]; |
|||
}; |
@ -0,0 +1,24 @@ |
|||
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; |
Loading…
Reference in new issue