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.

Preventing Selecting "Disabled" items?

See original GitHub issue
  • downshift version: 1.21.1
  • node version: 6.9.1
  • npm (or yarn) version: yarn 1.20

Relevant code or config

  handleSelection = (selectedItem, stateAndHelpers) => {
    if (!selectedItem) { return; }

    if (selectedItem.disabled) {
      stateAndHelpers.clearSelection();
      return;
    }

    if (this.props.clearOnSelect) { stateAndHelpers.clearSelection(); }
    stateAndHelpers.closeMenu();
    this.props.onSelect(selectedItem);
  }

...
  render() {
    return (
      <Downshift
        defaultHighlightedIndex={0}
        onSelect={this.handleSelection}
        itemToString={item => (item ? item.name : '')}
        render={this.renderAutocomplete}
      />
    );
  }

What you did:

I have a list of items, users in this case, some of which are disabled. I’m trying to prevent downshift from allowing them to be selected.

What happened:

So far, this works OK, except it also clears the input value – which is what clearSelection was designed to do.

Reproduction repository: I’ll try to extract some code later, but I think this makes sense. This is a feature request /question and not a bug. 😃

Problem description: I’d like to be able to support disabled items. I think I might be able to get around this by managing the selectedItem state myself, but that seems like more than I’d like to do if possible.

Suggested solution: It would be nice if a disabled attribute on an item in a menu prevented any selection from happening. Optionally, arrow keys could skip over them.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, Nov 22, 2018

Actually, thinking about it I just realized that this is not necessary. Just don’t call getItemProps for an item which you render which is disabled. Like in this example: https://codesandbox.io/s/v666z3o6ml

1reaction
cycomacheadcommented, Nov 30, 2017

Hi Kent,

Thanks so much for the quick response! I hadn’t yet considered not sending item props. That’s brilliantly simple! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to show disable HTML select option in by default?
This will allow the user to see the first option as a disabled heading ('Choose Tagging'), and select all other options. enter image...
Read more >
How to avoid selection of a disabled item of a ...
I need to avoid selection of disabled item of CheckedListBoxEdit when user click on select All. right now if i uncheck that select...
Read more >
HTML select disabled Attribute - W3Schools
The disabled attribute is a boolean attribute. When present, it specifies that the drop-down list should be disabled. A disabled drop-down list is...
Read more >
HTML attribute: disabled - HTML: HyperText Markup Language
This Boolean attribute prevents the user from interacting with the button. If this attribute isn't set, the button can still be disabled from...
Read more >
How to disable the drop-down list in HTML5 ? - GeeksforGeeks
disabled : The <select> disabled attribute is used to specify the select element is disabled. A disabled drop-down list is un-clickable and ...
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