packages/basic-component-mixins/src/SingleSelectionMixin.js
function trackSelectedItem(element) {

  const items = element.items;
  const itemCount = items ? items.length : 0;

  const previousSelectedItem = element.selectedItem;
  if (!previousSelectedItem) {
    // No item was previously selected.
    if (element.selectionRequired) {
      // Select the first item by default.
      element.selectedIndex = 0;
    }
  } else if (itemCount === 0) {
    // We've lost the selection, and there's nothing left to select.
    element.selectedItem = null;
  } else {
    // Try to find the previously-selected item in the current set of items.
    const indexInCurrentItems = Array.prototype.indexOf.call(items, previousSelectedItem);
    const previousSelectedIndex = element.selectedIndex;
    if (indexInCurrentItems < 0) {
      // Previously-selected item was removed from the items.
      // Select the item at the same index (if it exists) or as close as possible.
      const newSelectedIndex = Math.min(previousSelectedIndex, itemCount - 1);
      // Select by item, since index may be the same, and we want to raise the
      // selected-item-changed event.
      element.selectedItem = items[newSelectedIndex];
    } else if (indexInCurrentItems !== previousSelectedIndex) {
      // Previously-selected item still there, but changed position.
      element.selectedIndex = indexInCurrentItems;
    }
  }
}
function trackSelectedItem(element) {

  const items = element.items;
  const itemCount = items ? items.length : 0;

  const previousSelectedItem = element.selectedItem;
  if (!previousSelectedItem) {
    // No item was previously selected.
    if (element.selectionRequired) {
      // Select the first item by default.
      element.selectedIndex = 0;
    }
  } else if (itemCount === 0) {
    // We've lost the selection, and there's nothing left to select.
    element.selectedItem = null;
  } else {
    // Try to find the previously-selected item in the current set of items.
    const indexInCurrentItems = Array.prototype.indexOf.call(items, previousSelectedItem);
    const previousSelectedIndex = element.selectedIndex;
    if (indexInCurrentItems < 0) {
      // Previously-selected item was removed from the items.
      // Select the item at the same index (if it exists) or as close as possible.
      const newSelectedIndex = Math.min(previousSelectedIndex, itemCount - 1);
      // Select by item, since index may be the same, and we want to raise the
      // selected-item-changed event.
      element.selectedItem = items[newSelectedIndex];
    } else if (indexInCurrentItems !== previousSelectedIndex) {
      // Previously-selected item still there, but changed position.
      element.selectedIndex = indexInCurrentItems;
    }
  }
}
function trackSelectedItem ( element ) { const items = element . items ; const itemCount = items ? items . length : 0 ; const previousSelectedItem = element . selectedItem ; if ( ! previousSelectedItem ) { // No item was previously selected. if ( element . selectionRequired ) { // Select the first item by default. element . selectedIndex = 0 ; } } else if ( itemCount === 0 ) { // We've lost the selection, and there's nothing left to select. element . selectedItem = null ; } else { // Try to find the previously-selected item in the current set of items. const indexInCurrentItems = Array . prototype . indexOf . call ( items , previousSelectedItem ) ; const previousSelectedIndex = element . selectedIndex ; if ( indexInCurrentItems < 0 ) { // Previously-selected item was removed from the items. // Select the item at the same index (if it exists) or as close as possible. const newSelectedIndex = Math . min ( previousSelectedIndex , itemCount - 1 ) ; // Select by item, since index may be the same, and we want to raise the // selected-item-changed event. element . selectedItem = items [ newSelectedIndex ] ; } else if ( indexInCurrentItems !== previousSelectedIndex ) { // Previously-selected item still there, but changed position. element . selectedIndex = indexInCurrentItems ; } } }
Following a change in the set of items, or in the value of the `selectionRequired` property, reacquire the selected item. If it's moved, update `selectedIndex`. If it's been removed, and a selection is required, try to select another item.
Following a change in the set of items or in the value of the selectionRequired property reacquire the selected item . If it s moved update selectedIndex . If it s been removed and a selection is required try to select another item .