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): open/close on focus/blur #500

Merged
merged 9 commits into from
Sep 16, 2016
Merged

Conversation

levithomason
Copy link
Member

@levithomason levithomason commented Sep 15, 2016

Fixes #493

The Dropdown opens/closes on focus/blur. Focus and blur are triggered by either keyboard (tab) or mouse events (click). Focus and blur fire before on mouse down while click fires on mouse up.

This meant focus and blur would cause the menu to open/close before the click handler would also toggle the menu close. Further, the blur event would close the menu before the click event could fire on a menu item.

This PR adds a isMouseDown flag. Focus/blur functionality for open/close is only called if the mouse is not down. This allows the user to tab to open/close, but the focus/blur fired from a click is ignored. Allowing the user to select items without the Dropdown closing and click on the Dropdown to toggle its open state without double toggling the open state.

@@ -849,9 +863,11 @@ export default class Dropdown extends Component {
active={isActive(opt.value)}
onClick={this.handleItemClick}
selected={selectedIndex === i}
onMouseDown={e => e.preventDefault()} // prevent default to allow item select without closing on blur
// prevent default to allow item select without closing on blur
onMouseDown={e => e.preventDefault()}
Copy link
Member

Choose a reason for hiding this comment

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

I think you can get rid of this now since preventDefault here is meant to prevent closing on blur but you're preventing that more completely via this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would have thought so, but it didn't seem to be the case. I'll look at it again and see.

@jeffcarbs
Copy link
Member

I pulled this branch down to test it locally and it seems to have fixed the problems referenced in that issue, including interacting with focusable elements within the dropdown or items.

@levithomason
Copy link
Member Author

Awesome, thanks much. Just need to get tests updated now.

@codecov-io
Copy link

codecov-io commented Sep 16, 2016

Current coverage is 98.68% (diff: 100%)

Merging #500 into master will increase coverage by 0.01%

@@             master       #500   diff @@
==========================================
  Files           102        102          
  Lines          1503       1520    +17   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits           1483       1500    +17   
  Misses           20         20          
  Partials          0          0          

Powered by Codecov. Last update de2b2fa...855bce4

@levithomason levithomason force-pushed the fix/dropdown-focus-blur branch from 267ba5a to 2f4ddf1 Compare September 16, 2016 14:16
}, {
dropdownClasses: 'active visible',
menuClasses: 'visible',
})
Copy link
Member Author

Choose a reason for hiding this comment

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

We should only be setting the visibility classes if the open state was successful. I moved these to their respective render functions.

@levithomason
Copy link
Member Author

I've updated per the comments, thanks. I also fixed another bug.

The visibility classes were being assigned every time open/close was called. However, open/close only tries to set state, but defers to the open prop if it exists. So, if the open prop was false (controlled component) then the state would not set to open but the classes would still be applied, showing the menu. Now, those classes are calculated in the render functions based on the open state.

@levithomason levithomason merged commit 704697a into master Sep 16, 2016
@levithomason levithomason deleted the fix/dropdown-focus-blur branch September 16, 2016 15:28
@levithomason
Copy link
Member Author

Released in stardust@0.44.2

@mclarentgp
Copy link
Contributor

I am noticing an issue where I have a drop down with an OnChange event and after clicking on the drop down and selecting the value the OnChange fires off as expected, but if after doing this action and i click anywhere else on the screen it triggers the OnChange event again. I was just wondering does this fix address this issue or is what im experiencing completely seperate from this fix?

@levithomason
Copy link
Member Author

levithomason commented Sep 16, 2016

This sounds like an unrelated issue. Suggest testing against the latest version. If it still occurs, please open a separate issue with explicit steps to reproduce. Thanks for the report.

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

Successfully merging this pull request may close these issues.

Dropdown: <Dropdown.Item as={'a'} /> with href does not function appropriately
4 participants