Skip to content

Commit

Permalink
Merge pull request #298 from open-pv/270-repair-search-field-parsing
Browse files Browse the repository at this point in the history
Repair parsing + trigger for autocomplete
  • Loading branch information
FlorianK13 authored Sep 15, 2024
2 parents 5bf0830 + ea98608 commit ae52748
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/components/PVSimulation/SearchField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export default function SearchField({ callback }) {
const [inputValue, setInputValue] = useState("")
const [suggestions, setSuggestions] = useState([])
const [suggestionsVisible, setSuggestionsVisible] = useState(false)
// isSelectedAdress is used so that if an adress is already selected,
// the autocomplete does stop to run
const [isSelectedAdress, setIsSelectedAdress] = useState(false)
const suggestionsRef = useRef([])
const inputRef = useRef()
Expand All @@ -33,7 +35,9 @@ export default function SearchField({ callback }) {

useEffect(() => {
const fetchSuggestions = async () => {
if (!inputValue) {
if (inputValue.length < 3) {
// If the input is deleted or replaced with one
// charakter, the autocomplete should start again
setIsSelectedAdress(false)
}
if (isSelectedAdress) {
Expand All @@ -45,8 +49,17 @@ export default function SearchField({ callback }) {
let streetAddressNumber = null

// Find the street address number
for (const inputPart of inputValueParts) {
if (/^\d+[a-zA-Z]?$/.test(inputPart)) {
for (let inputPart of inputValueParts) {
if (inputPart[inputPart.length - 1] === ",") {
//drop last character (ie the comma)
inputPart = inputPart.slice(0, -1)
}
if (inputPart.length == 5) {
// continue if it has the length of a zip code
continue
}
if (/^\d{1,3}[a-zA-Z]?$/.test(inputPart)) {
// regex chatches numbers with 1-3 digits plus one charater
streetAddressNumber = inputPart
break
}
Expand Down Expand Up @@ -83,7 +96,7 @@ export default function SearchField({ callback }) {
setSuggestionsVisible(suggestions.length > 0)
}

const debounceTimer = setTimeout(fetchSuggestions, 300)
const debounceTimer = setTimeout(fetchSuggestions, 200)
return () => clearTimeout(debounceTimer)
}, [inputValue, isSelectedAdress])

Expand Down

0 comments on commit ae52748

Please sign in to comment.