-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dropdown: option keys fallback to values incorrectly #1720
Comments
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 |
@notgiorgi Nice catch, there is also #1710. I think that both issues should be fixed in a single PR. |
I can do them both, which solution should I go with? I mentioned two above. |
I don't like idea with getValidKeyI think it will be enought to use lodash's const getKeyOrVal = (key, value) => _.isNil(key) ? value : key And it should be placed directly in These lines should be updated: update factory's usageThis will fix #1710. I commented why this problem occurs, fix for it is pretty simple: return _.map(options, (opt, i) => DropdownItem.create({
active: isActive(opt.value),
onClick: this.handleItemClick,
selected: selectedIndex === i,
...opt,
+ key: getKeyOrValue(opt.key, opt.value),
// Needed for handling click events on disabled items
style: { ...opt.style, pointerEvents: 'all' },
})) @notgiorgi I'll be glad to see PR that makes these changes. |
I was gonna say, I'll get into it on weekend, but it seems to be done already 😄 |
Yes sir, cutting a release as we speak. |
Released in |
Steps
if we define our
Dropdown
component like this:Note: this only happens with
selection
proptrue
, because only in that case,select
is rendered behind the scenesExpected Result
This should render
<select>
with two<option>
s with keys0
and1
Actual Result
This tries to render
<select>
with two<option>
s with keys1
and1
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
whenoption.key
is eitherundefined
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)
The text was updated successfully, but these errors were encountered: