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

Commit 1c7335b

Browse files
Matt Gooacdvorak
Matt Goo
authored andcommitted
feat(chips): ts conversion (#4332)
related #4225
1 parent 3279cc9 commit 1c7335b

File tree

13 files changed

+332
-450
lines changed

13 files changed

+332
-450
lines changed

packages/mdc-chips/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,12 @@ Method Signature | Description
279279

280280
Property | Value Type | Description
281281
--- | --- | ---
282-
`id` | string | Unique identifier on the chip\*
283-
`selected` | Boolean | Proxies to the foundation's `isSelected`/`setSelected` methods
284-
`shouldRemoveOnTrailingIconClick` | Boolean | Proxies to the foundation's `getShouldRemoveOnTrailingIconClick`/`setShouldRemoveOnTrailingIconClick` methods\*\*
285-
`ripple` | `MDCRipple` | The `MDCRipple` instance for the root element that `MDCChip` initializes
282+
`id` | `string` (read-only) | Unique identifier on the chip\*
283+
`selected` | `boolean` | Proxies to the foundation's `isSelected`/`setSelected` methods
284+
`shouldRemoveOnTrailingIconClick` | `boolean` | Proxies to the foundation's `getShouldRemoveOnTrailingIconClick`/`setShouldRemoveOnTrailingIconClick` methods\*\*
285+
`ripple` | `MDCRipple` (read-only) | The `MDCRipple` instance for the root element that `MDCChip` initializes
286286

287-
> \*_NOTE_: This will be the same as the `id` attribute on the root element. If an `id` is not provided, a unique one will be generated.
287+
> \*_NOTE_: This will be the same as the `id` attribute on the root element. If an `id` is not provided, a unique one will be generated by `MDCChipSet.addChip()`.
288288
289289
> \*\*_NOTE_: If `shouldRemoveOnTrailingIconClick` is set to false, you must manually call `beginExit()` on the chip to remove it.
290290
@@ -307,16 +307,16 @@ Method Signature | Description
307307

308308
Property | Value Type | Description
309309
--- | --- | ---
310-
`chips` | Array<`MDCChip`> | An array of the `MDCChip` objects that represent chips in the set
311-
`selectedChipIds` | `!Array<string>` (read-only) | An array of the IDs of all selected chips
310+
`chips` | `ReadonlyArray<MDCChip>` | An array of the `MDCChip` objects that represent chips in the set
311+
`selectedChipIds` | `ReadonlyArray<string>` | An array of the IDs of all selected chips
312312

313313
## Usage within Web Frameworks
314314

315315
If you are using a JavaScript framework, such as React or Angular, you can create Chips for your framework. Depending on your needs, you can use the _Simple Approach: Wrapping MDC Web Vanilla Components_, or the _Advanced Approach: Using Foundations and Adapters_. Please follow the instructions [here](../../docs/integrating-into-frameworks.md).
316316

317317
### Adapters: `MDCChipAdapter` and `MDCChipSetAdapter`
318318

319-
See [`chip/index.js`](chip/index.js) and [`chip-set/index.js`](chip-set/index.js) for vanilla DOM implementations of these adapter APIs for reference.
319+
See [`chip/index.ts`](chip/index.ts) and [`chip-set/index.ts`](chip-set/index.ts) for vanilla DOM implementations of these adapter APIs for reference.
320320

321321
#### `MDCChipAdapter`
322322

@@ -336,7 +336,7 @@ Method Signature | Description
336336
`setStyleProperty(propertyName: string, value: string) => void` | Sets the property value of the given style property on the root element
337337
`hasLeadingIcon() => boolean` | Returns whether the chip has a leading icon
338338
`getRootBoundingClientRect() => ClientRect` | Returns the bounding client rect of the root element
339-
`getCheckmarkBoundingClientRect() => ?ClientRect` | Returns the bounding client rect of the checkmark element or null if it doesn't exist
339+
`getCheckmarkBoundingClientRect() => ClientRect \| null` | Returns the bounding client rect of the checkmark element or null if it doesn't exist
340340

341341
> \*_NOTE_: `notifyInteraction` and `notifyTrailingIconInteraction` must pass along the target chip's ID, and must be observable by the parent `mdc-chip-set` element (e.g. via DOM event bubbling).
342342
@@ -382,7 +382,7 @@ Events | Element Selector | Foundation Handler
382382

383383
Method Signature | Description
384384
--- | ---
385-
`getSelectedChipIds() => !Array<string>` | Returns an array of the IDs of all selected chips
385+
`getSelectedChipIds() => ReadonlyArray<string>` | Returns an array of the IDs of all selected chips
386386
`select(chipId: string) => void` | Selects the chip with the given id
387387
`handleChipInteraction(chipId: string) => void` | Handles a custom `MDCChip:interaction` event on the root element
388388
`handleChipSelection(chipId: string, selected: boolean) => void` | Handles a custom `MDCChip:selection` event on the root element

packages/mdc-chips/chip-set/adapter.js renamed to packages/mdc-chips/chip-set/adapter.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,28 @@
2121
* THE SOFTWARE.
2222
*/
2323

24-
/* eslint no-unused-vars: [2, {"args": "none"}] */
25-
2624
/**
27-
* Adapter for MDC Chip Set.
28-
*
29-
* Defines the shape of the adapter expected by the foundation. Implement this
30-
* adapter to integrate the Chip Set into your framework. See
31-
* https://github.com/material-components/material-components-web/blob/master/docs/authoring-components.md
32-
* for more information.
33-
*
34-
* @record
25+
* Defines the shape of the adapter expected by the foundation.
26+
* Implement this adapter for your framework of choice to delegate updates to
27+
* the component in your framework of choice. See architecture documentation
28+
* for more details.
29+
* https://github.com/material-components/material-components-web/blob/master/docs/code/architecture.md
3530
*/
36-
class MDCChipSetAdapter {
31+
interface MDCChipSetAdapter {
3732
/**
38-
* Returns true if the root element contains the given class name.
39-
* @param {string} className
40-
* @return {boolean}
33+
* @return true if the root element contains the given class name.
4134
*/
42-
hasClass(className) {}
35+
hasClass(className: string): boolean;
4336

4437
/**
4538
* Removes the chip with the given id from the chip set.
46-
* @param {string} chipId
4739
*/
48-
removeChip(chipId) {}
40+
removeChip(chipId: string): void;
4941

5042
/**
5143
* Sets the selected state of the chip with the given id.
52-
* @param {string} chipId
53-
* @param {boolean} selected
5444
*/
55-
setSelected(chipId, selected) {}
45+
setSelected(chipId: string, selected: boolean): void;
5646
}
5747

58-
export default MDCChipSetAdapter;
48+
export {MDCChipSetAdapter as default, MDCChipSetAdapter};

packages/mdc-chips/chip-set/constants.js renamed to packages/mdc-chips/chip-set/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
* THE SOFTWARE.
2222
*/
2323

24-
/** @enum {string} */
2524
const strings = {
2625
CHIP_SELECTOR: '.mdc-chip',
2726
};
2827

29-
/** @enum {string} */
3028
const cssClasses = {
3129
CHOICE: 'mdc-chip-set--choice',
3230
FILTER: 'mdc-chip-set--filter',

packages/mdc-chips/chip-set/foundation.js renamed to packages/mdc-chips/chip-set/foundation.ts

Lines changed: 42 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -22,78 +22,46 @@
2222
*/
2323

2424
import {MDCFoundation} from '@material/base/foundation';
25-
import MDCChipSetAdapter from './adapter';
26-
// eslint-disable-next-line no-unused-vars
27-
import {MDCChipInteractionEventType, MDCChipSelectionEventType, MDCChipRemovalEventType} from '../chip/foundation';
28-
import {strings, cssClasses} from './constants';
25+
import {MDCChipSetAdapter} from './adapter';
26+
import {cssClasses, strings} from './constants';
2927

30-
/**
31-
* @extends {MDCFoundation<!MDCChipSetAdapter>}
32-
* @final
33-
*/
34-
class MDCChipSetFoundation extends MDCFoundation {
35-
/** @return enum {string} */
28+
class MDCChipSetFoundation extends MDCFoundation<MDCChipSetAdapter> {
3629
static get strings() {
3730
return strings;
3831
}
3932

40-
/** @return enum {string} */
4133
static get cssClasses() {
4234
return cssClasses;
4335
}
4436

45-
/**
46-
* {@see MDCChipSetAdapter} for typing information on parameters and return
47-
* types.
48-
* @return {!MDCChipSetAdapter}
49-
*/
50-
static get defaultAdapter() {
51-
return /** @type {!MDCChipSetAdapter} */ ({
52-
hasClass: () => {},
53-
removeChip: () => {},
54-
setSelected: () => {},
55-
});
37+
static get defaultAdapter(): MDCChipSetAdapter {
38+
return {
39+
hasClass: () => false,
40+
removeChip: () => undefined,
41+
setSelected: () => undefined,
42+
};
5643
}
5744

5845
/**
59-
* @param {!MDCChipSetAdapter} adapter
46+
* The ids of the selected chips in the set. Only used for choice chip set or filter chip set.
6047
*/
61-
constructor(adapter) {
62-
super(Object.assign(MDCChipSetFoundation.defaultAdapter, adapter));
48+
private selectedChipIds_: string[] = [];
6349

64-
/**
65-
* The ids of the selected chips in the set. Only used for choice chip set or filter chip set.
66-
* @private {!Array<string>}
67-
*/
68-
this.selectedChipIds_ = [];
50+
constructor(adapter: MDCChipSetAdapter) {
51+
super(Object.assign(MDCChipSetFoundation.defaultAdapter, adapter));
6952
}
7053

7154
/**
7255
* Returns an array of the IDs of all selected chips.
73-
* @return {!Array<string>}
7456
*/
75-
getSelectedChipIds() {
76-
return this.selectedChipIds_;
77-
}
78-
79-
/**
80-
* Toggles selection of the chip with the given id.
81-
* @private
82-
* @param {string} chipId
83-
*/
84-
toggleSelect_(chipId) {
85-
if (this.selectedChipIds_.indexOf(chipId) >= 0) {
86-
this.deselect_(chipId);
87-
} else {
88-
this.select(chipId);
89-
}
57+
getSelectedChipIds(): ReadonlyArray<string> {
58+
return this.selectedChipIds_.slice();
9059
}
9160

9261
/**
9362
* Selects the chip with the given id. Deselects all other chips if the chip set is of the choice variant.
94-
* @param {string} chipId
9563
*/
96-
select(chipId) {
64+
select(chipId: string) {
9765
if (this.selectedChipIds_.indexOf(chipId) >= 0) {
9866
return;
9967
}
@@ -107,35 +75,19 @@ class MDCChipSetFoundation extends MDCFoundation {
10775
this.adapter_.setSelected(chipId, true);
10876
}
10977

110-
/**
111-
* Deselects the chip with the given id.
112-
* @private
113-
* @param {string} chipId
114-
*/
115-
deselect_(chipId) {
116-
const index = this.selectedChipIds_.indexOf(chipId);
117-
if (index >= 0) {
118-
this.selectedChipIds_.splice(index, 1);
119-
this.adapter_.setSelected(chipId, false);
120-
}
121-
}
122-
12378
/**
12479
* Handles a chip interaction event
125-
* @param {string} chipId
12680
*/
127-
handleChipInteraction(chipId) {
81+
handleChipInteraction(chipId: string) {
12882
if (this.adapter_.hasClass(cssClasses.CHOICE) || this.adapter_.hasClass(cssClasses.FILTER)) {
12983
this.toggleSelect_(chipId);
13084
}
13185
}
13286

13387
/**
13488
* Handles a chip selection event, used to handle discrepancy when selection state is set directly on the Chip.
135-
* @param {string} chipId
136-
* @param {boolean} selected
13789
*/
138-
handleChipSelection(chipId, selected) {
90+
handleChipSelection(chipId: string, selected: boolean) {
13991
const chipIsSelected = this.selectedChipIds_.indexOf(chipId) >= 0;
14092
if (selected && !chipIsSelected) {
14193
this.select(chipId);
@@ -146,12 +98,33 @@ class MDCChipSetFoundation extends MDCFoundation {
14698

14799
/**
148100
* Handles the event when a chip is removed.
149-
* @param {string} chipId
150101
*/
151-
handleChipRemoval(chipId) {
102+
handleChipRemoval(chipId: string) {
152103
this.deselect_(chipId);
153104
this.adapter_.removeChip(chipId);
154105
}
106+
107+
/**
108+
* Deselects the chip with the given id.
109+
*/
110+
private deselect_(chipId: string) {
111+
const index = this.selectedChipIds_.indexOf(chipId);
112+
if (index >= 0) {
113+
this.selectedChipIds_.splice(index, 1);
114+
this.adapter_.setSelected(chipId, false);
115+
}
116+
}
117+
118+
/**
119+
* Toggles selection of the chip with the given id.
120+
*/
121+
private toggleSelect_(chipId: string) {
122+
if (this.selectedChipIds_.indexOf(chipId) >= 0) {
123+
this.deselect_(chipId);
124+
} else {
125+
this.select(chipId);
126+
}
127+
}
155128
}
156129

157-
export default MDCChipSetFoundation;
130+
export {MDCChipSetFoundation as default, MDCChipSetFoundation};

0 commit comments

Comments
 (0)