Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

feat(checkbox): Toggle selected class with state #4612

Merged
merged 4 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/mdc-checkbox/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const cssClasses = {
CHECKED: 'mdc-checkbox--checked',
DISABLED: 'mdc-checkbox--disabled',
INDETERMINATE: 'mdc-checkbox--indeterminate',
SELECTED: 'mdc-checkbox--selected',
UPGRADED: 'mdc-checkbox--upgraded',
};

Expand Down
10 changes: 10 additions & 0 deletions packages/mdc-checkbox/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ export class MDCCheckboxFoundation extends MDCFoundation<MDCCheckboxAdapter> {

this.updateAriaChecked_();

const {TRANSITION_STATE_UNCHECKED} = strings;
const {SELECTED} = cssClasses;
if (oldState === TRANSITION_STATE_UNCHECKED) {
patrickrodee marked this conversation as resolved.
Show resolved Hide resolved
this.adapter_.addClass(SELECTED);
}

if (newState === TRANSITION_STATE_UNCHECKED) {
this.adapter_.removeClass(SELECTED);
}

// Check to ensure that there isn't a previously existing animation class, in case for example
// the user interacted with the checkbox before the animation was finished.
if (this.currentAnimationClass_.length > 0) {
Expand Down
52 changes: 52 additions & 0 deletions test/unit/mdc-checkbox/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,58 @@ testChangeHandler('no transition classes applied when no state change', [
},
], cssClasses.ANIM_UNCHECKED_CHECKED, {times: 1});

test('changing from unchecked to checked adds selected class', () => {
const {mockAdapter, change} = setupChangeHandlerTest();
change({
checked: false,
indeterminate: false,
});
change({
checked: true,
indeterminate: false,
});
td.verify(mockAdapter.addClass(cssClasses.SELECTED), {times: 1});
});

test('changing from unchecked to indeterminate adds selected class', () => {
const {mockAdapter, change} = setupChangeHandlerTest();
change({
checked: false,
indeterminate: false,
});
change({
checked: false,
indeterminate: true,
});
td.verify(mockAdapter.addClass(cssClasses.SELECTED), {times: 1});
});

test('changing from checked to unchecked removes selected class', () => {
const {mockAdapter, change} = setupChangeHandlerTest();
change({
checked: true,
indeterminate: false,
});
change({
checked: false,
indeterminate: false,
});
td.verify(mockAdapter.removeClass(cssClasses.SELECTED), {times: 1});
});

test('changing from indeterminate to unchecked removes selected class', () => {
const {mockAdapter, change} = setupChangeHandlerTest();
change({
checked: false,
indeterminate: true,
});
change({
checked: false,
indeterminate: false,
});
td.verify(mockAdapter.removeClass(cssClasses.SELECTED), {times: 1});
});

test('animation end handler removes animation class after short delay', () => {
const clock = installClock();
const {ANIM_UNCHECKED_CHECKED} = cssClasses;
Expand Down