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.

Dropdown: option keys fallback to values incorrectly

See original GitHub issue

Steps

if we define our Dropdown component like this:

const opts = [
  {
    key: 0,
    value: 1,
    text: 'Foo',
  },
  {
    key: 1,
    value: 2,
    text: 'Bar',
  },
]

const MyDropdown = () => <Dropdown selection options={opts} />

Note: this only happens with selection prop true, because only in that case, select is rendered behind the scenes

Expected Result

This should render <select> with two <option>s with keys 0 and 1

Actual Result

image

This tries to render <select> with two <option>s with keys 1 and 1 because of { option.key || option.value } (see this line of the code https://github.com/Semantic-Org/Semantic-UI-React/blob/master/src/modules/Dropdown/Dropdown.js#L1074)

I think this line should be rewritten to only fallback to option.value when option.key is either undefined or empty string. All other falsy values are valid keys, react converts them to strings.

Version

last

Testcase

https://codepen.io/notgiorgi/pen/ZKgBob?editors=0010 (you cannot use devtools on codepen)

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
notgiorgicommented, May 31, 2017

One approach can be to write a helper function:

function getValidKey(key) {
  switch (key) {
    case null: return 'null'
    case false: return 'false'
    case 0: return '0'

    case undefined:
    case '':
      return false
    default: return key
  }
}

and then

key={getValidKey(option.key) || option.value}

Or we could do

function getValidKey(key, fallback) {
  switch (key) {
    case undefined:
    case '':
      return fallback
    default: return key
  }
}

and then

key={getValidKey(option.key, option.value)}

I can send a PR

0reactions
levithomasoncommented, Jun 1, 2017

Released in semantic-ui-react@0.68.5.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to implement a better fallback when a selected option in ...
I have a dropdown list called Reward. All the options in the dropdown list are populated from the info submitted by users elsewhere, ......
Read more >
Dropdown: items missing "key" prop, React warning displayed
Keys in Dropdown options are still required and still work. If you do not pass a key we will use the value as...
Read more >
Striking a Balance Between Native and Custom Select Elements
We're going to build a styled select element. Not just the outside, ... Clicking an option in the listbox updates the selected value....
Read more >
Update enables SSL 3.0 fallback warnings in Internet Explorer ...
On the Edit menu, point to New, and then click DWORD Value. Type EnableSSL3Fallback, and then press the Enter key. In the Details...
Read more >
Essentials - i18next documentation
Calling the t function with an array of keys enables you to translate dynamic keys providing a non specific fallback value. As a...
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