Only render first 3 categories on first render

release
Etienne Lemay 2016-07-15 12:24:50 -04:00
parent ce3ab53400
commit 9eb8f0b6ac
2 changed files with 44 additions and 14 deletions

View File

@ -11,6 +11,7 @@ class Example extends React.Component {
perLine: 9, perLine: 9,
skin: 1, skin: 1,
set: 'apple', set: 'apple',
hidden: false,
} }
} }
@ -43,6 +44,12 @@ class Example extends React.Component {
})} })}
</div> </div>
<div>
<button
onClick={() => this.setState({ hidden: !this.state.hidden })}
>{this.state.hidden ? 'Mount' : 'Unmount'}</button>
</div>
<pre style={{ <pre style={{
fontSize: 18, fontSize: 18,
display: 'inline-block', display: 'inline-block',
@ -61,13 +68,15 @@ class Example extends React.Component {
<br /><Operator>/&gt;</Operator> <br /><Operator>/&gt;</Operator>
</pre> </pre>
<Picker {!this.state.hidden &&
emojiSize={this.state.emojiSize} <Picker
perLine={this.state.perLine} emojiSize={this.state.emojiSize}
skin={this.state.skin} perLine={this.state.perLine}
sheetURL={`../sheets/sheet_${this.state.set}_64.png`} skin={this.state.skin}
onClick={(emoji) => console.log(emoji)} sheetURL={`../sheets/sheet_${this.state.set}_64.png`}
/> onClick={(emoji) => console.log(emoji)}
/>
}
</div> </div>
} }
} }

View File

@ -21,6 +21,7 @@ export default class Picker extends React.Component {
this.state = { this.state = {
skin: store.get('skin') || props.skin, skin: store.get('skin') || props.skin,
firstRender: true,
} }
} }
@ -30,6 +31,15 @@ export default class Picker extends React.Component {
} }
} }
componentDidMount() {
if (this.state.firstRender) {
setTimeout(() => {
if (!this.isMounted) return
this.setState({ firstRender: false })
}, 60)
}
}
componentDidUpdate() { componentDidUpdate() {
this.updateCategoriesSize() this.updateCategoriesSize()
this.handleScroll() this.handleScroll()
@ -86,6 +96,10 @@ export default class Picker extends React.Component {
handleScrollPaint() { handleScrollPaint() {
this.waitingForPaint = false this.waitingForPaint = false
if (!this.refs.scroll) {
return
}
var target = this.refs.scroll, var target = this.refs.scroll,
scrollTop = target.scrollTop, scrollTop = target.scrollTop,
scrollingDown = scrollTop > (this.scrollTop || 0), scrollingDown = scrollTop > (this.scrollTop || 0),
@ -97,14 +111,15 @@ export default class Picker extends React.Component {
category = CATEGORIES[ii], category = CATEGORIES[ii],
component = this.refs[`category-${ii}`] component = this.refs[`category-${ii}`]
if (!minTop || component.top < minTop) {
if (component.top > 0) {
minTop = component.top
}
}
if (component) { if (component) {
let active = component.handleScroll(scrollTop) let active = component.handleScroll(scrollTop)
if (!minTop || component.top < minTop) {
if (component.top > 0) {
minTop = component.top
}
}
if (active && !activeCategory) { if (active && !activeCategory) {
if (category.anchor) category = category.anchor if (category.anchor) category = category.anchor
activeCategory = category activeCategory = category
@ -186,6 +201,12 @@ export default class Picker extends React.Component {
} }
} }
getCategories() {
var categories = CATEGORIES
return this.state.firstRender ? categories.slice(0, 3) : categories
}
render() { render() {
var { perLine, emojiSize, sheetURL, style } = this.props, var { perLine, emojiSize, sheetURL, style } = this.props,
{ skin } = this.state, { skin } = this.state,
@ -206,7 +227,7 @@ export default class Picker extends React.Component {
onSearch={this.handleSearch.bind(this)} onSearch={this.handleSearch.bind(this)}
/> />
{CATEGORIES.map((category, i) => { {this.getCategories().map((category, i) => {
return <Category return <Category
ref={`category-${i}`} ref={`category-${i}`}
key={category.name} key={category.name}