Browse Source

fix: React 17 findDOMNode is deprecated (#27755)

* fix: use ref instead of findDOMNode

* chore: use fillRef in setAndForwardRef

* chore: use rc-util fillRef instead

* chore: use support ref

* test: removed empty class

* chore: use composeRef instead of setAndForwardRef
pull/27805/head
Hossein Mohammadi 4 years ago
committed by GitHub
parent
commit
4b1000fb7f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      components/_util/wave.tsx
  2. 12
      components/anchor/Anchor.tsx

18
components/_util/wave.tsx

@ -1,8 +1,9 @@
import * as React from 'react'; import * as React from 'react';
import { findDOMNode } from 'react-dom';
import TransitionEvents from '@ant-design/css-animation/lib/Event'; import TransitionEvents from '@ant-design/css-animation/lib/Event';
import { supportRef, composeRef } from 'rc-util/lib/ref';
import raf from './raf'; import raf from './raf';
import { ConfigConsumer, ConfigConsumerProps, CSPConfig, ConfigContext } from '../config-provider'; import { ConfigConsumer, ConfigConsumerProps, CSPConfig, ConfigContext } from '../config-provider';
import { cloneElement } from './reactNode';
let styleForPseudo: HTMLStyleElement | null; let styleForPseudo: HTMLStyleElement | null;
@ -30,6 +31,8 @@ export default class Wave extends React.Component<{ insertExtraNode?: boolean }>
cancel: () => void; cancel: () => void;
}; };
private containerRef = React.createRef<HTMLDivElement>();
private extraNode: HTMLDivElement; private extraNode: HTMLDivElement;
private clickWaveTimeoutId: number; private clickWaveTimeoutId: number;
@ -45,7 +48,7 @@ export default class Wave extends React.Component<{ insertExtraNode?: boolean }>
context: ConfigConsumerProps; context: ConfigConsumerProps;
componentDidMount() { componentDidMount() {
const node = findDOMNode(this) as HTMLElement; const node = this.containerRef.current as HTMLDivElement;
if (!node || node.nodeType !== 1) { if (!node || node.nodeType !== 1) {
return; return;
} }
@ -112,7 +115,7 @@ export default class Wave extends React.Component<{ insertExtraNode?: boolean }>
return; return;
} }
const node = findDOMNode(this) as HTMLElement; const node = this.containerRef.current as HTMLDivElement;
if (!e || e.target !== node || this.animationStart) { if (!e || e.target !== node || this.animationStart) {
return; return;
} }
@ -195,7 +198,14 @@ export default class Wave extends React.Component<{ insertExtraNode?: boolean }>
const { children } = this.props; const { children } = this.props;
this.csp = csp; this.csp = csp;
return children; if (!React.isValidElement(children)) return children;
let ref: React.Ref<any> = this.containerRef;
if (supportRef(children)) {
ref = composeRef((children as any).ref, this.containerRef as any);
}
return cloneElement(children, { ref });
}; };
render() { render() {

12
components/anchor/Anchor.tsx

@ -1,5 +1,4 @@
import * as React from 'react'; import * as React from 'react';
import * as ReactDOM from 'react-dom';
import classNames from 'classnames'; import classNames from 'classnames';
import addEventListener from 'rc-util/lib/Dom/addEventListener'; import addEventListener from 'rc-util/lib/Dom/addEventListener';
import Affix from '../affix'; import Affix from '../affix';
@ -100,6 +99,8 @@ export default class Anchor extends React.Component<AnchorProps, AnchorState, Co
content: ConfigConsumerProps; content: ConfigConsumerProps;
private wrapperRef = React.createRef<HTMLDivElement>();
private inkNode: HTMLSpanElement; private inkNode: HTMLSpanElement;
// scroll scope's container // scroll scope's container
@ -253,9 +254,10 @@ export default class Anchor extends React.Component<AnchorProps, AnchorState, Co
}; };
updateInk = () => { updateInk = () => {
const { prefixCls } = this; const { prefixCls, wrapperRef } = this;
const anchorNode = ReactDOM.findDOMNode(this) as Element; const anchorNode = wrapperRef.current;
const linkNode = anchorNode.getElementsByClassName(`${prefixCls}-link-title-active`)[0]; const linkNode = anchorNode?.getElementsByClassName(`${prefixCls}-link-title-active`)[0];
if (linkNode) { if (linkNode) {
this.inkNode.style.top = `${(linkNode as any).offsetTop + linkNode.clientHeight / 2 - 4.5}px`; this.inkNode.style.top = `${(linkNode as any).offsetTop + linkNode.clientHeight / 2 - 4.5}px`;
} }
@ -304,7 +306,7 @@ export default class Anchor extends React.Component<AnchorProps, AnchorState, Co
}; };
const anchorContent = ( const anchorContent = (
<div className={wrapperClass} style={wrapperStyle}> <div ref={this.wrapperRef} className={wrapperClass} style={wrapperStyle}>
<div className={anchorClass}> <div className={anchorClass}>
<div className={`${prefixCls}-ink`}> <div className={`${prefixCls}-ink`}>
<span className={inkClass} ref={this.saveInkNode} /> <span className={inkClass} ref={this.saveInkNode} />

Loading…
Cancel
Save