WICG / virtual-scroller

Other
2k stars 83 forks source link

Api to get the item for a element and vice versa #143

Closed jogibear9988 closed 5 years ago

jogibear9988 commented 5 years ago

Is there a API to get the underlying item for an element? And one to get the Element for and underlying item?

I've needed this several times when I used iron-list

valdrinkoshi commented 5 years ago

What would be the use case? If you save the item on each child (e.g. as we do for <contact-element>), it should be as simple as:

scroller.updateElement = (elem, item) => elem.item = item;
const elementForItem = (item) => scroller.children.find(elem => elem.item === item);
const itemForElement = (elem) => elem.item;
jogibear9988 commented 5 years ago

In iron-grid I needed it to the get selected Elemnt from the selected item to focus an inner child on selection change.

valdrinkoshi commented 5 years ago

@jogibear9988 interesting, can you provide more details? E.g. do you want to focus only when receiving the data, or programmatically trigger it when the user clicks on the element?

You could have a container-like custom element that forwards focus to the focusable child when selected. You can as well configure its shadowRoot with delegatesFocus: true, which basically makes the element focusable and forwards the focus to the first focusable child if any. In this case you wouldn't need a selected property, but just call focus() on the container custom element, or just set the autofocus attribute on it

domenic commented 5 years ago

Closing this as this is related to the old virtual-scroller concept that uses ItemSource. The new approach outlined in the explainer no longer uses this concept.