From 7963bb9755367cc0f5f02b3d11bff5886278ddf5 Mon Sep 17 00:00:00 2001 From: Maria Date: Mon, 31 Oct 2022 11:56:15 -0400 Subject: [PATCH] Add default value to SearchBar --- src/components/SearchBar.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/components/SearchBar.js b/src/components/SearchBar.js index 73a2e72..0e2e69f 100644 --- a/src/components/SearchBar.js +++ b/src/components/SearchBar.js @@ -1,12 +1,15 @@ import React, { useCallback, useState } from 'react' import { Search } from '@trussworks/react-uswds' -import { useHistory } from 'react-router-dom' +import { useHistory, useLocation } from 'react-router-dom' import { SEARCH_FRAGMENT_ROUTE } from '../constants' const SearchBar = () => { const history = useHistory() - const [searchTerm, setSearchTerm] = useState('') + const location = useLocation() + const [searchTerm, setSearchTerm] = useState( + location.pathname.replace('/search/', '') || '' + ) const handleChange = useCallback((e) => setSearchTerm(e.target.value), []) @@ -26,6 +29,7 @@ const SearchBar = () => { onSubmit={handleSearch} onChange={handleChange} size="big" + defaultValue={searchTerm} />