Skip to content

Commit

Permalink
fix(Dropdown): disabled toggle (#1571)
Browse files Browse the repository at this point in the history
* fix: Dropdown disabled toggle issue

This fix addresses the issue where Dropdown will be passed a
`disabled` prop, yet it is still clickable. This is due to context
being passed the user-defined `toggle` prop directly, rather than
our defined `this.toggle` function that includes a check for
`disabled`.

Fixes #1542

* fix: incorrect test assertion

In the previous implementation, Dropdown wasn't properly firing
the toggle function within Dropdown, so the assertion was written
down wrong to compensate for that.
  • Loading branch information
bpas247 authored and TheSharpieOne committed Jul 12, 2019
1 parent ce2a9a0 commit b4edeb8
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Dropdown.js
Expand Up @@ -52,10 +52,11 @@ class Dropdown extends React.Component {

getContextValue() {
return {
toggle: this.props.toggle,
toggle: this.toggle,
isOpen: this.props.isOpen,
direction: (this.props.direction === 'down' && this.props.dropup) ? 'up' : this.props.direction,
inNavbar: this.props.inNavbar,
disabled: this.props.disabled
};
}

Expand Down
1 change: 1 addition & 0 deletions src/DropdownContext.js
Expand Up @@ -7,6 +7,7 @@ import React from 'react';
* isOpen: PropTypes.bool.isRequired,
* direction: PropTypes.oneOf(['up', 'down', 'left', 'right']).isRequired,
* inNavbar: PropTypes.bool.isRequired,
* disabled: PropTypes.bool
* }
*/
export const DropdownContext = React.createContext({});
2 changes: 1 addition & 1 deletion src/DropdownToggle.js
Expand Up @@ -33,7 +33,7 @@ class DropdownToggle extends React.Component {
}

onClick(e) {
if (this.props.disabled) {
if (this.props.disabled || this.context.disabled) {
e.preventDefault();
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/Dropdown.spec.js
Expand Up @@ -735,7 +735,7 @@ describe('Dropdown', () => {

wrapper.find('#first').hostNodes().simulate('keydown', { which: keyCodes.space });

expect(Dropdown.prototype.toggle.mock.calls.length).toBe(0);
expect(Dropdown.prototype.toggle.mock.calls.length).toBe(1);
expect(click.mock.calls.length).toBe(1);

wrapper.detach();
Expand All @@ -761,7 +761,7 @@ describe('Dropdown', () => {

wrapper.find('#first').hostNodes().simulate('keydown', { which: keyCodes.space });

expect(Dropdown.prototype.toggle.mock.calls.length).toBe(0);
expect(Dropdown.prototype.toggle.mock.calls.length).toBe(1);
expect(click.mock.calls.length).toBe(1);

wrapper.detach();
Expand Down

0 comments on commit b4edeb8

Please sign in to comment.