From 9eb8f0b6ac3ecdd814dc55470fbaf44e801a395d Mon Sep 17 00:00:00 2001 From: Etienne Lemay Date: Fri, 15 Jul 2016 12:24:50 -0400 Subject: [PATCH] Only render first 3 categories on first render --- example/index.js | 23 ++++++++++++++++------- src/components/picker.js | 35 ++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/example/index.js b/example/index.js index 1a93f06dc..2f2078777 100644 --- a/example/index.js +++ b/example/index.js @@ -11,6 +11,7 @@ class Example extends React.Component { perLine: 9, skin: 1, set: 'apple', + hidden: false, } } @@ -43,6 +44,12 @@ class Example extends React.Component { })} +
+ +
+
/>
       
- console.log(emoji)} - /> + {!this.state.hidden && + console.log(emoji)} + /> + } } } diff --git a/src/components/picker.js b/src/components/picker.js index 002652e75..64fd11b00 100644 --- a/src/components/picker.js +++ b/src/components/picker.js @@ -21,6 +21,7 @@ export default class Picker extends React.Component { this.state = { 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() { this.updateCategoriesSize() this.handleScroll() @@ -86,6 +96,10 @@ export default class Picker extends React.Component { handleScrollPaint() { this.waitingForPaint = false + if (!this.refs.scroll) { + return + } + var target = this.refs.scroll, scrollTop = target.scrollTop, scrollingDown = scrollTop > (this.scrollTop || 0), @@ -97,14 +111,15 @@ export default class Picker extends React.Component { category = CATEGORIES[ii], component = this.refs[`category-${ii}`] - if (!minTop || component.top < minTop) { - if (component.top > 0) { - minTop = component.top - } - } - if (component) { let active = component.handleScroll(scrollTop) + + if (!minTop || component.top < minTop) { + if (component.top > 0) { + minTop = component.top + } + } + if (active && !activeCategory) { if (category.anchor) category = category.anchor 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() { var { perLine, emojiSize, sheetURL, style } = this.props, { skin } = this.state, @@ -206,7 +227,7 @@ export default class Picker extends React.Component { onSearch={this.handleSearch.bind(this)} /> - {CATEGORIES.map((category, i) => { + {this.getCategories().map((category, i) => { return