From 34e0a3e0cf601d9d86516d179a7b5b5dbd5f1307 Mon Sep 17 00:00:00 2001 From: lijianan <574980606@qq.com> Date: Sat, 14 Oct 2023 11:57:53 +0800 Subject: [PATCH] style: simplify code --- components/affix/index.tsx | 53 ++++++++++++-------------------------- 1 file changed, 17 insertions(+), 36 deletions(-) diff --git a/components/affix/index.tsx b/components/affix/index.tsx index 1088bdbdc4..437bce423d 100644 --- a/components/affix/index.tsx +++ b/components/affix/index.tsx @@ -45,10 +45,6 @@ enum AffixStatus { Prepare, } -interface InternalAffixProps extends AffixProps { - affixPrefixCls: string; -} - interface AffixState { affixStyle?: React.CSSProperties; placeholderStyle?: React.CSSProperties; @@ -61,18 +57,23 @@ interface AffixRef { updatePosition: ReturnType; } -const InternalAffix = React.forwardRef((props, ref) => { +const Affix = React.forwardRef((props, ref) => { const { style, offsetTop, offsetBottom, - affixPrefixCls, + prefixCls, + className, rootClassName, children, target, onChange, } = props; + const { getPrefixCls, getTargetContainer } = React.useContext(ConfigContext); + + const affixPrefixCls = getPrefixCls('affix', prefixCls); + const [lastAffix, setLastAffix] = React.useState(false); const [affixStyle, setAffixStyle] = React.useState(); const [placeholderStyle, setPlaceholderStyle] = React.useState(); @@ -86,8 +87,6 @@ const InternalAffix = React.forwardRef((props, ref const fixedNodeRef = React.useRef(null); const timer = React.useRef | null>(null); - const { getTargetContainer } = React.useContext(ConfigContext); - const targetFunc = target ?? getTargetContainer ?? getDefaultTarget; const internalOffsetTop = offsetBottom === undefined && offsetTop === undefined ? 0 : offsetTop; @@ -244,10 +243,11 @@ const InternalAffix = React.forwardRef((props, ref updatePosition(); }, [target, offsetTop, offsetBottom]); - const className = classNames({ - [affixPrefixCls]: affixStyle, - [rootClassName!]: affixStyle && rootClassName, - }); + const [wrapSSR, hashId] = useStyle(affixPrefixCls); + + const rootCls = classNames(rootClassName, hashId, affixPrefixCls); + + const mergedCls = classNames({ [rootCls]: affixStyle }); let otherProps = omit(props, [ 'prefixCls', @@ -255,44 +255,25 @@ const InternalAffix = React.forwardRef((props, ref 'offsetBottom', 'target', 'onChange', - 'affixPrefixCls', 'rootClassName', ]); if (process.env.NODE_ENV === 'test') { - otherProps = omit(otherProps as typeof otherProps & { onTestUpdatePosition: any }, [ - 'onTestUpdatePosition', - ]); + otherProps = omit(otherProps, ['onTestUpdatePosition' as any]); } - return ( + return wrapSSR( -
+
{affixStyle &&