Skip to content

Commit

Permalink
fix(Dropdown): prefer key property for generated options
Browse files Browse the repository at this point in the history
  • Loading branch information
David Zukowski committed Apr 28, 2017
1 parent c6f80cf commit c4194fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 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}-${i}`} value={option.value}>{option.text}</option>
))}
</select>
)
Expand Down Expand Up @@ -1152,7 +1152,7 @@ export default class Dropdown extends Component {

return _.map(options, (opt, i) => (
<DropdownItem
key={`${opt.value}-${i}`}
key={opt.key || `${opt.value}-${i}`}
active={isActive(opt.value)}
onClick={this.handleItemClick}
selected={selectedIndex === i}
Expand Down
22 changes: 22 additions & 0 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,28 @@ describe('Dropdown', () => {
})
})

describe('renderOptions', () => {
it('prefers the option\'s "key" property for keying generated DropdownItems', () => {
const customOptions = [
{ text: '1', value: '1', key: 'a' },
{ text: '2', value: '2', key: 'b' },
]
wrapperShallow(<Dropdown options={customOptions} />)
wrapper.should.contain('DropdownItem[key="a"]')
wrapper.should.contain('DropdownItem[key="b"]')
})

it('generates a "key" property using the option.value if no key is provided', () => {
const customOptions = [
{ text: '1', value: '1' },
{ text: '2', value: '2' },
]
wrapperShallow(<Dropdown options={customOptions} />)
wrapper.should.contain('DropdownItem[key="1-0"]')
wrapper.should.contain('DropdownItem[key="2-1"]')
})
})

describe('Dropdown.Menu child', () => {
it('renders child passed', () => {
wrapperShallow(
Expand Down

0 comments on commit c4194fa

Please sign in to comment.