-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We want to migrate all of our legacy Javascript files to ESM, so this is the latest in the series of conversions. Rewrite follows the same conventions we previously used for FocusBanner, Collapsible checkboxes, ColourPreview, FileUpload, Autofocus, PreviewPane and CopyToClipboard. Added bonus: removes the use of jQuery as well
- Loading branch information
Showing
3 changed files
with
94 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { isSupported } from 'govuk-frontend'; | ||
|
||
class LiveSearch { | ||
constructor($module) { | ||
if (!isSupported()) { | ||
return this; | ||
} | ||
|
||
this.$module = $module; | ||
|
||
this.$searchBox = this.$module.querySelector('input'); | ||
this.$searchLabel = this.$module.querySelector('label'); | ||
this.$liveRegion = this.$module.querySelector('.live-search__status'); | ||
this.$targets = document.querySelectorAll(this.$module.dataset.targets); | ||
this.state = 'loaded'; | ||
|
||
|
||
this.$searchBox.addEventListener("input", () => { | ||
this.filter(this.$searchBox, this.$searchLabel, this.$liveRegion, this.$targets); | ||
}); | ||
|
||
this.filter(this.$searchBox, this.$searchLabel, this.$liveRegion, this.$targets); | ||
|
||
} | ||
|
||
filter ($searchBox, $searchLabel, $liveRegion, $targets) { | ||
|
||
let query = this.normalize(this.$searchBox.value); | ||
let results = 0; | ||
|
||
$targets.forEach((node) => { | ||
|
||
let content = node.querySelector('.live-search-relevant') ? node.querySelector('.live-search-relevant').textContent : node.textContent; | ||
let isMatch = this.normalize(content).includes(this.normalize(query)); | ||
// if there is a child node with checked state | ||
if (node.querySelectorAll(':checked').length > 0) { | ||
node.style.display = 'block'; | ||
results++; | ||
return; | ||
} | ||
|
||
if (query == '') { | ||
node.style.display = ''; | ||
results++; | ||
return; | ||
} | ||
|
||
node.style.display = isMatch ? 'block': 'none'; | ||
|
||
if (isMatch) { | ||
results++; | ||
} | ||
|
||
}); | ||
|
||
if (this.state === 'loaded') { | ||
if (query !== '') { | ||
$searchBox.setAttribute('aria-label', $searchLabel.textContent.trim() + ', ' + this.resultsSummary(results)); | ||
} | ||
this.state = 'active'; | ||
} else { | ||
$searchBox.removeAttribute('aria-label'); | ||
$liveRegion.textContent = this.resultsSummary(results); | ||
} | ||
|
||
// make sticky JS recalculate its cache of the element's position | ||
// because live search can change the height document | ||
if ('stickAtBottomWhenScrolling' in window.GOVUK) { | ||
window.GOVUK.stickAtBottomWhenScrolling.recalculate(); | ||
} | ||
|
||
} | ||
|
||
normalize (string) { | ||
return string.toLowerCase().replace(/ /g,''); | ||
} | ||
|
||
resultsSummary (num) { | ||
if (num === 0) { | ||
return "no results"; | ||
} else { | ||
return num + (num === 1 ? " result" : " results"); | ||
} | ||
}; | ||
} | ||
|
||
export default LiveSearch; |
This file was deleted.
Oops, something went wrong.