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.

Key press issue with custom Control

See original GitHub issue

I’m using react-select v3.1 and I’m experiencing some very odd behavior, not sure if I’m doing something wrong or if there is a bug.

Scenario : I am making a custom select and I want to create a custom Control that the user can type in to perform searches.

Expected behavior: User can type normally in the custom Control to search for an item to select

Current behavior: Custom Control is made but the typing experience is not working correctly. Some key presses aren’t being triggered in the custom control such as backspace, space-bar and delete. Here is some code that illustrates what I am currently doing and the result (this is just for illustration):

import React from "react";
import Select from "react-select";

const options = [
  { value: "chocolate", label: "Chocolate" },
  { value: "strawberry", label: "Strawberry" },
  { value: "vanilla", label: "Vanilla" },
];

const MyCustomControl = () => {
  return <input />;
};

class App extends React.Component {
  state = {
    selectedOption: null,
  };
  handleChange = (selectedOption) => {
    this.setState({ selectedOption }, () =>
      console.log(`Option selected:`, this.state.selectedOption)
    );
  };
  render() {
    const { selectedOption } = this.state;

    return (
      <Select
        value={selectedOption}
        onChange={this.handleChange}
        isSearchable={true}
        options={options}
        components={{
          Control: MyCustomControl,
        }}
      />
    );
  }
}

export default App;

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
jbyomuhangicommented, Dec 7, 2020

Wow, thanks so much @ebonow! This works! Really appreciate the help and speedy replies

0reactions
ebonowcommented, Dec 7, 2020

@jbyomuhangi Fortunately, this is not an issue. Full explanation and working demo below

You will see that when you comment out line 20, you lose the keyboard controls, so my question is, why does this happen

First, an explanation of the impact of line 20

  • props.children[0] of the Control is the ValueContainer which contains the DummyInput
  • The DummyInput is the element responsible for receiving focus when the Select is clicked on
  • Removing this element removes the input that normally receives focus

But, what about the button?

  • Clicking the button does not maintain focus on it.
  • Adding an onClick handler to it to console.log(document.activeElement) reveals that the focus is on the body, so the keyDown events are no longer captured by the Select.
  • This is also why if you tab over to the button instead of clicking into it, hit Enter, the menu opens and interacts with the arrow keys

what component is responsible for the focus management

How can I achieve what I’m trying to do with react-select?

  • You simply need to maintain focus within the SelectContainer so that the keyDown listener will be able to control the Menu.
const Control = (props) => {
  const onClick = e => e.target.focus();

  return (
    <components.Control {...props}>
      <button onClick={onClick}> hello </button>
    </components.Control>
  );
};

Working demo

I hope this answers all of your questions. It wasn’t entirely intuitive for me either, especially realizing that the button wasn’t maintaining focus. I will close this issue as everything does appear to be functional, but happy to work through any other questions you have. As before, I can reopen this if necessary.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WinForms Custom Control: Why doesn't the arrow key raise ...
This is a dummy custom control. The entire source is at the bottom. If I place the control on an empty form (no...
Read more >
Custom user control key events problem [SOLVED] - Daniweb
I didn't figure out exactly how to do what I had wanted, but I did achieve the end result for my program. I...
Read more >
Control key acts as if it is stuck periodically - Super User
Generic fix for this generic issue: press stuck key or Ctrl + Alt + Del , Esc . · Press both ctrl at...
Read more >
Resolve Hotkeys or Volume control keys not working on ...
This issue may occur if there is a problem with the USB connection on your computer. This issue can also occur if a...
Read more >
If your Mac doesn't respond to key presses - Apple Support
If this solves the problem, you may need to take your keyboard in for service. If no keys work on your Mac notebook...
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