Skip to content

Commit

Permalink
feat(components/config-provider): useLocale
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxinssfd committed Feb 19, 2024
1 parent 86ee060 commit df196c2
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/components/src/config-provider/useLocale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Locale } from './config-provider.types';
import { ConfigContext } from './config.context';
import { useContext, useMemo } from 'react';

export function useLocale(): Locale;
export function useLocale<K extends keyof Locale>(name: K): Locale[K];
export function useLocale<K extends keyof Locale>(
name: K,
presetLocale: Locale[K],
): Required<Locale[K]>;
export function useLocale(name?: keyof Locale, presetLocale?: object) {
const locale = useContext(ConfigContext).locale;
return useMemo(() => {
if (!name) return locale;

const componentLocale = locale[name];
if (!presetLocale) return componentLocale;

return { ...presetLocale, ...componentLocale };
}, [name, locale, presetLocale]);
}

0 comments on commit df196c2

Please sign in to comment.