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

Commit 10a75f6

Browse files
authoredApr 3, 2018
refactor(chips): Manage chip foundations instead of chips in the chip set foundation (#2397)
BREAKING CHANGE: Expose a foundation getter in MDCChips
1 parent a767a0f commit 10a75f6

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed
 

‎packages/mdc-chips/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ To use the `MDCChip` and `MDCChipSet` classes, [import](../../docs/importing-js.
154154

155155
Method Signature | Description
156156
--- | ---
157+
`get foundation() => MDCChipFoundation` | Returns the foundation
157158
`toggleSelected() => void` | Proxies to the foundation's `toggleSelected` method
158159

159160
Property | Value Type | Description
@@ -182,8 +183,10 @@ Method Signature | Description
182183
`deregisterEventHandler(evtType: string, handler: EventListener) => void` | Deregisters an event listener on the root element
183184
`registerTrailingIconInteractionHandler(evtType: string, handler: EventListener) => void` | Registers an event listener on the trailing icon element
184185
`deregisterTrailingIconInteractionHandler(evtType: string, handler: EventListener) => void` | Deregisters an event listener on the trailing icon element
185-
`notifyInteraction() => void` | Emits a custom event `MDCChip:interaction` denoting the chip has been interacted with, which bubbles to the parent `mdc-chip-set` element
186-
`notifyTrailingIconInteraction() => void` | Emits a custom event `MDCChip:trailingIconInteraction` denoting the chip's trailing icon has been interacted with, which bubbles to the parent `mdc-chip-set` element
186+
`notifyInteraction() => void` | Emits a custom event `MDCChip:interaction` denoting the chip has been interacted with
187+
`notifyTrailingIconInteraction() => void` | Emits a custom event `MDCChip:trailingIconInteraction` denoting the chip's trailing icon has been interacted with
188+
189+
> _NOTE_: The custom events emitted by `notifyInteraction` and `notifyTrailingIconInteraction` must pass along the target chip in its event `detail`, as well as bubble to the parent `mdc-chip-set` element.
187190
188191
#### `MDCChipSetAdapter`
189192

‎packages/mdc-chips/chip-set/foundation.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
import MDCFoundation from '@material/base/foundation';
1919
import MDCChipSetAdapter from './adapter';
20-
// eslint-disable-next-line no-unused-vars
21-
import {MDCChip, MDCChipFoundation} from '../chip/index';
20+
import MDCChipFoundation from '../chip/foundation';
2221
import {strings, cssClasses} from './constants';
2322

2423
/**
@@ -57,7 +56,7 @@ class MDCChipSetFoundation extends MDCFoundation {
5756

5857
/**
5958
* The selected chips in the set. Only used for choice chip set or filter chip set.
60-
* @private {!Array<!MDCChip>}
59+
* @private {!Array<!MDCChipFoundation>}
6160
*/
6261
this.selectedChips_ = [];
6362

@@ -81,25 +80,25 @@ class MDCChipSetFoundation extends MDCFoundation {
8180
* @private
8281
*/
8382
handleChipInteraction_(evt) {
84-
const {chip} = evt.detail;
83+
const chipFoundation = evt.detail.chip.foundation;
8584
if (this.adapter_.hasClass(cssClasses.CHOICE)) {
8685
if (this.selectedChips_.length === 0) {
87-
this.selectedChips_[0] = chip;
88-
} else if (this.selectedChips_[0] !== chip) {
86+
this.selectedChips_[0] = chipFoundation;
87+
} else if (this.selectedChips_[0] !== chipFoundation) {
8988
this.selectedChips_[0].toggleSelected();
90-
this.selectedChips_[0] = chip;
89+
this.selectedChips_[0] = chipFoundation;
9190
} else {
9291
this.selectedChips_ = [];
9392
}
94-
chip.toggleSelected();
93+
chipFoundation.toggleSelected();
9594
} else if (this.adapter_.hasClass(cssClasses.FILTER)) {
96-
const index = this.selectedChips_.indexOf(chip);
95+
const index = this.selectedChips_.indexOf(chipFoundation);
9796
if (index >= 0) {
9897
this.selectedChips_.splice(index, 1);
9998
} else {
100-
this.selectedChips_.push(chip);
99+
this.selectedChips_.push(chipFoundation);
101100
}
102-
chip.toggleSelected();
101+
chipFoundation.toggleSelected();
103102
}
104103
}
105104
}

‎packages/mdc-chips/chip/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ class MDCChip extends MDCComponent {
5959
this.foundation_.toggleSelected();
6060
}
6161

62+
/**
63+
* @return {!MDCChipFoundation}
64+
*/
65+
get foundation() {
66+
return this.foundation_;
67+
}
68+
6269
/**
6370
* @return {!MDCChipFoundation}
6471
*/

‎test/unit/mdc-chips/mdc-chip-set.foundation.test.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ const setupTest = () => {
4242
const mockAdapter = td.object(MDCChipSetFoundation.defaultAdapter);
4343
const foundation = new MDCChipSetFoundation(mockAdapter);
4444
const chipA = td.object({
45-
toggleSelected: () => {},
45+
foundation: {
46+
toggleSelected: () => {},
47+
},
4648
});
4749
const chipB = td.object({
48-
toggleSelected: () => {},
50+
foundation: {
51+
toggleSelected: () => {},
52+
},
4953
});
5054
return {foundation, mockAdapter, chipA, chipB};
5155
};
@@ -81,24 +85,24 @@ test('on custom MDCChip:interaction event toggles selected state with single sel
8185
chip: chipA,
8286
},
8387
});
84-
td.verify(chipA.toggleSelected());
88+
td.verify(chipA.foundation.toggleSelected());
8589
assert.equal(foundation.selectedChips_.length, 1);
8690

8791
chipInteractionHandler({
8892
detail: {
8993
chip: chipB,
9094
},
9195
});
92-
td.verify(chipA.toggleSelected());
93-
td.verify(chipB.toggleSelected());
96+
td.verify(chipA.foundation.toggleSelected());
97+
td.verify(chipB.foundation.toggleSelected());
9498
assert.equal(foundation.selectedChips_.length, 1);
9599

96100
chipInteractionHandler({
97101
detail: {
98102
chip: chipB,
99103
},
100104
});
101-
td.verify(chipB.toggleSelected());
105+
td.verify(chipB.foundation.toggleSelected());
102106
assert.equal(foundation.selectedChips_.length, 0);
103107
});
104108

@@ -119,30 +123,30 @@ test('on custom MDCChip:interaction event toggles selected state with multi-sele
119123
chip: chipA,
120124
},
121125
});
122-
td.verify(chipA.toggleSelected());
126+
td.verify(chipA.foundation.toggleSelected());
123127
assert.equal(foundation.selectedChips_.length, 1);
124128

125129
chipInteractionHandler({
126130
detail: {
127131
chip: chipB,
128132
},
129133
});
130-
td.verify(chipB.toggleSelected());
134+
td.verify(chipB.foundation.toggleSelected());
131135
assert.equal(foundation.selectedChips_.length, 2);
132136

133137
chipInteractionHandler({
134138
detail: {
135139
chip: chipB,
136140
},
137141
});
138-
td.verify(chipB.toggleSelected());
142+
td.verify(chipB.foundation.toggleSelected());
139143
assert.equal(foundation.selectedChips_.length, 1);
140144

141145
chipInteractionHandler({
142146
detail: {
143147
chip: chipA,
144148
},
145149
});
146-
td.verify(chipA.toggleSelected());
150+
td.verify(chipA.foundation.toggleSelected());
147151
assert.equal(foundation.selectedChips_.length, 0);
148152
});

0 commit comments

Comments
 (0)
This repository has been archived.