Skip to content

Commit

Permalink
refactor(react): replace findDOMNode by ref (#7479)
Browse files Browse the repository at this point in the history
  • Loading branch information
renjie-run authored Feb 18, 2025
1 parent e242c77 commit 680006a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
11 changes: 2 additions & 9 deletions frontend/src/components/common/account.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { findDOMNode } from 'react-dom';
import { Utils } from '../../utils/utils';
import { seafileAPI } from '../../utils/seafile-api';
import { siteRoot, isPro, gettext, appAvatarURL, enableSSOToThirdpartWebsite } from '../../utils/constants';
Expand Down Expand Up @@ -35,10 +34,6 @@ class Account extends Component {
this.handleProps();
}

getContainer = () => {
return findDOMNode(this);
};

handleProps = () => {
if (this.state.showInfo) {
this.addEvents();
Expand All @@ -61,9 +56,7 @@ class Account extends Component {

handleDocumentClick = (e) => {
if (e && (e.which === 3 || (e.type === 'keyup' && e.which !== Utils.keyCodes.tab))) return;
const container = this.getContainer();

if (container.contains(e.target) && container !== e.target && (e.type !== 'keyup' || e.which === Utils.keyCodes.tab)) {
if (this.accountDOM && this.accountDOM.contains(e.target) && this.accountDOM !== e.target && (e.type !== 'keyup' || e.which === Utils.keyCodes.tab)) {
return;
}

Expand Down Expand Up @@ -148,7 +141,7 @@ class Account extends Component {

render() {
return (
<div id="account">
<div id="account" ref={ref => this.accountDOM = ref}>
<a id="my-info" href="#" onClick={this.onClickAccount} className="account-toggle no-deco d-none d-md-block" aria-label={gettext('View profile and more')}>
{this.renderAvatar()}
</a>
Expand Down
13 changes: 8 additions & 5 deletions frontend/src/components/sf-table/masks/cell-mask.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import React from 'react';
import { findDOMNode } from 'react-dom';
import PropTypes from 'prop-types';

class CellMask extends React.PureComponent {

componentDidUpdate() {
// Scrolling left and right causes the interface to re-render,
// and the style of CellMask is reset and needs to be fixed
const dom = findDOMNode(this);
if (dom.style.position === 'fixed') {
dom.style.transform = 'none';
if (this.cellMaskNode && this.cellMaskNode.style.position === 'fixed') {
this.cellMaskNode.style.transform = 'none';
}
}

Expand All @@ -27,14 +25,19 @@ class CellMask extends React.PureComponent {
};
};

setNode = (node) => {
this.cellMaskNode = node;
this.props.innerRef && this.props.innerRef(node);
};

render() {
const { width, height, top, left, zIndex, children, innerRef, ...rest } = this.props;
const style = this.getMaskStyle();
return (
<div
style={style}
data-test="cell-mask"
ref={innerRef}
ref={this.setNode}
{...rest}
>
{children}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { findDOMNode } from 'react-dom';
import classnames from 'classnames';
import { KeyCodes } from '../../../../constants';
import { CellValueType } from './constants';
Expand Down Expand Up @@ -105,12 +104,11 @@ class SimpleTextEditor extends Component {
};

getInputNode = () => {
const domNode = findDOMNode(this.input);
if (domNode.tagName === 'INPUT') {
return domNode;
if (!this.input) return null;
if (this.input.tagName === 'INPUT') {
return this.input;
}

return domNode.querySelector('input:not([type=hidden])');
return this.input.querySelector('input:not([type=hidden])');
};

setInputRef = (input) => {
Expand Down
13 changes: 8 additions & 5 deletions frontend/src/metadata/views/table/masks/cell-mask.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import React from 'react';
import { findDOMNode } from 'react-dom';
import PropTypes from 'prop-types';

class CellMask extends React.PureComponent {

componentDidUpdate() {
// Scrolling left and right causes the interface to re-render,
// and the style of CellMask is reset and needs to be fixed
const dom = findDOMNode(this);
if (dom.style.position === 'fixed') {
dom.style.transform = 'none';
if (this.cellMaskNode && this.cellMaskNode.style.position === 'fixed') {
this.cellMaskNode.style.transform = 'none';
}
}

Expand All @@ -27,14 +25,19 @@ class CellMask extends React.PureComponent {
};
};

setNode = (node) => {
this.cellMaskNode = node;
this.props.innerRef && this.props.innerRef(node);
};

render() {
const { width, height, top, left, zIndex, children, innerRef, ...rest } = this.props;
const style = this.getMaskStyle();
return (
<div
style={style}
data-test="cell-mask"
ref={innerRef}
ref={this.setNode}
{...rest}
>
{children}
Expand Down

0 comments on commit 680006a

Please sign in to comment.