From 47f2341f5356ad4643336abf7bb9162193144fa9 Mon Sep 17 00:00:00 2001 From: Etienne Lemay Date: Fri, 8 Jul 2016 13:56:29 -0400 Subject: [PATCH] Refactor search --- src/components/anchors.js | 13 ++++++-- src/components/category.js | 23 +++++++++++-- src/components/picker.js | 68 +++++++++++++++++++++++--------------- 3 files changed, 73 insertions(+), 31 deletions(-) diff --git a/src/components/anchors.js b/src/components/anchors.js index fd8747b..118d937 100644 --- a/src/components/anchors.js +++ b/src/components/anchors.js @@ -7,8 +7,13 @@ export default class Anchors extends React.Component { constructor(props) { super(props) + var defaultCategory = props.categories[0] + if (defaultCategory.anchor) { + defaultCategory = defaultCategory.anchor + } + this.state = { - selected: props.categories[0].name + selected: defaultCategory.name } } @@ -18,7 +23,11 @@ export default class Anchors extends React.Component { return
{categories.map((category, i) => { - var { name } = category + var { name, anchor } = category + + if (anchor) { + return null + } return ( { + component.memoizeSize() if (maxMargin == component.maxMargin) return - var { categories } = this.state - - for (let i = 0, l = categories.length; i < l; i++) { - let component = this.refs[`category-${i}`] - if (component) component.memoizeSize() - } + this.updateCategoriesSize() this.handleScrollPaint() }) } @@ -88,18 +89,18 @@ export default class Picker extends React.Component { var target = this.refs.scroll, scrollTop = target.scrollTop, - scrollingDown = scrollTop >= (this.scrollTop || 0), - activeCategory = null, - { categories } = this.state + scrollingDown = scrollTop > (this.scrollTop || 0), + activeCategory = null - for (let i = 0, l = categories.length; i < l; i++) { - let ii = scrollingDown ? (categories.length - 1 - i) : i, - category = categories[ii], + for (let i = 0, l = CATEGORIES.length; i < l; i++) { + let ii = scrollingDown ? (CATEGORIES.length - 1 - i) : i, + category = CATEGORIES[ii], component = this.refs[`category-${ii}`] if (component) { let active = component.handleScroll(scrollTop) if (active && !activeCategory) { + if (category.anchor) category = category.anchor activeCategory = category } } @@ -118,14 +119,20 @@ export default class Picker extends React.Component { } handleSearch(emojis) { - if (emojis == null) { - this.setState({ categories: DEFAULT_CATEGORIES }) - } else { - this.setState({ categories: [{ - name: 'Search', - emojis: emojis, - }]}) + SEARCH_CATEGORY.emojis = emojis + + for (let i = 0, l = CATEGORIES.length; i < l; i++) { + let component = this.refs[`category-${i}`] + + if (component && component.props.name != 'Search') { + let DOMNode = ReactDOM.findDOMNode(component), + display = emojis ? 'none' : null + + if (DOMNode) DOMNode.style.display = display + } } + + this.forceUpdate() } handleAnchorClick(category, i) { @@ -135,7 +142,7 @@ export default class Picker extends React.Component { if (component) { let { top } = component - if (i == 0) { + if (category.name == 'Recent') { top = 0 } else { top += 1 @@ -152,6 +159,13 @@ export default class Picker extends React.Component { store.update(newState) } + updateCategoriesSize() { + for (let i = 0, l = CATEGORIES.length; i < l; i++) { + let component = this.refs[`category-${i}`] + if (component) component.memoizeSize() + } + } + render() { var { perLine, emojiSize, sheetURL } = this.props, { skin } = this.state, @@ -161,7 +175,7 @@ export default class Picker extends React.Component {
@@ -171,7 +185,7 @@ export default class Picker extends React.Component { onSearch={this.handleSearch.bind(this)} /> - {this.state.categories.map((category, i) => { + {CATEGORIES.map((category, i) => { return