Skip to content
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

fix(Dropdown): fix key handling for options #1639

Merged
merged 2 commits into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -1058,8 +1058,8 @@ export default class Dropdown extends Component {
return (
<select type='hidden' aria-hidden='true' name={name} value={value} multiple={multiple}>
<option value='' />
{_.map(options, option => (
<option key={option.value} value={option.value}>{option.text}</option>
{_.map(options, (option, i) => (
<option key={option.key || option.value} value={option.value}>{option.text}</option>
Copy link
Member Author

@dvdzkwsk dvdzkwsk Apr 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To retain parity with the generated DropdownItem this should likely include the -${index} suffix when using the value as the key. I originally included that fix in this change, but then redacted it since I'd prefer we stop using indexes in keys and require users to provide their own when values are non-unique.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. In fact, this should use a factory which already requires users to use keys when passing objects or elements. We use literal values for the key otherwise.

See factories.js where we should likely add an HTML factory for option:
https://github.com/Semantic-Org/Semantic-UI-React/blob/master/src/lib/factories.js#L125

))}
</select>
)
Expand Down Expand Up @@ -1124,7 +1124,7 @@ export default class Dropdown extends Component {
const defaultProps = {
active: item.value === selectedLabel,
as: 'a',
key: item.value,
key: item.key || item.value,
onClick: this.handleLabelClick,
onRemove: this.handleLabelRemove,
value: item.value,
Expand All @@ -1150,17 +1150,14 @@ export default class Dropdown extends Component {
? optValue => _.includes(value, optValue)
: optValue => optValue === value

return _.map(options, (opt, i) => (
<DropdownItem
key={`${opt.value}-${i}`}
active={isActive(opt.value)}
onClick={this.handleItemClick}
selected={selectedIndex === i}
{...opt}
// Needed for handling click events on disabled items
style={{ ...opt.style, pointerEvents: 'all' }}
/>
))
return _.map(options, (opt, i) => DropdownItem.create({
active: isActive(opt.value),
onClick: this.handleItemClick,
selected: selectedIndex === i,
...opt,
// Needed for handling click events on disabled items
style: { ...opt.style, pointerEvents: 'all' },
}))
}

renderMenu = () => {
Expand Down
7 changes: 6 additions & 1 deletion src/modules/Dropdown/DropdownItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, { Component } from 'react'
import {
childrenUtils,
createShorthand,
createShorthandFactory,
customPropTypes,
META,
getElementType,
Expand All @@ -20,7 +21,7 @@ import Label from '../../elements/Label'
/**
* An item sub-component for Dropdown component.
*/
export default class DropdownItem extends Component {
class DropdownItem extends Component {
static propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,
Expand Down Expand Up @@ -162,3 +163,7 @@ export default class DropdownItem extends Component {
)
}
}

DropdownItem.create = createShorthandFactory(DropdownItem, opts => opts)

export default DropdownItem
1 change: 1 addition & 0 deletions test/specs/modules/Dropdown/DropdownItem-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('DropdownItem', () => {
common.propKeyOnlyToClassName(DropdownItem, 'selected')
common.propKeyOnlyToClassName(DropdownItem, 'active')

common.implementsCreateMethod(DropdownItem)
common.implementsIconProp(DropdownItem)
common.implementsLabelProp(DropdownItem)
common.implementsImageProp(DropdownItem)
Expand Down