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

Don't crash dropdown menu when selectedIndex is less than 0 #480

Merged
merged 7 commits into from
Apr 9, 2015

Conversation

jeroencoumans
Copy link
Contributor

Fixes 'Uncaught TypeError: Cannot read property 'text' of undefined' when passing an index of -1.

@@ -48,12 +48,14 @@ var DropDownMenu = React.createClass({
'mui-open': this.state.open
});

var selectedIndex = this.state.selectedIndex > -1 ? this.state.selectedIndex : 0;
Copy link
Member

Choose a reason for hiding this comment

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

I would rather right

var selectedIndex = this.state.selectedIndex;
selectedIndex = (selectedIndex > -1) ? selectedIndex : 0;

for a better optimisation with uglifyjs

@mmrtnz
Copy link
Contributor

mmrtnz commented Mar 30, 2015

Can you include a console warning when users pass a negative index? Negative indices are typically due to poorly written code or bugs. A warning will notify users of the potential error. I would also follow @oliviertassinari's suggestion.

Please follow these examples when writing the warning.

This PR will be good to go after those two changes are made.

@jeroencoumans
Copy link
Contributor Author

I've changed it according to your comments. I also moved the check to componentWillReceiveProps, which seems more appropriate than doing this in render.

@oliviertassinari
Copy link
Member

Be aware that componentWillReceiveProps is not called for the initial render.

@jeroencoumans
Copy link
Contributor Author

I've made a separate method that's called on mounting and when receiving props. Merged and pulled and rebased against master... let me know if this is fine :)

mmrtnz added a commit that referenced this pull request Apr 9, 2015
Don't crash dropdown menu when selectedIndex is less than 0
@mmrtnz mmrtnz merged commit 7fb3605 into mui:master Apr 9, 2015
@mmrtnz
Copy link
Contributor

mmrtnz commented Apr 9, 2015

Thanks @jeroencoumans!

@zannager zannager added the component: menu This is the name of the generic UI component, not the React module! label Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: menu This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants