Skip to content

Commit

Permalink
fix(Dropdown): Fix onClick for child elements not getting invoked (#208)
Browse files Browse the repository at this point in the history
* Override toggle dropdown click inside

* Fixes for review and updating the demo

* Fixing issues with onClick of dropdown children not getting invoked

* Fix wording on demo for dropdown
  • Loading branch information
burnumd authored Aug 16, 2019
1 parent 7451edc commit 2d75eaa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/components/Dropdown/Dropdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ By default the dropdown menu will not close when an item within the dropdown is

```jsx
<Dropdown label="Toggle On Click" toggleDropdownOnClickInside>
<Button variant="plain">Item one</Button>
<Button variant="plain">Item two</Button>
<Button onClick={() => alert('Clicked Item one')}>Item one</Button>
<Button onClick={() => alert('Clicked Item two')}>Item two</Button>
</Dropdown>
```
3 changes: 2 additions & 1 deletion src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export class Dropdown extends React.PureComponent<DropdownProps & React.HTMLAttr
return false;
}

if (event.targets(ReactDOM.findDOMNode(this)) && !this.props.toggleDropdownOnClickInside && (!event.isKeyEvent() || event.isTabKeyPress())) {
const preventToggleOnInsideClick = !event.isKeyEvent() && !this.props.toggleDropdownOnClickInside;
if (event.targets(ReactDOM.findDOMNode(this)) && (preventToggleOnInsideClick || event.isTabKeyPress())) {
// If the user clicks, touches or tabs inside the dropdown do not close the menu
return false;
}
Expand Down
22 changes: 21 additions & 1 deletion src/components/Dropdown/DropdownEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,27 @@ import AbstractUserActionEvent from '../util/AbstractUserActionEvent';
export default class DropdownEvent extends AbstractUserActionEvent {

public static handler = (callback) => {
return AbstractUserActionEvent.handler(callback, DropdownEvent);
const eventHandler = (event) => {
callback(new DropdownEvent(event));
};
return ({
register: () => {
['click'].forEach(event =>
document.addEventListener(event, eventHandler, false)
);
['touchstart', 'keyup'].forEach(event =>
document.addEventListener(event, eventHandler, true)
);
},
deregister: () => {
['click'].forEach(event =>
document.removeEventListener(event, eventHandler, false)
);
['touchstart', 'keyup'].forEach(event =>
document.removeEventListener(event, eventHandler, true)
);
}
});
}

public isUnhandledKeyPress() : boolean {
Expand Down

0 comments on commit 2d75eaa

Please sign in to comment.