refactoring: rewrite useLocale return value (#40884)

* refactoring: rewrite useLocale return

* rename

* revert name
This commit is contained in:
lijianan 2023-02-24 10:51:59 +08:00 committed by GitHub
parent 433a73c53b
commit 412fe13d0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 33 additions and 21 deletions

View File

@ -236,7 +236,7 @@ function generateCalendar<DateType>(generateConfig: GenerateConfig<DateType>) {
[monthFullCellRender, monthCellRender], [monthFullCellRender, monthCellRender],
); );
const contextLocale = useLocale('Calendar', getDefaultLocale); const [contextLocale] = useLocale('Calendar', getDefaultLocale);
return wrapSSR( return wrapSSR(
<div <div

View File

@ -102,7 +102,7 @@ export default function generateRangePicker<DateType>(generateConfig: GenerateCo
blur: () => innerRef.current?.blur(), blur: () => innerRef.current?.blur(),
})); }));
const contextLocale = useLocale('Calendar', enUS); const [contextLocale] = useLocale('Calendar', enUS);
const locale = { ...contextLocale, ...props.locale! }; const locale = { ...contextLocale, ...props.locale! };

View File

@ -122,7 +122,7 @@ export default function generatePicker<DateType>(generateConfig: GenerateConfig<
</> </>
); );
const contextLocale = useLocale('DatePicker', enUS); const [contextLocale] = useLocale('DatePicker', enUS);
const locale = { ...contextLocale, ...props.locale! }; const locale = { ...contextLocale, ...props.locale! };

View File

@ -46,7 +46,7 @@ const Empty: CompoundedComponent = ({
const prefixCls = getPrefixCls('empty', customizePrefixCls); const prefixCls = getPrefixCls('empty', customizePrefixCls);
const [wrapSSR, hashId] = useStyle(prefixCls); const [wrapSSR, hashId] = useStyle(prefixCls);
const locale = useLocale('Empty'); const [locale] = useLocale('Empty');
const des = typeof description !== 'undefined' ? description : locale?.description; const des = typeof description !== 'undefined' ? description : locale?.description;
const alt = typeof des === 'string' ? des : 'empty'; const alt = typeof des === 'string' ? des : 'empty';

View File

@ -53,7 +53,7 @@ const FormItemLabel: React.FC<FormItemLabelProps & { required?: boolean; prefixC
requiredMark, requiredMark,
tooltip, tooltip,
}) => { }) => {
const formLocale = useLocale('Form'); const [formLocale] = useLocale('Form');
const { const {
vertical, vertical,

View File

@ -9,16 +9,28 @@ export type LocaleComponentName = Exclude<keyof Locale, 'locale'>;
const useLocale = <C extends LocaleComponentName = LocaleComponentName>( const useLocale = <C extends LocaleComponentName = LocaleComponentName>(
componentName: C, componentName: C,
defaultLocale?: Locale[C] | (() => Locale[C]), defaultLocale?: Locale[C] | (() => Locale[C]),
): Locale[C] => { ): readonly [NonNullable<Locale[C]>, string] => {
const antLocale = React.useContext<LocaleContextProps | undefined>(LocaleContext); const fullLocale = React.useContext<LocaleContextProps | undefined>(LocaleContext);
return React.useMemo<NonNullable<Locale[C]>>(() => {
const getLocale = React.useMemo<NonNullable<Locale[C]>>(() => {
const locale = defaultLocale || defaultLocaleData[componentName]; const locale = defaultLocale || defaultLocaleData[componentName];
const localeFromContext = antLocale?.[componentName] ?? {}; const localeFromContext = fullLocale?.[componentName] ?? {};
return { return {
...(typeof locale === 'function' ? locale() : locale), ...(typeof locale === 'function' ? locale() : locale),
...(localeFromContext || {}), ...(localeFromContext || {}),
}; };
}, [componentName, defaultLocale, antLocale]); }, [componentName, defaultLocale, fullLocale]);
const getLocaleCode = React.useMemo<string>(() => {
const localeCode = fullLocale?.locale;
// Had use LocaleProvide but didn't set locale
if (fullLocale?.exist && !localeCode) {
return defaultLocaleData.locale;
}
return localeCode!;
}, [fullLocale]);
return [getLocale, getLocaleCode] as const;
}; };
export default useLocale; export default useLocale;

View File

@ -81,7 +81,7 @@ export function ConfirmContent(
const autoFocusButton = props.autoFocusButton === null ? false : props.autoFocusButton || 'ok'; const autoFocusButton = props.autoFocusButton === null ? false : props.autoFocusButton || 'ok';
const locale = useLocale('Modal'); const [locale] = useLocale('Modal');
const mergedLocale = staticLocale || locale; const mergedLocale = staticLocale || locale;

View File

@ -58,7 +58,7 @@ export const Footer: React.FC<
footer, footer,
} = props; } = props;
const locale = useLocale('Modal', getConfirmLocale()); const [locale] = useLocale('Modal', getConfirmLocale());
return footer === undefined ? ( return footer === undefined ? (
<> <>

View File

@ -46,7 +46,7 @@ const HookModal: React.ForwardRefRenderFunction<HookModalRef, HookModalProps> =
const mergedOkCancel = innerConfig.okCancel ?? innerConfig.type === 'confirm'; const mergedOkCancel = innerConfig.okCancel ?? innerConfig.type === 'confirm';
const contextLocale = useLocale('Modal', defaultLocale.Modal); const [contextLocale] = useLocale('Modal', defaultLocale.Modal);
return ( return (
<ConfirmDialog <ConfirmDialog

View File

@ -93,7 +93,7 @@ const Pagination: React.FC<PaginationProps> = ({
return { prevIcon, nextIcon, jumpPrevIcon, jumpNextIcon }; return { prevIcon, nextIcon, jumpPrevIcon, jumpNextIcon };
}; };
const contextLocale = useLocale('Pagination', enUS); const [contextLocale] = useLocale('Pagination', enUS);
const locale = { ...contextLocale, ...customLocale }; const locale = { ...contextLocale, ...customLocale };

View File

@ -56,7 +56,7 @@ export const Overlay: React.FC<OverlayProps> = (props) => {
const { getPrefixCls } = React.useContext(ConfigContext); const { getPrefixCls } = React.useContext(ConfigContext);
const contextLocale = useLocale('Popconfirm', defaultLocale.Popconfirm); const [contextLocale] = useLocale('Popconfirm', defaultLocale.Popconfirm);
return ( return (
<div className={`${prefixCls}-inner-content`}> <div className={`${prefixCls}-inner-content`}>

View File

@ -53,7 +53,7 @@ const QRCode: React.FC<QRCodeProps> = (props) => {
}; };
}, [errorLevel, color, icon, iconSize, size, value]); }, [errorLevel, color, icon, iconSize, size, value]);
const locale = useLocale('QRCode'); const [locale] = useLocale('QRCode');
if (!value) { if (!value) {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {

View File

@ -92,7 +92,7 @@ const TourPanel: React.FC<TourPanelProps> = ({ stepProps, current, type, indicat
ghost: mergedType === 'primary', ghost: mergedType === 'primary',
}; };
const contextLocale = useLocale('Tour', defaultLocale.Tour); const [contextLocale] = useLocale('Tour', defaultLocale.Tour);
return ( return (
<div <div

View File

@ -43,7 +43,7 @@ const ListItem = <RecordType extends KeyWiseTransferItem>(props: ListItemProps<R
title = String(renderedText); title = String(renderedText);
} }
const contextLocale = useLocale('Transfer', defaultLocale.Transfer); const [contextLocale] = useLocale('Transfer', defaultLocale.Transfer);
const liProps: React.HTMLAttributes<HTMLLIElement> = { className, title }; const liProps: React.HTMLAttributes<HTMLLIElement> = { className, title };

View File

@ -367,7 +367,7 @@ const Transfer = <RecordType extends TransferItem = TransferItem>(
hashId, hashId,
); );
const contextLocale = useLocale('Transfer', defaultLocale.Transfer); const [contextLocale] = useLocale('Transfer', defaultLocale.Transfer);
const listLocale = getLocale(contextLocale!); const listLocale = getLocale(contextLocale!);

View File

@ -135,7 +135,7 @@ const Base = React.forwardRef<HTMLElement, BlockProps>((props, ref) => {
...restProps ...restProps
} = props; } = props;
const { getPrefixCls, direction } = React.useContext(ConfigContext); const { getPrefixCls, direction } = React.useContext(ConfigContext);
const textLocale = useLocale('Text'); const [textLocale] = useLocale('Text');
const typographyRef = React.useRef<HTMLElement>(null); const typographyRef = React.useRef<HTMLElement>(null);
const editIconRef = React.useRef<HTMLDivElement>(null); const editIconRef = React.useRef<HTMLDivElement>(null);

View File

@ -339,7 +339,7 @@ const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (pr
const [wrapSSR, hashId] = useStyle(prefixCls); const [wrapSSR, hashId] = useStyle(prefixCls);
const contextLocale = useLocale('Upload', defaultLocale.Upload); const [contextLocale] = useLocale('Upload', defaultLocale.Upload);
const { const {
showRemoveIcon, showRemoveIcon,