Skip to content

Commit

Permalink
Added callbacks so places are added to state when result is clicked.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaddox5 committed Dec 7, 2023
1 parent 2a6f7f3 commit a346bd7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ComponentType } from "react";
import React, { ComponentType, useState } from "react";
import { ReactSearchAutocomplete } from "react-search-autocomplete";

interface SearchItem {
Expand All @@ -8,11 +8,15 @@ interface SearchItem {

interface SearchBarProps {
items: SearchItem[];
handleSearchResultClick: (item: SearchItem) => void;
}

const SearchBar: ComponentType<SearchBarProps> = ({
items,
handleSearchResultClick,
}: SearchBarProps) => {
const [inputString, setInputString] = useState<string>("");

const formatResult = (item: SearchItem) => {
return (
<span className="result-row body--regular">
Expand All @@ -21,6 +25,14 @@ const SearchBar: ComponentType<SearchBarProps> = ({
);
};

const handleOnSearch = (searchString: string, _results: SearchItem[]) =>
setInputString(searchString);

const handleOnSelect = (item: SearchItem) => {
setInputString("");
handleSearchResultClick(item);
};

return (
<ReactSearchAutocomplete
formatResult={formatResult}
Expand All @@ -32,6 +44,9 @@ const SearchBar: ComponentType<SearchBarProps> = ({
className="search-bar body--medium"
showIcon={false}
showClear={false}
onSearch={handleOnSearch}
onSelect={handleOnSelect}
inputSearchString={inputString}
styling={{
height: "38px",
border: "none",
Expand All @@ -45,4 +60,5 @@ const SearchBar: ComponentType<SearchBarProps> = ({
);
};

export { SearchItem };
export default SearchBar;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { ComponentType, useState } from "react";
import { WorkflowProps } from "../ConfigureScreensPage";
import { Container } from "react-bootstrap";
import PlaceRow from "../../PlaceRow";
import SearchBar from "../SearchBar";
import SearchBar, { SearchItem } from "../SearchBar";
import { sortByStationOrder } from "../../../../util";
import SortLabel from "../../SortLabel";
import { SORT_LABELS } from "../../../../constants/constants";
Expand All @@ -13,6 +13,10 @@ const GlEinkWorkflow: ComponentType<WorkflowProps> = ({
}: WorkflowProps) => {
const [selectedPlaces, setSelectedPlaces] = useState<Set<string>>(new Set());
const [sortDirection, setSortDirection] = useState<DirectionID>(0);
const handleSearchResultClick = (place: SearchItem) => {
const existingSelectedPlaces = new Set(selectedPlaces);
setSelectedPlaces(existingSelectedPlaces.add(place.id));
};

return (
<Container className="workflow-container">
Expand All @@ -27,7 +31,10 @@ const GlEinkWorkflow: ComponentType<WorkflowProps> = ({
<div className="body--medium mb-2">
Enter Station ID or name to select stations
</div>
<SearchBar items={places} />
<SearchBar
items={places}
handleSearchResultClick={handleSearchResultClick}
/>
</div>
<div className="mt-4">
<SortLabel
Expand Down

0 comments on commit a346bd7

Please sign in to comment.