Skip to content

Commit

Permalink
refactor(react-ui): 使用 useLocale 代替 useContext
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxinssfd committed Feb 20, 2024
1 parent f9a0bc4 commit 37b41af
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
15 changes: 5 additions & 10 deletions packages/components/src/calendar/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import {
getClasses,
useWatch,
} from '@pkg/shared';
import type { CalendarLocale, CalendarProps } from './calendar.types';
import { getStartOfMonth, getClassNames } from '@tool-pack/basic';
import { ConfigContext } from '~/config-provider/config.context';
import { CalendarHeader } from '~/calendar/components/Header';
import { useLocale } from '~/config-provider/useLocale';
import type { CalendarProps } from './calendar.types';
import { CalendarTable } from '~/calendar/components';
import type { RequiredPart } from '@tool-pack/types';
import React, { useContext, useMemo } from 'react';
import calendarLocale from './locale/en-US';
import EnUS from './locale/en-US';
import React from 'react';

const cls = getClasses('calendar', ['date-cell'], ['prev-month', 'next-month']);
const defaultProps = {
Expand All @@ -24,7 +24,6 @@ export const Calendar: React.FC<CalendarProps> = React.forwardRef<
HTMLDivElement,
CalendarProps
>((props, ref) => {
const contextLocale = useContext(ConfigContext).locale;
const {
locale: propsLocale,
month: propsMonth,
Expand All @@ -38,11 +37,7 @@ export const Calendar: React.FC<CalendarProps> = React.forwardRef<
value,
} = props as RequiredPart<CalendarProps, keyof typeof defaultProps>;

const locale = useMemo<CalendarLocale>(
() =>
Object.assign({}, calendarLocale, contextLocale.calendar, propsLocale),
[contextLocale.calendar, propsLocale],
);
const locale = Object.assign(useLocale('calendar', EnUS), propsLocale);

const [valueRef, setValueRef] = useStateRef(value);
const [month, setMonth] = useFollowingState(
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/pop-confirm/PopConfirm.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ConfigContext } from '~/config-provider/config.context';
import { getComponentClass, useVisible } from '@pkg/shared';
import type { PopConfirmProps } from './pop-confirm.types';
import { useLocale } from '~/config-provider/useLocale';
import type { RequiredPart } from '@tool-pack/types';
import React, { useContext, useEffect } from 'react';
import { Layout, Footer, Main } from '~/layouts';
import { CircleInfoFill } from '@pkg/icons';
import React, { useEffect } from 'react';
import { Popover } from '~/popover';
import { Button } from '~/button';
import enUS from './locale/en-US';
Expand Down Expand Up @@ -33,7 +33,7 @@ export const PopConfirm: React.FC<PopConfirmProps> = (props) => {
const _confirm = confirmProps ?? {};
const _cancel = cancelProps ?? {};

const locale = useContext(ConfigContext).locale.popConfirm || enUS;
const locale = useLocale('popConfirm', enUS);
const [visible, hide, setVisible] = useVisible(outerVisible);

useEffect(() => {
Expand Down

0 comments on commit 37b41af

Please sign in to comment.