Skip to content

Commit e4edb0c

Browse files
ryanomackeychrisdhanaraj
authored andcommitted
feat(Dropdown): add disabled state to dropdown component (#276)
* feat(Dropdown): add disabled state to dropdown component * refactor(Dropdown): simplify sass and disable cursor
1 parent 973049b commit e4edb0c

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/components/dropdown/_dropdown.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,9 @@
111111
}
112112
}
113113
}
114+
115+
.bx--dropdown--disabled {
116+
opacity: .5;
117+
cursor: not-allowed;
118+
}
114119
}

src/components/dropdown/dropdown.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,14 @@ class Dropdown extends mixin(createComponent, initComponentBySearch, trackBlur)
7474
* @param {Event} [event] The event triggering this method.
7575
*/
7676
_toggle(event) {
77-
if (([13, 32, 40].indexOf(event.which) >= 0 && !event.target.matches(this.options.selectorItem))
78-
|| event.which === 27 || event.type === 'click') {
77+
const isDisabled = this.element.classList.contains('bx--dropdown--disabled');
78+
79+
if (isDisabled) {
80+
return;
81+
}
82+
83+
if (([13, 32, 40].indexOf(event.which) >= 0 && !event.target.matches(this.options.selectorItem)) ||
84+
event.which === 27 || event.type === 'click') {
7985
const isOpen = this.element.classList.contains('bx--dropdown--open');
8086
const isOfSelf = this.element.contains(event.target);
8187
const actions = {

tests/spec/dropdown_spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ describe('Dropdown', function () {
138138
expect(stubFocus, 'Focus requested').not.to.have.been.called;
139139
});
140140

141+
it('Should not open when the disabled class is applied', function () {
142+
element.classList.add('bx--dropdown--disabled');
143+
element.dispatchEvent(new CustomEvent('click', { bubbles: true }));
144+
expect(element.classList.contains('bx--dropdown--open'), 'Open state').to.be.false;
145+
});
146+
141147
afterEach(function () {
142148
if (stubFocus) {
143149
stubFocus.restore();

0 commit comments

Comments
 (0)