question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-select focus() doesn't show cursor after change

See original GitHub issue

Stackoverflow question: https://stackoverflow.com/questions/58538908/react-select-focus-doesnt-show-cursor-after-change

I’m trying to use ref and focus() to manually set the focus into the control input field. In most instances it works, but not immediately after selecting an Option from the dropdown.

After selecting an option from the dropdown, the control gets the focus but the cursor doesn’t appear. It only appears if you start typing (including hitting the Esc key). On subsequent openings of the menu, the cursor appears along with the focus of the entire control field. Any ideas how to get this working in all situations, including immediately after making a selection?

I’ve created a sample code here: https://codesandbox.io/s/react-select-focus-uk3ic

Screenshot 2019-10-24 at 10 58 00

This is the code:

import React, { Component } from "react";
import ReactDOM from "react-dom";
import Select from "react-select";
import styled from "styled-components";

import { stateOptions } from "./data.js";

class PopoutExample extends Component {
  selectRef = React.createRef();

  state = {
    isOpen: false,
    option: undefined,
  };

  toggleOpen = () => {
    const isOpening = !this.state.isOpen;
    this.setState(
      {
        isOpen: isOpening,
      },
      () => isOpening && setTimeout(() => this.selectRef.focus(), 400),
    );
  };

  onSelectChange = option => {
    this.toggleOpen();
    this.setState({ option });
  };

  render() {
    const { isOpen, option } = this.state;
    return (
      <Dropdown
        target={
          <MainButton onClick={this.toggleOpen}>
            {option ? option.label : "Select a State"}
          </MainButton>
        }
      >
        <Select
          menuIsOpen
          ref={ref => {
            this.selectRef = ref;
          }}
          styles={{
            container: provided => ({
              ...provided,
              display: isOpen ? "block" : "none",
            }),
          }}
          onChange={this.onSelectChange}
          options={stateOptions}
          value={option}
          controlShouldRenderValue={false}
        />
      </Dropdown>
    );
  }
}

const MainButton = styled.button`
  padding: 10px;
  background-color: aqua;
  width: 100%;
`;

const Dropdown = ({ children, target }) => (
  <div>
    {target}
    {children}
  </div>
);

ReactDOM.render(<PopoutExample />, document.getElementById("root"));

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:13
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
namKolocommented, Nov 1, 2019

@swiss-chris I think this could be workaround for you demofixing

1reaction
swiss-chriscommented, Nov 7, 2019

@namKolo What worked for me was a combination of keeping menuIsOpen to true (because I want the dropdown to remember the scroll position), but also adding openMenuOnFocus as you suggested ! Thank you very much for this solution 🙏 !!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-select focus() doesn't show cursor after change
After selecting an option from the dropdown, the control gets the focus but the cursor doesn't appear. It only appears if you start...
Read more >
API - React Select
A flexible and beautiful Select Input control for ReactJS with multiselect, autocomplete and ajax support.
Read more >
focus-visible - CSS: Cascading Style Sheets - MDN Web Docs
If your code has to work in old browser versions that do not support :focus-visible , check supports of :focus-visible with @supports and ......
Read more >
react-select focus() stackoverflow - CodeSandbox
CodeSandbox is an online editor tailored for web applications. ... stackoverflow.com/questions/58538908/react-select-focus-doesnt-show-cursor-after-change.
Read more >
Initializing focus state in React: you might be doing it wrong
Unfortunately, trying to use :focus limits what you can do: you can style the input or siblings that come after the input… but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found