From fa6c9c15f4ff571ebe64d6f37becd0c6abcf4f86 Mon Sep 17 00:00:00 2001 From: lijianan <574980606@qq.com> Date: Sun, 25 Dec 2022 22:54:56 +0800 Subject: [PATCH] refactor: delete legacy function withConfigConsumer (#39798) * chore: delete legacy function withConfigConsumer * fix * fix --- components/config-provider/context.tsx | 40 +------------------- components/statistic/Statistic.tsx | 39 ++++++++++--------- components/statistic/index.tsx | 7 +--- components/statistic/{utils.tsx => utils.ts} | 0 4 files changed, 24 insertions(+), 62 deletions(-) rename components/statistic/{utils.tsx => utils.ts} (100%) diff --git a/components/config-provider/context.tsx b/components/config-provider/context.tsx index f881fa4f6d..465fcc37f2 100644 --- a/components/config-provider/context.tsx +++ b/components/config-provider/context.tsx @@ -81,42 +81,4 @@ export const ConfigContext = React.createContext({ iconPrefixCls: defaultIconPrefixCls, }); -export const ConfigConsumer = ConfigContext.Consumer; - -// =========================== withConfigConsumer =========================== -interface BasicExportProps { - prefixCls?: string; -} - -interface ConsumerConfig { - prefixCls: string; -} - -interface ConstructorProps { - displayName?: string; -} - -/** @deprecated Use hooks instead. This is a legacy function */ -export function withConfigConsumer(config: ConsumerConfig) { - return function withConfigConsumerFunc( - Component: React.ComponentType, - ): React.FC & ComponentDef { - // Wrap with ConfigConsumer. Since we need compatible with react 15, be careful when using ref methods - const SFC: React.FC & ComponentDef = ((props) => { - const configProps = React.useContext(ConfigContext); - const { getPrefixCls } = configProps; - const { prefixCls: basicPrefixCls } = config; - const { prefixCls: customizePrefixCls } = props; - const prefixCls = getPrefixCls(basicPrefixCls, customizePrefixCls); - return ; - }) as React.FC & ComponentDef; - - const cons: ConstructorProps = Component.constructor as ConstructorProps; - const name = (cons && cons.displayName) || Component.name || 'Component'; - - if (process.env.NODE_ENV !== 'production') { - SFC.displayName = `withConfigConsumer(${name})`; - } - return SFC; - }; -} +export const { Consumer: ConfigConsumer } = ConfigContext; diff --git a/components/statistic/Statistic.tsx b/components/statistic/Statistic.tsx index 796c50d9f2..c2ae076173 100644 --- a/components/statistic/Statistic.tsx +++ b/components/statistic/Statistic.tsx @@ -1,18 +1,12 @@ import classNames from 'classnames'; import * as React from 'react'; - import type { ConfigConsumerProps } from '../config-provider'; -import { withConfigConsumer } from '../config-provider/context'; +import { ConfigContext } from '../config-provider'; import Skeleton from '../skeleton'; -import type Countdown from './Countdown'; import StatisticNumber from './Number'; import type { FormatConfig, valueType } from './utils'; - import useStyle from './style'; - -type CompoundedComponent = { - Countdown: typeof Countdown; -}; +import Countdown from './Countdown'; export interface StatisticProps extends FormatConfig { prefixCls?: string; @@ -29,9 +23,13 @@ export interface StatisticProps extends FormatConfig { onMouseLeave?: React.MouseEventHandler; } -const Statistic: React.FC = (props) => { +type CompoundedComponent = { + Countdown: typeof Countdown; +}; + +const Statistic: React.FC & CompoundedComponent = (props) => { const { - prefixCls, + prefixCls: customizePrefixCls, className, style, valueStyle, @@ -41,22 +39,28 @@ const Statistic: React.FC = (props) => { prefix, suffix, loading = false, - direction, onMouseEnter, onMouseLeave, decimalSeparator = '.', groupSeparator = ',', } = props; - const valueNode = ( + + const { getPrefixCls, direction } = React.useContext(ConfigContext); + + const prefixCls = getPrefixCls('statistic', customizePrefixCls); + + const [wrapSSR, hashId] = useStyle(prefixCls); + + const valueNode: React.ReactNode = ( ); - // Style - const [wrapSSR, hashId] = useStyle(String(prefixCls)); + const cls = classNames( prefixCls, { @@ -65,6 +69,7 @@ const Statistic: React.FC = (props) => { className, hashId, ); + return wrapSSR(
{title &&
{title}
} @@ -79,8 +84,6 @@ const Statistic: React.FC = (props) => { ); }; -const WrapperStatistic = withConfigConsumer({ - prefixCls: 'statistic', -})(Statistic); +Statistic.Countdown = Countdown; -export default WrapperStatistic; +export default Statistic; diff --git a/components/statistic/index.tsx b/components/statistic/index.tsx index 01f1758ccd..d9dd108202 100644 --- a/components/statistic/index.tsx +++ b/components/statistic/index.tsx @@ -1,8 +1,5 @@ -import Countdown from './Countdown'; -import Statistic, { type StatisticProps } from './Statistic'; - -Statistic.Countdown = Countdown; +import Statistic from './Statistic'; +import type { StatisticProps } from './Statistic'; export type { StatisticProps }; - export default Statistic; diff --git a/components/statistic/utils.tsx b/components/statistic/utils.ts similarity index 100% rename from components/statistic/utils.tsx rename to components/statistic/utils.ts