Skip to content

Commit

Permalink
docs(checkbox): Add an example of a checkbox that can be disabled wit…
Browse files Browse the repository at this point in the history
…h javascript (#711)
  • Loading branch information
robzenn92 authored and amsheehan committed Jun 1, 2017
1 parent 736866b commit b27c6f0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions demos/checkbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ <h2>With Javascript</h2>
<label for="native-js-checkbox" id="my-checkbox-label">This is my checkbox</label>
</div>
<button type="button" id="make-ind">Make indeterminate</button>
<button type="button" id="toggle-disabled">Toggle Disabled</button>
</section>

<section class="example mdc-theme--dark">
Expand Down Expand Up @@ -201,6 +202,10 @@ <h2>Dark Theme</h2>
document.getElementById('make-ind').addEventListener('click', function() {
checkboxInstance.indeterminate = true;
});

document.getElementById('toggle-disabled').addEventListener('click', function() {
checkboxInstance.disabled = !checkboxInstance.disabled;
});
})(this);
</script>
</body>
Expand Down
1 change: 1 addition & 0 deletions packages/mdc-checkbox/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const cssClasses = {
UPGRADED: 'mdc-checkbox--upgraded',
CHECKED: 'mdc-checkbox--checked',
INDETERMINATE: 'mdc-checkbox--indeterminate',
DISABLED: 'mdc-checkbox--disabled',
ANIM_UNCHECKED_CHECKED: 'mdc-checkbox--anim-unchecked-checked',
ANIM_UNCHECKED_INDETERMINATE: 'mdc-checkbox--anim-unchecked-indeterminate',
ANIM_CHECKED_UNCHECKED: 'mdc-checkbox--anim-checked-unchecked',
Expand Down
5 changes: 5 additions & 0 deletions packages/mdc-checkbox/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ export default class MDCCheckboxFoundation extends MDCFoundation {

setDisabled(disabled) {
this.getNativeControl_().disabled = disabled;
if (disabled) {
this.adapter_.addClass(cssClasses.DISABLED);
} else {
this.adapter_.removeClass(cssClasses.DISABLED);
}
}

getValue() {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/mdc-checkbox/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@ test('#setDisabled updates the value of nativeControl.disabled', () => {
assert.isNotOk(nativeControl.disabled);
});

test('#setDisabled adds mdc-checkbox--disabled class to the root element when set to true', () => {
const {foundation, mockAdapter} = setupTest();
const nativeControl = {disabled: false};
td.when(mockAdapter.getNativeControl()).thenReturn(nativeControl);
foundation.setDisabled(true);
td.verify(mockAdapter.addClass(cssClasses.DISABLED));
});

test('#setDisabled removes mdc-checkbox--disabled class from the root element when set to false', () => {
const {foundation, mockAdapter} = setupTest();
const nativeControl = {disabled: true};
td.when(mockAdapter.getNativeControl()).thenReturn(nativeControl);
foundation.setDisabled(false);
td.verify(mockAdapter.removeClass(cssClasses.DISABLED));
});

test('#isDisabled returns false when no native control is returned', () => {
const {foundation, mockAdapter} = setupTest();
td.when(mockAdapter.getNativeControl()).thenReturn(null);
Expand Down

0 comments on commit b27c6f0

Please sign in to comment.