Simplify onChangeFilter category updating

Signed-off-by: Tulir Asokan <tulir@maunium.net>
This commit is contained in:
Tulir Asokan 2019-10-14 21:30:21 +03:00
parent bb1a242ef0
commit 7fc12b4b13

View file

@ -173,14 +173,17 @@ class EmojiPicker extends React.Component {
} }
onChangeFilter(filter) { onChangeFilter(filter) {
for (let [id, emojis] of Object.entries(this.memoizedDataByCategory)) { for (const cat of this.categories) {
let emojis;
// If the new filter string includes the old filter string, we don't have to re-filter the whole dataset. // If the new filter string includes the old filter string, we don't have to re-filter the whole dataset.
if (!filter.includes(this.state.filter)) { if (filter.includes(this.state.filter)) {
emojis = id === "recent" ? this.recentlyUsed : DATA_BY_CATEGORY[id]; emojis = this.memoizedDataByCategory[cat.id];
} else {
emojis = cat.id === "recent" ? this.recentlyUsed : DATA_BY_CATEGORY[cat.id];
} }
this.memoizedDataByCategory[id] = emojis.filter(emoji => emoji.filterString.includes(filter)); emojis = emojis.filter(emoji => emoji.filterString.includes(filter));
const cat = this.categories.find(cat => cat.id === id); this.memoizedDataByCategory[cat.id] = emojis;
cat.enabled = this.memoizedDataByCategory[id].length > 0; cat.enabled = emojis.length > 0;
// The setState below doesn't re-render the header and we already have the refs for updateVisibility, so... // The setState below doesn't re-render the header and we already have the refs for updateVisibility, so...
cat.ref.current.disabled = !cat.enabled; cat.ref.current.disabled = !cat.enabled;
} }