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

Conversation

dvdzkwsk
Copy link
Member

@dvdzkwsk dvdzkwsk commented Apr 28, 2017

Fixes #1634

@codecov-io
Copy link

codecov-io commented Apr 28, 2017

Codecov Report

Merging #1639 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1639      +/-   ##
==========================================
+ Coverage   99.75%   99.75%   +<.01%     
==========================================
  Files         141      141              
  Lines        2406     2408       +2     
==========================================
+ Hits         2400     2402       +2     
  Misses          6        6
Impacted Files Coverage Δ
src/modules/Dropdown/Dropdown.js 100% <100%> (ø) ⬆️
src/modules/Dropdown/DropdownItem.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 444fcef...04ef42b. Read the comment docs.

@dvdzkwsk dvdzkwsk force-pushed the fix/dropdown-keys branch 2 times, most recently from c4194fa to af69e96 Compare April 28, 2017 00:33
{_.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

@@ -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}`}
Copy link
Member

@levithomason levithomason Apr 28, 2017

Choose a reason for hiding this comment

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

Again here, we should add the create method to the DropdownItem. We have a factory creator for this:

DropdownItem.js

Dropdown.create = createShorthandFactory(Dropdown, val => ({ text: val, value: val }))

Then:

_.map(options, opt => Dropdown.create(opt)) // PLUS all the other props here...

Tests:
DropdownItem-test.js

common.implementsCreateMethod(Dropdown)

Copy link
Member Author

Choose a reason for hiding this comment

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

Did not know, will take a look. You mean DropdownItem.create though, right?

Copy link
Member

Choose a reason for hiding this comment

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

Whoops. Yes, sir.DropdownItem.create.

Fixes "key" prop not being respected on options provided to <Dropdown />
Copy link
Member

@levithomason levithomason left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @davezuko

@levithomason levithomason merged commit a9e8870 into master May 10, 2017
@levithomason levithomason deleted the fix/dropdown-keys branch May 10, 2017 16:08
@levithomason
Copy link
Member

Released in semantic-ui-react@0.68.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants