Skip to content

Commit

Permalink
fix(chips): MdcChip, emit input event when selection state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcrimmins committed Mar 14, 2024
1 parent ee5c22e commit 8803ef7
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/components/mdc-chips/mdc-chip-set.ts
Expand Up @@ -138,13 +138,37 @@ export default Vue.extend({
const {detail} = event;

this.mdcFoundation.handleChipSelection(detail);
// this.$emit('input', detail.);

// Deep clone the value to avoid mutating the prop value itself.
const newValue = <number[]>JSON.parse(JSON.stringify(this.value));
const selectedChipIndex = this.getIndexOfChipById(detail.chipId);

if (selectedChipIndex !== -1) {
newValue.splice(
selectedChipIndex,
newValue.includes(selectedChipIndex) ? 1 : 0
);
}

this.$emit('input', newValue);
},

onRemoval(event: MDCChipRemovalEvent) {
this.mdcFoundation.handleChipRemoval(event.detail);
},

//
// Private/adapter methods
//

getIndexOfChipById(chipId: string) {
for (let x = 0; x < this.chipList.length; x++) {
if (this.chipList[x].id === chipId) return x;
}

return -1;
},

//
// Adapter methods
//
Expand All @@ -165,14 +189,6 @@ export default Vue.extend({
return this.chipList.length;
},

getIndexOfChipById(chipId: string) {
for (let x = 0; x < this.chipList.length; x++) {
if (this.chipList[x].id === chipId) return x;
}

return -1;
},

hasClass(className: string) {
return this.$el.classList.contains(className);
},
Expand Down

0 comments on commit 8803ef7

Please sign in to comment.