Skip to content

Commit

Permalink
feat(Dropdown): add disabled state to dropdown component (#276)
Browse files Browse the repository at this point in the history
* feat(Dropdown): add disabled state to dropdown component

* refactor(Dropdown): simplify sass and disable cursor
  • Loading branch information
ryanomackey authored and chrisdhanaraj committed Aug 22, 2017
1 parent 973049b commit e4edb0c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/components/dropdown/_dropdown.scss
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@
}
}
}

.bx--dropdown--disabled {
opacity: .5;
cursor: not-allowed;
}
}
10 changes: 8 additions & 2 deletions src/components/dropdown/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,14 @@ class Dropdown extends mixin(createComponent, initComponentBySearch, trackBlur)
* @param {Event} [event] The event triggering this method.
*/
_toggle(event) {
if (([13, 32, 40].indexOf(event.which) >= 0 && !event.target.matches(this.options.selectorItem))
|| event.which === 27 || event.type === 'click') {
const isDisabled = this.element.classList.contains('bx--dropdown--disabled');

if (isDisabled) {
return;
}

if (([13, 32, 40].indexOf(event.which) >= 0 && !event.target.matches(this.options.selectorItem)) ||
event.which === 27 || event.type === 'click') {
const isOpen = this.element.classList.contains('bx--dropdown--open');
const isOfSelf = this.element.contains(event.target);
const actions = {
Expand Down
6 changes: 6 additions & 0 deletions tests/spec/dropdown_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ describe('Dropdown', function () {
expect(stubFocus, 'Focus requested').not.to.have.been.called;
});

it('Should not open when the disabled class is applied', function () {
element.classList.add('bx--dropdown--disabled');
element.dispatchEvent(new CustomEvent('click', { bubbles: true }));
expect(element.classList.contains('bx--dropdown--open'), 'Open state').to.be.false;
});

afterEach(function () {
if (stubFocus) {
stubFocus.restore();
Expand Down

0 comments on commit e4edb0c

Please sign in to comment.