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

Commit eb00fd3

Browse files
authored
feat(chips): Expose method to begin chip exit animation (#2845)
1 parent 5f4454a commit eb00fd3

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

demos/chips.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ <h2>Input Chips</h2>
6363
<button id="input-chip-set-button" class="mdc-button mdc-button--dense">
6464
Add Input Chip
6565
</button>
66+
<button id="input-chip-set-delete-button" class="mdc-button mdc-button--dense">
67+
Delete Last Chip
68+
</button>
6669
<div id="input-chip-set" class="mdc-chip-set mdc-chip-set--input">
6770
<div class="demo-chip mdc-chip" tabindex="0">
6871
<i class="material-icons mdc-chip__icon mdc-chip__icon--leading">face</i>
@@ -239,6 +242,7 @@ <h2>Custom theme</h2>
239242
var chipSets = document.querySelectorAll('.mdc-chip-set:not(#input-chip-set)');
240243
var input = document.getElementById('input-chip-set-input');
241244
var inputButton = document.getElementById('input-chip-set-button');
245+
var deleteButton = document.getElementById('input-chip-set-delete-button');
242246

243247
[].forEach.call(chipSets, function(chipSet) {
244248
mdc.chips.MDCChipSet.attachTo(chipSet);
@@ -281,8 +285,15 @@ <h2>Custom theme</h2>
281285
root && inputChipSetEl.removeChild(root);
282286
}
283287

288+
function deleteLastChip() {
289+
const lastChipIndex = inputChipSetComponent.chips.length - 1;
290+
const lastChip = inputChipSetComponent.chips[lastChipIndex];
291+
lastChip.beginExit();
292+
};
293+
284294
inputButton.addEventListener('click', addInputChip);
285295
input.addEventListener('keydown', addInputChip);
296+
deleteButton.addEventListener('click', deleteLastChip);
286297
inputChipSetEl.addEventListener('MDCChip:removal', removeChip);
287298
});
288299
</script>

packages/mdc-chips/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ Method Signature | Description
213213
--- | ---
214214
`get foundation() => MDCChipFoundation` | Returns the foundation
215215
`isSelected() => boolean` | Proxies to the foundation's `isSelected` method
216+
`beginExit() => void` | Begins the exit animation which leads to removal of the chip
216217

217218
Property | Value Type | Description
218219
--- | --- | ---

packages/mdc-chips/chip/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {MDCRipple, MDCRippleFoundation} from '@material/ripple/index';
2020

2121
import MDCChipAdapter from './adapter';
2222
import MDCChipFoundation from './foundation';
23-
import {strings} from './constants';
23+
import {strings, cssClasses} from './constants';
2424

2525
/**
2626
* @extends {MDCComponent<!MDCChipFoundation>}
@@ -82,6 +82,13 @@ class MDCChip extends MDCComponent {
8282
return this.foundation_.isSelected();
8383
}
8484

85+
/**
86+
* Begins the exit animation which leads to removal of the chip.
87+
*/
88+
beginExit() {
89+
this.root_.classList.add(cssClasses.CHIP_EXIT);
90+
}
91+
8592
/**
8693
* @return {!MDCChipFoundation}
8794
*/

test/unit/mdc-chips/mdc-chip.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,9 @@ test('#isSelected proxies to foundation', () => {
202202
component.isSelected();
203203
td.verify(mockFoundation.isSelected());
204204
});
205+
206+
test(`#beginExit adds ${MDCChipFoundation.cssClasses.CHIP_EXIT} class`, () => {
207+
const {component, root} = setupMockFoundationTest();
208+
component.beginExit();
209+
assert.isTrue(root.classList.contains(MDCChipFoundation.cssClasses.CHIP_EXIT));
210+
});

0 commit comments

Comments
 (0)