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,6 +68,7 @@ class Example extends React.Component {
<br /><Operator>/&gt;</Operator> <br /><Operator>/&gt;</Operator>
</pre> </pre>
{!this.state.hidden &&
<Picker <Picker
emojiSize={this.state.emojiSize} emojiSize={this.state.emojiSize}
perLine={this.state.perLine} perLine={this.state.perLine}
@ -68,6 +76,7 @@ class Example extends React.Component {
sheetURL={`../sheets/sheet_${this.state.set}_64.png`} sheetURL={`../sheets/sheet_${this.state.set}_64.png`}
onClick={(emoji) => console.log(emoji)} 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 (component) {
let active = component.handleScroll(scrollTop)
if (!minTop || component.top < minTop) { if (!minTop || component.top < minTop) {
if (component.top > 0) { if (component.top > 0) {
minTop = component.top minTop = component.top
} }
} }
if (component) {
let active = component.handleScroll(scrollTop)
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}