简体   繁体   English

将ref传递给组件props,然后将焦点放在div上的输入元素上,单击React.createElement

[英]Passing a ref to component props then focus on input element on div click with React.createElement

struggling to get my head round refs and how they work... Perhaps someone can help? 努力争取我的裁判以及他们的工作方式...也许有人可以帮忙?

Basically, I am trying to get the cursor to focus on the input element when I click the div, but struggling to figure out how it works. 基本上,当我单击div时,我试图使光标集中在输入元素上,但是努力弄清楚它是如何工作的。

We click the "Rename" h4 tag 我们单击“重命名” h4标签

overlay = info => {
    const arr = ['Inbox', 'Business', 'Personal'];
    if (arr.indexOf(info.node.props.title) >= 0) {
        return <h4 onClick={() => this.newFolder(info)}>New Folder</h4>;
    } else {
        return (
            <div>
                <h4 onClick={() => this.renameFolder(info)}>Rename</h4>
                <h4 onClick={() => this.newFolder(info)}>New Folder</h4>
                <h4>Delete</h4>
                {/* <h4>{info.node.props.title}</h4> */}
            </div>
        );
    }
};

    renameFolder(info) {
    this.props.renameFolder({
        tree: [...this.props.tracks.tree],
        key: info.node.props.eventKey
    });
    // const e = 'theevent';
    // this.handleFocus(e);
    if (this.toolTip) {
        ReactDOM.unmountComponentAtNode(this.cmContainer);
        this.toolTip = null;
    }
    () => this.myInp.focus();
}

The component. 组件。

<TreeNode
    key={item.key}
    ref={ref={(ip) => this.myInp = ip}}
    handleEditable={this.handleEditable}
    handleFocus={this.handleFocus}
    title={item.title}
    editable={true}
    draggable={false}/>

How the component and input is created. 组件和输入的创建方式。

this.renderSelector = function() {
    var dragNodeHighlight = _this2.state.dragNodeHighlight;
    var _props6 = _this2.props;
    var title = _props6.title,
        selected = _props6.selected,
        editable = _props6.editable,
        handleEditable = _props6.handleEditable,
        handleFocus = _props6.handleFocus,
        ref = _props6.ref
icon = _props6.icon,
        loading = _props6.loading;
    var _context$rcTree5 = _this2.context.rcTree,
        prefixCls = _context$rcTree5.prefixCls,
        showIcon = _context$rcTree5.showIcon,
        treeIcon = _context$rcTree5.icon,
        draggable =
            _props6.draggable === false ? false : _context$rcTree5.draggable,
        loadData = _context$rcTree5.loadData;
    var disabled = _this2.isDisabled();

The input field with the passed down ref prop. 具有向下传递的ref属性的输入字段。

    var $title =  React.createElement('input', {
            type: 'text',
            value: title,
            className: 'rc-input-text',
            onChange: handleEditable,
            onFocus: handleFocus,
            ref: ref
        });

If you using React 16.3+ you can use React.createRef() like this: 如果您使用React 16.3+,则可以使用React.createRef()这样:

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.input = React.createRef();
  }

  focusInput = () => {
    this.input.current.focus();
  }

  render() {
    return (
        <div>
            <div onClick={this.focusInput}>Focus Input</div>
            <input type="text" ref={this.input} />
        </div>
    );
  }
}

For more about the new React.createRef() see: https://reactjs.org/docs/refs-and-the-dom.html 有关新的React.createRef()更多信息,请参见: https : React.createRef()

Working example: https://jsfiddle.net/uns4jp0o/ 工作示例: https : //jsfiddle.net/uns4jp0o/

Ok, so for anyone that stumbles on to this. 好的,所以对于任何偶然发现此问题的人。 Here's the answer. 这就是答案。

You need to check your lifecycle methods. 您需要检查生命周期方法。 I was getting this.input.current as a null object, however, I was updating what input to display based on the state of one of the child props - editable passed down to the component. 我将this.input.current作为空对象获取,但是,我正在根据子道具之一的状态更新可显示的输入-可编辑的向下传递到组件。

Using a deprecated function, componentDidUpdate - comparing the state to the previous state, I was able to acquire the ref and thus ultimately click on the input! 使用不推荐使用的函数componentDidUpdate-将状态与先前的状态进行比较,我能够获取引用,并最终单击输入!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM