Skip to content

Commit

Permalink
fix(list): removed the need of followHref
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiomkar committed Dec 7, 2018
1 parent 1d4f896 commit a7e8edd
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 29 deletions.
2 changes: 0 additions & 2 deletions packages/mdc-list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,13 +499,11 @@ Method Signature | Description
`getListItemCount() => Number` | Returns the total number of list items (elements with `mdc-list-item` class) that are direct children of the `root_` element.
`getFocusedElementIndex() => Number` | Returns the `index` value of the currently focused element.
`getListItemIndex(ele: Element) => Number` | Returns the `index` value of the provided `ele` element.
`getAttributeForElementIndex(index: number, attr: string) => string` | Gets the `attr` attribute value for the list item at `index`.
`setAttributeForElementIndex(index: Number, attr: String, value: String) => void` | Sets the `attr` attribute to `value` for the list item at `index`.
`addClassForElementIndex(index: Number, className: String) => void` | Adds the `className` class to the list item at `index`.
`removeClassForElementIndex(index: Number, className: String) => void` | Removes the `className` class to the list item at `index`.
`focusItemAtIndex(index: Number) => void` | Focuses the list item at the `index` value specified.
`setTabIndexForListItemChildren(index: Number, value: Number) => void` | Sets the `tabindex` attribute to `value` for each child button or anchor element in the list item at the `index` specified.
`followHref(element: Element) => void` | If the given element has an href, follows the link.
`hasRadioAtIndex(index: number) => boolean` | Returns true if radio button is present at given list item index.
`hasCheckboxAtIndex(index: number) => boolean` | Returns true if checkbox is present at given list item index.
`isCheckboxCheckedAtIndex(index: number) => boolean` | Returns true if checkbox inside a list item is checked.
Expand Down
12 changes: 0 additions & 12 deletions packages/mdc-list/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ class MDCListAdapter {
* @return {number} */
getFocusedElementIndex() {}

/**
* @param {number} index
* @param {string} attribute
*/
getAttributeForElementIndex(index, attr) {}

/**
* @param {number} index
* @param {string} attribute
Expand Down Expand Up @@ -89,12 +83,6 @@ class MDCListAdapter {
*/
setTabIndexForListItemChildren(listItemIndex, tabIndexValue) {}

/**
* If the given element has an href, follows the link.
* @param {!Element} ele
*/
followHref(ele) {}

/**
* @param {number} index
* @return {boolean} Returns true if radio button is present at given list item index.
Expand Down
12 changes: 4 additions & 8 deletions packages/mdc-list/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ class MDCListFoundation extends MDCFoundation {
return /** @type {!MDCListAdapter} */ ({
getListItemCount: () => {},
getFocusedElementIndex: () => {},
getAttributeForElementIndex: () => {},
setAttributeForElementIndex: () => {},
removeAttributeForElementIndex: () => {},
addClassForElementIndex: () => {},
removeClassForElementIndex: () => {},
focusItemAtIndex: () => {},
setTabIndexForListItemChildren: () => {},
followHref: () => {},
hasRadioAtIndex: () => {},
hasCheckboxAtIndex: () => {},
isCheckboxCheckedAtIndex: () => {},
Expand Down Expand Up @@ -251,6 +249,9 @@ class MDCListFoundation extends MDCFoundation {
this.focusLastElement();
} else if (isEnter || isSpace) {
if (isRootListItem) {
// Return early if enter key is pressed on anchor element which triggers synthetic MouseEvent event.
if (evt.target.tagName === 'A' && isEnter) return;

if (this.isSingleSelectionList_) {
// Check if the space key was pressed on the list item or a child element.
this.preventDefaultEvent_(evt);
Expand All @@ -266,12 +267,7 @@ class MDCListFoundation extends MDCFoundation {
this.setSelectedIndex(currentIndex);
}

// Explicitly activate links, since we're preventing default on Enter, and Space doesn't activate them.
if (this.adapter_.getAttributeForElementIndex(currentIndex, 'href')) {
this.adapter_.followHref(currentIndex);
} else {
this.adapter_.notifyAction(currentIndex);
}
this.adapter_.notifyAction(currentIndex);
}
}
}
Expand Down
7 changes: 0 additions & 7 deletions packages/mdc-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ class MDCList extends MDCComponent {
return new MDCListFoundation(/** @type {!MDCListAdapter} */ (Object.assign({
getListItemCount: () => this.listElements.length,
getFocusedElementIndex: () => this.listElements.indexOf(document.activeElement),
getAttributeForElementIndex: (index, attr) => this.listElements[index].getAttribute(attr),
setAttributeForElementIndex: (index, attr, value) => {
const element = this.listElements[index];
if (element) {
Expand Down Expand Up @@ -242,12 +241,6 @@ class MDCList extends MDCComponent {
const listItemChildren = [].slice.call(element.querySelectorAll(strings.CHILD_ELEMENTS_TO_TOGGLE_TABINDEX));
listItemChildren.forEach((ele) => ele.setAttribute('tabindex', tabIndexValue));
},
followHref: (index) => {
const listItem = this.listElements[index];
if (listItem && listItem.href) {
listItem.click();
}
},
hasCheckboxAtIndex: (index) => {
const listItem = this.listElements[index];
return !!listItem.querySelector(strings.CHECKBOX_SELECTOR);
Expand Down

0 comments on commit a7e8edd

Please sign in to comment.