From 7ddf207064c2ab4d4697c6b7f8b3fcce0442dfc9 Mon Sep 17 00:00:00 2001 From: Collin Beczak <88843144+CollinBeczak@users.noreply.github.com> Date: Thu, 25 Jan 2024 15:25:44 -0600 Subject: [PATCH] move active filters at top of filter list fo auto suggest text box (#2244) * move active filters at top of filter list fo auto suggest text box * add sorting condition and simplify code * fix sorting conditions * make it so "All" related check boxes arnt selected when other items are * fix checcked item appearing above all option on initial render --- .../AutosuggestTextBox/AutosuggestTextBox.js | 88 ++++++++++--------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/src/components/AutosuggestTextBox/AutosuggestTextBox.js b/src/components/AutosuggestTextBox/AutosuggestTextBox.js index ac62099b2..ef2d84eea 100644 --- a/src/components/AutosuggestTextBox/AutosuggestTextBox.js +++ b/src/components/AutosuggestTextBox/AutosuggestTextBox.js @@ -95,64 +95,68 @@ export default class AutosuggestTextBox extends Component { */ dropdownItems(getItemProps) { const isChecked = (result) => { - if (result.id === FILTER_SEARCH_ALL && this.props.multiselect?.includes(FILTER_SEARCH_ALL)) { - return true + const isAllOption = result.id === FILTER_SEARCH_ALL + + if (this.props.multiselect) { + const isItemSelected = this.props.multiselect.includes(result.id) + return this.props.multiselect.length === 0 ? isAllOption : isItemSelected } - - if (this.props.multiselect?.includes(result.id)) { - return true - } - - return false + + return isAllOption } - + const generateResult = (result, className = "", index) => { if (this.state.highlightResult === index) { className += this.props.highlightClassName } - - if (!_isEmpty(result)) { - return ( - - - {this.props.multiselect && result.id !== FILTER_SEARCH_TEXT - ? - : null - } - {this.props.resultLabel(result)} - - - ) - } - else return null + + return !_isEmpty(result) ? ( + + + {this.props.multiselect && result.id !== FILTER_SEARCH_TEXT && ( + + )} + {this.props.resultLabel(result)} + + + ) : null } let items = [] const searchResults = this.getSearchResults() const preferredResults = this.getPreferredResults() + + const reorderedSearchResults = searchResults.sort((a, b) => { + if (a.id === FILTER_SEARCH_ALL) return -1 + if (b.id === FILTER_SEARCH_ALL) return 1 + if (a.name === this.props.inputValue) return -1 + if (b.name === this.props.inputValue) return 1 + return isChecked(b) - isChecked(a) + }) + if (!_isEmpty(preferredResults)) { let className = "mr-font-medium" - items = items.concat(_map(preferredResults, + items = items.concat(_map(reorderedSearchResults, (result, index) => { // Add a border bottom to the last entry if there are more // search results. - if (index === preferredResults.length - 1 && searchResults.length > 0) { + if (index === reorderedSearchResults.length - 1 && reorderedSearchResults.length > 0) { className += " mr-border-b-2 mr-border-white-50 mr-mb-2 mr-pb-2" }