From 5e9f746f3766ee77b04c42d574d588f6dc5f35ed Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Sun, 26 Dec 2021 20:04:48 -0500 Subject: [PATCH] Fix Search error when user can't search If there are no search sources, HTML for the Search component won't be rendered, so trying to attach listeners to it will likely error. In this PR, we don't attach such listeners/logic if there are no sources. We also stop asserting that sources is defined to help avoid other similar issues in the future. --- framework/core/js/src/forum/components/Search.tsx | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/framework/core/js/src/forum/components/Search.tsx b/framework/core/js/src/forum/components/Search.tsx index 645fa9241..054c0b307 100644 --- a/framework/core/js/src/forum/components/Search.tsx +++ b/framework/core/js/src/forum/components/Search.tsx @@ -88,7 +88,7 @@ export default class Search extends Compone /** * An array of SearchSources. */ - protected sources!: SearchSource[]; + protected sources?: SearchSource[]; /** * The number of sources that are still loading results. @@ -192,7 +192,7 @@ export default class Search extends Compone this.setIndex(this.getCurrentNumericIndex()); // If there are no sources, the search view is not shown. - if (!this.sources.length) return; + if (!this.sources?.length) return; this.updateMaxHeight(); } @@ -200,6 +200,10 @@ export default class Search extends Compone oncreate(vnode: Mithril.VnodeDOM) { super.oncreate(vnode); + // If there are no sources, we shouldn't initialize logic for + // search elements, as they will not be shown. + if (!this.sources?.length) return; + const search = this; const state = this.searchState; @@ -237,7 +241,7 @@ export default class Search extends Compone if (state.isCached(query)) return; if (query.length >= (search.constructor as typeof Search).MIN_SEARCH_LEN) { - search.sources.map((source) => { + search.sources?.map((source) => { if (!source.search) return; search.loadingSources++;