Skip to content

Commit

Permalink
WIP: Change MDCList.listElements from HTMLElement[] back to `Elem…
Browse files Browse the repository at this point in the history
…ent[]`
  • Loading branch information
acdvorak committed Feb 11, 2019
1 parent 7a7d960 commit 91f5975
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
17 changes: 8 additions & 9 deletions packages/mdc-list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class MDCList extends MDCComponent<MDCListFoundation> {
this.foundation_.setVerticalOrientation(value);
}

get listElements(): HTMLElement[] {
return [].slice.call(this.root_.querySelectorAll<HTMLElement>(strings.ENABLED_ITEMS_SELECTOR));
get listElements(): Element[] {
return [].slice.call(this.root_.querySelectorAll(strings.ENABLED_ITEMS_SELECTOR));
}

set wrapFocus(value: boolean) {
Expand Down Expand Up @@ -104,16 +104,16 @@ class MDCList extends MDCComponent<MDCListFoundation> {
*/
initializeListType() {
const checkboxListItems = this.root_.querySelectorAll(strings.ARIA_ROLE_CHECKBOX_SELECTOR);
const singleSelectedListItem = this.root_.querySelector<HTMLElement>(`
const singleSelectedListItem = this.root_.querySelector(`
.${cssClasses.LIST_ITEM_ACTIVATED_CLASS},
.${cssClasses.LIST_ITEM_SELECTED_CLASS}
`);
const radioSelectedListItem = this.root_.querySelector<HTMLElement>(strings.ARIA_CHECKED_RADIO_SELECTOR);
const radioSelectedListItem = this.root_.querySelector(strings.ARIA_CHECKED_RADIO_SELECTOR);

if (checkboxListItems.length) {
const preselectedItems = this.root_.querySelectorAll(strings.ARIA_CHECKED_CHECKBOX_SELECTOR);
this.selectedIndex =
[].map.call(preselectedItems, (listItem: HTMLElement) => this.listElements.indexOf(listItem)) as number[];
[].map.call(preselectedItems, (listItem: Element) => this.listElements.indexOf(listItem)) as number[];
} else if (singleSelectedListItem) {
if (singleSelectedListItem.classList.contains(cssClasses.LIST_ITEM_ACTIVATED_CLASS)) {
this.foundation_.setUseActivatedClass(true);
Expand All @@ -135,12 +135,12 @@ class MDCList extends MDCComponent<MDCListFoundation> {
}
},
focusItemAtIndex: (index) => {
const element = this.listElements[index] as HTMLElement;
const element = this.listElements[index] as HTMLElement | null;
if (element) {
element.focus();
}
},
getFocusedElementIndex: () => this.listElements.indexOf(document.activeElement as HTMLElement),
getFocusedElementIndex: () => this.listElements.indexOf(document.activeElement!),
getListItemCount: () => this.listElements.length,
hasCheckboxAtIndex: (index) => {
const listItem = this.listElements[index];
Expand Down Expand Up @@ -203,8 +203,7 @@ class MDCList extends MDCComponent<MDCListFoundation> {
*/
private getListItemIndex_(evt: Event) {
const eventTarget = evt.target as Element;
const nearestParent =
ponyfill.closest<HTMLElement>(eventTarget, `.${cssClasses.LIST_ITEM_CLASS}, .${cssClasses.ROOT}`);
const nearestParent = ponyfill.closest(eventTarget, `.${cssClasses.LIST_ITEM_CLASS}, .${cssClasses.ROOT}`);

// Get the index of the element if it is a list item.
if (nearestParent && ponyfill.matches(nearestParent, `.${cssClasses.LIST_ITEM_CLASS}`)) {
Expand Down
6 changes: 3 additions & 3 deletions packages/mdc-menu/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class MDCMenu extends MDCComponent<MDCMenuFoundation> {
* the items container that are proper list items, and not supplemental / presentational DOM
* elements.
*/
get items(): HTMLElement[] {
get items(): Element[] {
return this.list_ ? this.list_.listElements : [];
}

Expand Down Expand Up @@ -191,10 +191,10 @@ class MDCMenu extends MDCComponent<MDCMenuFoundation> {
},
elementContainsClass: (element, className) => element.classList.contains(className),
closeSurface: () => this.open = false,
getElementIndex: (element) => this.items.indexOf(element as HTMLElement),
getElementIndex: (element) => this.items.indexOf(element),
getParentElement: (element) => element.parentElement,
getSelectedElementIndex: (selectionGroup) => {
const selectedListItem = selectionGroup.querySelector<HTMLElement>(`.${cssClasses.MENU_SELECTED_LIST_ITEM}`);
const selectedListItem = selectionGroup.querySelector(`.${cssClasses.MENU_SELECTED_LIST_ITEM}`);
return selectedListItem ? this.items.indexOf(selectedListItem) : -1;
},
notifySelected: (evtData) => this.emit<DefaultMenuItemEventDetail>(strings.SELECTED_EVENT, {
Expand Down
8 changes: 4 additions & 4 deletions packages/mdc-select/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class MDCSelect extends MDCComponent<MDCSelectFoundation> implements RippleCapab
get selectedIndex(): number {
let selectedIndex = -1;
if (this.menuElement_ && this.menu_) {
const selectedEl = this.menuElement_.querySelector<HTMLElement>(strings.SELECTED_ITEM_SELECTOR)!;
const selectedEl = this.menuElement_.querySelector(strings.SELECTED_ITEM_SELECTOR)!;
selectedIndex = this.menu_.items.indexOf(selectedEl);
} else if (this.nativeControl_) {
selectedIndex = this.nativeControl_.selectedIndex;
Expand Down Expand Up @@ -245,7 +245,8 @@ class MDCSelect extends MDCComponent<MDCSelectFoundation> implements RippleCapab
this.handleMenuOpened_ = () => {
// Menu should open to the last selected element.
if (this.selectedIndex >= 0) {
this.menu_!.items[this.selectedIndex].focus();
const selectedItemEl = this.menu_!.items[this.selectedIndex] as HTMLElement;
selectedItemEl.focus();
}
};
this.handleMenuClosed_ = () => {
Expand Down Expand Up @@ -413,8 +414,7 @@ class MDCSelect extends MDCComponent<MDCSelectFoundation> implements RippleCapab
return '';
},
setValue: (value: string) => {
const element =
this.menuElement_!.querySelector<HTMLElement>(`[${strings.ENHANCED_VALUE_ATTR}="${value}"]`);
const element = this.menuElement_!.querySelector(`[${strings.ENHANCED_VALUE_ATTR}="${value}"]`);
this.setEnhancedSelectedIndex_(element ? this.menu_!.items.indexOf(element) : -1);
},
openMenu: () => {
Expand Down

0 comments on commit 91f5975

Please sign in to comment.