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

Commit

Permalink
WIP: Return ReadonlyArray<T> instead of T[] from public APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
acdvorak committed Feb 7, 2019
1 parent 15b004f commit 17ee992
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
6 changes: 3 additions & 3 deletions packages/mdc-chips/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ Method Signature | Description

Property | Value Type | Description
--- | --- | ---
`chips` | `MDCChip[]` | An array of the `MDCChip` objects that represent chips in the set
`selectedChipIds` | `string[]` (read-only) | An array of the IDs of all selected chips
`chips` | `ReadonlyArray<MDCChip>` | An array of the `MDCChip` objects that represent chips in the set
`selectedChipIds` | `ReadonlyArray<string>` | An array of the IDs of all selected chips

## Usage within Web Frameworks

Expand Down Expand Up @@ -382,7 +382,7 @@ Events | Element Selector | Foundation Handler

Method Signature | Description
--- | ---
`getSelectedChipIds() => string[]` | Returns an array of the IDs of all selected chips
`getSelectedChipIds() => ReadonlyArray<string>` | Returns an array of the IDs of all selected chips
`select(chipId: string) => void` | Selects the chip with the given id
`handleChipInteraction(chipId: string) => void` | Handles a custom `MDCChip:interaction` event on the root element
`handleChipSelection(chipId: string, selected: boolean) => void` | Handles a custom `MDCChip:selection` event on the root element
Expand Down
4 changes: 2 additions & 2 deletions packages/mdc-chips/chip-set/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class MDCChipSetFoundation extends MDCFoundation<MDCChipSetAdapter> {
/**
* Returns an array of the IDs of all selected chips.
*/
getSelectedChipIds(): string[] {
return this.selectedChipIds_;
getSelectedChipIds(): ReadonlyArray<string> {
return this.selectedChipIds_.slice();
}

/**
Expand Down
26 changes: 15 additions & 11 deletions packages/mdc-chips/chip-set/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ class MDCChipSet extends MDCComponent<MDCChipSetFoundation> {
return new MDCChipSet(root);
}

chips!: MDCChip[];
private chips_!: MDCChip[]; // assigned in initialize()

get chips(): ReadonlyArray<MDCChip> {
return this.chips_.slice();
}

private chipFactory_!: (el: Element) => MDCChip;
private handleChipInteraction_!: (evt: MDCChipInteractionEvent) => void;
Expand All @@ -52,13 +56,13 @@ class MDCChipSet extends MDCComponent<MDCChipSetFoundation> {
/**
* @param chipFactory A function which creates a new MDCChip.
*/
initialize(chipFactory = (el: Element) => new MDCChip(el)) {
initialize(chipFactory: ChipFactory = (el) => new MDCChip(el)) {
this.chipFactory_ = chipFactory;
this.chips = this.instantiateChips_(this.chipFactory_);
this.chips_ = this.instantiateChips_(this.chipFactory_);
}

initialSyncWithDOM() {
this.chips.forEach((chip) => {
this.chips_.forEach((chip) => {
if (chip.id && chip.selected) {
this.foundation_.select(chip.id);
}
Expand All @@ -76,7 +80,7 @@ class MDCChipSet extends MDCComponent<MDCChipSetFoundation> {
}

destroy() {
this.chips.forEach((chip) => {
this.chips_.forEach((chip) => {
chip.destroy();
});

Expand All @@ -95,7 +99,7 @@ class MDCChipSet extends MDCComponent<MDCChipSetFoundation> {
*/
addChip(chipEl: Element) {
chipEl.id = chipEl.id || `mdc-chip-${++idCounter}`;
this.chips.push(this.chipFactory_(chipEl));
this.chips_.push(this.chipFactory_(chipEl));
}

getDefaultFoundation() {
Expand All @@ -104,14 +108,14 @@ class MDCChipSet extends MDCComponent<MDCChipSetFoundation> {
removeChip: (chipId) => {
const index = this.findChipIndex_(chipId);
if (index >= 0) {
this.chips[index].destroy();
this.chips.splice(index, 1);
this.chips_[index].destroy();
this.chips_.splice(index, 1);
}
},
setSelected: (chipId, selected) => {
const index = this.findChipIndex_(chipId);
if (index >= 0) {
this.chips[index].selected = selected;
this.chips_[index].selected = selected;
}
},
});
Expand All @@ -133,8 +137,8 @@ class MDCChipSet extends MDCComponent<MDCChipSetFoundation> {
* Returns the index of the chip with the given id, or -1 if the chip does not exist.
*/
findChipIndex_(chipId: string): number {
for (let i = 0; i < this.chips.length; i++) {
if (this.chips[i].id === chipId) {
for (let i = 0; i < this.chips_.length; i++) {
if (this.chips_[i].id === chipId) {
return i;
}
}
Expand Down
5 changes: 1 addition & 4 deletions packages/mdc-chips/chip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ class MDCChip extends MDCComponent<MDCChipFoundation> implements RippleCapableSu
this.leadingIcon_.classList.add(className);
}
},
eventTargetHasClass: (target, className) => {
if (!target) return false;
return (target as Element).classList.contains(className);
},
eventTargetHasClass: (target, className) => target ? (target as Element).classList.contains(className) : false,
getCheckmarkBoundingClientRect: () => this.checkmark_ ? this.checkmark_.getBoundingClientRect() : null,
getComputedStyleValue: (propertyName) => window.getComputedStyle(this.root_).getPropertyValue(propertyName),
getRootBoundingClientRect: () => this.root_.getBoundingClientRect(),
Expand Down

0 comments on commit 17ee992

Please sign in to comment.