Using VirtualList, VirtualGridList and Scroller
This document describes VirtualList, VirtualGridList, and Scroller.
VirtualList/VirtualGridList
Section titled “VirtualList/VirtualGridList”Basic usage of VirtualList and VirtualGridList
Section titled “Basic usage of VirtualList and VirtualGridList”-
At least three props below are required to show a list properly.
-
dataSize: Size of the data. -
itemSize: Size of an item for the list. This is a required prop, and you will get an error when you build an app in dev mode without it. -
itemRenderer: The render function for an item of the list. -
Example
<VirtualListdataSize={this.items.length}itemRenderer={this.renderItem}itemSize={ri.scale(144)}/><VirtualGridListdataSize={this.items.length}itemRenderer={this.renderItem}itemSize={{minWidth: ri.scale(180), minHeight: ri.scale(270)}}/>
-
-
If you want to provide spacing or other numeric properties, you have to specify them surrounded by braces, not by quotes.
<VirtualListdataSize={this.items.length} //<-- numeric propertyitemRenderer={this.renderItem}itemSize={ri.scale(144)} //<-- numeric propertyspacing={ri.scale(20)} //<-- numeric property/>
Common rules of Items for VirtualList/VirtualGridList
Section titled “Common rules of Items for VirtualList/VirtualGridList”-
A renderer for an item should be specified in
itemRendererprop in VirtualList. -
VirtualList passes
index,data-index, andchildPropsto theitemRendererfunction. -
Be sure you are passing
{...rest}to the item component for getting focus properly. -
Make sure you are not using an inline function for
itemRenderer. -
If you want to scroll the list via 5-way navigation on the certain component in an item, you should pass
data-indexprop. -
Example:
items = []...renderItem = ({index, ...rest}) => {return (<div {...rest}>{this.items[index].name}</div>);}...render = () => {return (<VirtualListdataSize={this.items.length}itemRenderer={this.renderItem}itemSize={this.itemSize}/>);} -
If you create a custom item, make sure you are passing props from parents (render function) to the child.
MyListItem.js const MyListItem = kind({.....render = (props) => { //<-- should pass propsreturn (<div {...props}>...</div>);}});//app.jsrenderItem = ({index, ...rest}) => {return (<MyListItem index={index} {...rest} />);}
Items for VirtualGridList
Section titled “Items for VirtualGridList”-
ImageItemcomponents can be used as items ofVirtualGridList. They have Limestone styling applied and have a placeholder image as a background. -
captionandsubCaptionare supported. -
For showing Limestone’s selection overlay, set
selectionOverlayShowingtotrue. (default isfalse) -
selectedprop shows item’s status of selection. -
sourceprop should be set to URL path. (e.g.source="http://XXXX/image.png",source="../assets/image.png") -
Example:
renderItem = ({index, ...rest}) => {const {text, subText, source} = this.items[index];return (<ImageItem{...rest}caption={text}className={css.item}source={source}subCaption={subText}/>);};
Scroller
Section titled “Scroller”Basic usage of Scroller
Section titled “Basic usage of Scroller”-
Make sure you specify width and height of Scroller.
-
You can specify the scrollable direction with
directionprops. Valid values areboth,horizontal, andvertical. -
Example:
.scroller {height: 1100px;width: 960px;}<ScrollerclassName={css.scroller}direction="both"><div className={css.content}>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br /></div></Scroller>
Using scrollTo() method in VirtualList/VirtualGridList and Scroller
Section titled “Using scrollTo() method in VirtualList/VirtualGridList and Scroller”-
Prop
cbScrollTois a callback function.VirtualList,VirtualGridList, andScrollerprovidecbScrollToprop which can be set to a callback function that will receive thescrollTo()method.- The callback function is called just once when a list or a scroller is created to prevent repeated calls when a list or a scroller is updated.
- Do not change
cbScrollToprop after a list or a scroller is initially rendered. It does not have any effect.
-
The binding of a callback function
- Please make sure the context of a callback function is properly bound. In general, your app component instance would be the right one.
- We recommend to use ECMAScript 2015 (a.k.a. ECMAScript 6 or ES6) arrow functions to handle binding.
-
Example:
class SampleApp extends React.Component {constructor (props) {super(props);this.y = 0;}...getScrollTo = (scrollTo) => { // callback function to get scrollTo method; arrow function (ES6) is recommended to make sure `this` binding.this.scrollTo = scrollTo;}...doSomething = () => {...this.scrollTo({position: {y: this.y + offset}, animate: true}); // call the function of SampleApp, not a list.}...render = () => {return (<VirtualListcbScrollTo={this.getScrollTo} // pass callback functiondataSize={this.items.length}itemRenderer={this.renderItem}itemSize={this.itemSize}/>);}} -
Example:
this.scrollTo({position: {y: 100}, animate: false}); // scroll y position to 100px without animationthis.scrollTo({position: {x: 100, y: 200}); // scroll to (100px, 200px) position; animation is enabled if omittedthis.scrollTo({align: 'bottom'}); // scroll to the bottomthis.scrollTo({align: 'lefttop'}); // scroll to the left top position; identical to {position: {x: 0, y:0}}this.scrollTo({index: 20}); // VirtualList/VirtualGridList only; scroll to the 21st item; index is counting from 0this.scrollTo({index: 20, focus: true}); // VirtualList/VirtualGridList only; scroll to the 21st item and focus on the itemthis.scrollTo({node: childNode}); // Scroller only; scroll to the child node.this.scrollTo({node: childNode, focus: true}); // Scroller only; scroll to the child node and focus on the node.
Event Callbacks for VirtualList/VirtualGridList and Scroller
Section titled “Event Callbacks for VirtualList/VirtualGridList and Scroller”-
You can specify callback functions for scroll events.
-
When you scroll on a list or a scroller,
onScrollStart,onScroll, andonScrollStopevents fire. -
Each event sends an object with
scrollLeft,scrollTop, andmoreInfoproperties in it. -
For VirtualList/VirtualGridList,
moreInfohasfirstVisibleIndexandlastVisibleIndex. -
It is recommended not to call
setState()inonScrollevent callback. -
Example:
app.js ...handlerOnScrollStart = () => {this.setState({message: 'startScroll'});}handlerOnScroll = ({scrollTop}) => {this.y = scrollTop;}handlerOnScrollStop = ({scrollTop}) => {this.y = scrollTop;this.setState({message: 'scrollStopped'});}...render = () => {return (<VirtualList...onScrollStart={this.handlerOnScrollStart}onScroll={this.handlerOnScroll}onScrollStop={this.handlerOnScrollStop}/>);}