-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce filtering by country
- Loading branch information
Showing
10 changed files
with
144 additions
and
1 deletion.
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
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,15 @@ | ||
.countries-search { | ||
padding: 5px 7px; | ||
border-radius: 6px; | ||
line-height: 20px; | ||
font-size: inherit; | ||
width: 100%; | ||
border: 1px solid #e2e8f0; | ||
margin: 0 0 4px; | ||
} | ||
|
||
.countries-search:focus { | ||
outline: none; | ||
border-color: #0ea5e9; | ||
box-shadow: inset 0 0 0 2px #0ea5e9; | ||
} |
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,36 @@ | ||
import { type Country } from "flag-icons"; | ||
import { isTextMatch } from "../shared/filter"; | ||
import { debounce } from "../shared/utils"; | ||
|
||
import "./CountrySearch.css"; | ||
|
||
const INPUT_DELAY_IN_MS = 300; | ||
|
||
export class CountrySearch { | ||
constructor( | ||
private input: HTMLInputElement, | ||
private countries: Country[], | ||
) { } | ||
|
||
#filterCountries(text: string): Set<string> { | ||
const filteredCountries = new Set<string>(); | ||
|
||
for (const country of this.countries) { | ||
if (isTextMatch(country.name, text)) { | ||
filteredCountries.add(country.code); | ||
} | ||
} | ||
|
||
return filteredCountries; | ||
} | ||
|
||
onChange(callback: (codes: Set<string>) => void) { | ||
const debouncedCallback = debounce(() => { | ||
const filteredCountries = this.#filterCountries(this.input.value.trim()); | ||
|
||
callback(filteredCountries); | ||
}, INPUT_DELAY_IN_MS); | ||
|
||
this.input.addEventListener("input", debouncedCallback); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { ColorSelector } from "./ColorSelector"; | ||
export { CountrySearch } from "./CountrySearch"; | ||
export { CountrySelector } from "./CountrySelector"; | ||
export { Settings, type SettingsData } from "./Settings"; |
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,29 @@ | ||
/** | ||
* Rough fuzzy search that finds "omit" in "jOhn sMITh". | ||
* | ||
* @param value value to check, e.g. a page title "jOhn sMITh" | ||
* @param normalizedSearch string to search in the value, e.g. "omit" | ||
* @returns boolean if the value matches the search string | ||
*/ | ||
export function isTextMatch(value: string, search: string) { | ||
const normalizedValue = value.toLowerCase(); | ||
const normalizedSearch = search.toLowerCase(); | ||
|
||
let valuePointer = 0; | ||
let searchPointer = 0; | ||
|
||
while (valuePointer < normalizedValue.length && searchPointer < normalizedSearch.length) { | ||
if (normalizedSearch[searchPointer] === ' ') { | ||
searchPointer++; | ||
continue; | ||
} | ||
|
||
if (normalizedValue[valuePointer] === normalizedSearch[searchPointer]) { | ||
searchPointer++; | ||
} | ||
|
||
valuePointer++; | ||
} | ||
|
||
return searchPointer === normalizedSearch.length; | ||
} |
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