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.

Improving docs/examples for simple "dropdown select" component

See original GitHub issue

I’m new to Downshift and really impressed with what I’ve seen so far! I just want to provide some feedback regarding one aspect I’m struggling with - implementing a simple “dropdown select” component (similar to HTML “select” element) where the items are not just strings, but instead “key/value” pairs.

I’m finding that most examples use a string array for their items and also they usually start with an empty default value, allowing the user to type into the input and show suggestions in the dropdown menu. I don’t want any input at all. Instead, I want a list of item objects that represent key/value pairs (such as “id” and “label” for example), then I want to be able to set the selectedItem using a string prop that specifies the “item id” only.

When I came to Downshift for the first time, what I was hoping to find was a simple example that demonstrates how to implement the following component in Downshift:

const items = [{ id: 'one', label: 'Item 1' }, { id: 'two', label: 'Item 2' }];
<Select selectedItem='two' items={items}/> 

The requirements being:

  1. Item 2 is selected by default.
  2. items is an array of objects rather than strings.
  3. The selectedItem prop is a string representing the select item as the “id” attribute in the items array.

I’ve been trying to implement this off and on for a few days now but all my attempts so far feel a bit to hacky for my liking and I think I’m missing something fundamental about how Downshift works. I can’t seem to figure out the right way to have a selectedItem representing the “id” in the items object, if that makes sense?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
alexandernanbergcommented, Jun 22, 2018

I usually solve this by doing something like this: https://codesandbox.io/s/xpwovvx5vz

The usage looks like this. The defaultValue or value have to match the value of one of the items in the options array.

<Select
  label="Select fruit"
  defaultValue="apple"
  options={[
    { value: "apple", label: "Apple" },
    { value: "orange", label: "Orange" }
  ]}
/>

When you need to use this in a form I usually use a native <select /> element and sync the selectedItem to it. I can provide an example of it if anyone is interested.

2reactions
mischa-scommented, Mar 9, 2020

Are the work around’s suggested here still the best way to achieve this? Isn’t this a pretty common use case to need to present the label to the user and then use the value only when submitting the form?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bootstrap Dropdowns - examples & tutorial
Responsive dropdown/caret built with Bootstrap 5. Examples of navbar dropdown, dropdown list, submenu, dropdown toggle, dropdown hover, hamburger icon & more.
Read more >
Dropdown design: guidelines for web and mobile
We see dropdown components everywhere. What makes a good dropdown design? What changes when designing for web or mobile?
Read more >
Dropdowns: Design Guidelines
Dropdown boxes and menus are overused and clunky but can be useful for revealing a list of options or commands.
Read more >
Customize a reusable React dropdown menu component
A dropdown menu consists of four basic components: the header wrapping, header title, list wrapping, and list items. ... We need to be...
Read more >
Dropdown fields | Blockly
On this page · Creation. Simple text dropdowns; Image dropdowns; Dynamic dropdowns · Serialization · Customization. Dropdown arrow; Menu height.
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