Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggested approach for focussing inputs after render #23

Open
samlown opened this issue Mar 13, 2018 · 4 comments
Open

Suggested approach for focussing inputs after render #23

samlown opened this issue Mar 13, 2018 · 4 comments

Comments

@samlown
Copy link

samlown commented Mar 13, 2018

Is there a suggested approach for focussing input fields when a component is rendered? The autoFocus parameter works on a page refresh, but not when replacing the current contents.

The following approach does not work as the ref is associated with the Input class instance as opposed to the rendered input tag, and the focus() call is not forwarded:

componentDidMount() {
  this.inputField.focus();
}

render() {
  return (
     <div>
        <Input ref={ (input) => { this.inputField = input } } name="foo" /> 
     </div>
  )
}

I'm quite new to React, so maybe I'm missing something.

Thanks.

@kulakowka
Copy link
Owner

You should use findDomNode()

@samlown
Copy link
Author

samlown commented Mar 13, 2018

My understanding is that the use of this method is not recommended. It also doesn't appear to work in preact. Perhaps for these cases it will be easier just to provide the input box manually.

@kulakowka
Copy link
Owner

I think this is a preact problem, not this library. Why you do not want to use findDomNode?

We could introduce a new props "inputRef", but this does not seem to me to be the right decision.

@samlown
Copy link
Author

samlown commented Mar 16, 2018

It looks like findDomNode is supported in the preact-compat module, but I'd honestly prefer to keep the code as lean as possible.

Given that focus() is an important function for Input boxes, perhaps it's worth adding a wrapper method to the Input component? That way the regular and recommended ref can be used without issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants