Skip to content

Commit

Permalink
Highlight the filter text in the intervention popup
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Jul 24, 2023
1 parent 6d31533 commit 96ef988
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/lib/browse/Filters.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
// by scheme_reference
export let schemesToBeShown: Set<string> = new Set();
let filterText = "";
// Read-only, so callers can highlight search terms
export let filterText = "";
// Dropdown filters
let authorities: [string, string][] = [];
Expand Down Expand Up @@ -127,7 +128,7 @@
emptyOption
bind:value={filterFundingProgramme}
/>
<FormElement label="Name or description" id="filterText">
<FormElement label="Intervention name or description" id="filterText">
<input
type="text"
class="govuk-input govuk-input--width-10"
Expand Down
20 changes: 17 additions & 3 deletions src/pages/BrowseSchemes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
let schemes: Map<string, Scheme> = new Map();
let schemesToBeShown: Set<string> = new Set();
let filterText = "";
onDestroy(() => {
gjScheme.set(emptyGeojson() as GjScheme);
Expand All @@ -58,16 +59,29 @@
function tooltip(props: { [name: string]: any }): string {
// TODO Move into a Svelte component, so we don't have to awkwardly build up HTML like this
var html = `<div class="govuk-prose" style="max-width: 30vw;">`;
html += `<h2>${props.name} (${props.intervention_type})</h2>`;
html += `<h2>${highlightFilter(props.name)} (${
props.intervention_type
})</h2>`;
html += `<p>Scheme reference: ${props.scheme_reference}</p>`;
if (props.length_meters) {
html += `<p>Length: ${prettyPrintMeters(props.length_meters)}</p>`;
}
if (props.description) {
html += `<p>${props.description}</p>`;
html += `<p>${highlightFilter(props.description)}</p>`;
}
return html;
}
// When the user is filtering name/description by freeform text, highlight the matching pieces.
function highlightFilter(input: string): string {
if (!filterText) {
return input;
}
return input.replace(
new RegExp(filterText, "gi"),
(match) => `<mark>${match}</mark>`
);
}
</script>

<Layout>
Expand All @@ -85,7 +99,7 @@
<FileInput label="Load from GeoJSON" id="load-geojson" {loadFile} />

{#if schemes.size > 0}
<Filters {schemes} bind:schemesToBeShown />
<Filters {schemes} bind:schemesToBeShown bind:filterText />
{/if}

<ul>
Expand Down

0 comments on commit 96ef988

Please sign in to comment.