From 0a75cfe9e26008db1d01c66c57ef75d4843c46a7 Mon Sep 17 00:00:00 2001 From: lijianan <574980606@qq.com> Date: Sun, 5 Feb 2023 10:51:51 +0800 Subject: [PATCH] refactoring: use React.createRef replace ref function (#40538) * style: use React.createRef replace ref function * style: use React.createRef replace ref function * add useRef * fix * rename --- components/affix/index.tsx | 47 +++++++++----------- components/button/__tests__/index.test.tsx | 13 ++---- components/calendar/index.ts | 3 +- components/checkbox/__tests__/group.test.tsx | 14 ++---- components/slider/__tests__/index.test.tsx | 16 ++----- tests/utils.tsx | 2 +- 6 files changed, 34 insertions(+), 61 deletions(-) diff --git a/components/affix/index.tsx b/components/affix/index.tsx index 18ca667ed7..9461673218 100644 --- a/components/affix/index.tsx +++ b/components/affix/index.tsx @@ -1,7 +1,7 @@ import classNames from 'classnames'; import ResizeObserver from 'rc-resize-observer'; import omit from 'rc-util/lib/omit'; -import * as React from 'react'; +import React, { createRef, forwardRef, useContext } from 'react'; import type { ConfigConsumerProps } from '../config-provider'; import { ConfigContext } from '../config-provider'; import throttleByAnimationFrame from '../_util/throttleByAnimationFrame'; @@ -50,7 +50,6 @@ export interface AffixState { placeholderStyle?: React.CSSProperties; status: AffixStatus; lastAffix: boolean; - prevTarget: Window | HTMLElement | null; } @@ -63,11 +62,11 @@ class Affix extends React.Component { prevTarget: null, }; - placeholderNode: HTMLDivElement; + private placeholderNodeRef = createRef(); - fixedNode: HTMLDivElement; + private fixedNodeRef = createRef(); - private timeout: NodeJS.Timeout | null; + private timer: NodeJS.Timeout | null; context: ConfigConsumerProps; @@ -88,7 +87,7 @@ class Affix extends React.Component { if (targetFunc) { // [Legacy] Wait for parent component ref has its value. // We should use target as directly element instead of function which makes element check hard. - this.timeout = setTimeout(() => { + this.timer = setTimeout(() => { addObserveTarget(targetFunc(), this); // Mock Event object. this.updatePosition(); @@ -119,14 +118,13 @@ class Affix extends React.Component { ) { this.updatePosition(); } - this.measure(); } componentWillUnmount() { - if (this.timeout) { - clearTimeout(this.timeout); - this.timeout = null; + if (this.timer) { + clearTimeout(this.timer); + this.timer = null; } removeObserveTarget(this); this.updatePosition.cancel(); @@ -141,20 +139,17 @@ class Affix extends React.Component { getOffsetBottom = () => this.props.offsetBottom; - savePlaceholderNode = (node: HTMLDivElement) => { - this.placeholderNode = node; - }; - - saveFixedNode = (node: HTMLDivElement) => { - this.fixedNode = node; - }; - // =================== Measure =================== measure = () => { const { status, lastAffix } = this.state; const { onChange } = this.props; const targetFunc = this.getTargetFunc(); - if (status !== AffixStatus.Prepare || !this.fixedNode || !this.placeholderNode || !targetFunc) { + if ( + status !== AffixStatus.Prepare || + !this.fixedNodeRef.current || + !this.placeholderNodeRef.current || + !targetFunc + ) { return; } @@ -170,7 +165,7 @@ class Affix extends React.Component { status: AffixStatus.None, }; const targetRect = getTargetRect(targetNode); - const placeholderReact = getTargetRect(this.placeholderNode); + const placeholderReact = getTargetRect(this.placeholderNodeRef.current); const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop); const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom); @@ -244,9 +239,9 @@ class Affix extends React.Component { const offsetBottom = this.getOffsetBottom(); const targetNode = targetFunc(); - if (targetNode && this.placeholderNode) { + if (targetNode && this.placeholderNodeRef.current) { const targetRect = getTargetRect(targetNode); - const placeholderReact = getTargetRect(this.placeholderNode); + const placeholderReact = getTargetRect(this.placeholderNodeRef.current); const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop); const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom); @@ -288,9 +283,9 @@ class Affix extends React.Component { return ( -
+
{affixStyle &&