55 useLoaderData ,
66 Form ,
77 useNavigation ,
8+ useSubmit ,
89} from 'react-router-dom' ;
910import { getContacts , createContact } from '../contacts' ;
1011
@@ -23,11 +24,21 @@ export const action = async () => {
2324export default function Root ( ) {
2425 const { contacts, q } = useLoaderData ( ) ;
2526 const navigation = useNavigation ( ) ;
27+ const submit = useSubmit ( ) ;
2628
29+ const searching =
30+ navigation . location &&
31+ new URLSearchParams ( navigation . location . search ) . has ( 'q' ) ;
32+
33+ const changeHandler = ( event ) => {
34+ const isFirstSearch = q === null ;
35+ console . log ( 'isFirstSearch: ' , isFirstSearch ) ;
36+ submit ( event . currentTarget . form , { replace : ! isFirstSearch } ) ;
37+ } ;
2738 // Synchronize the URL and Form state
2839 useEffect ( ( ) => {
29- document . getElementById ( "q" ) . value = q
30- } , [ q ] )
40+ document . getElementById ( 'q' ) . value = q ;
41+ } , [ q ] ) ;
3142 return (
3243 < >
3344 < div id = 'sidebar' >
@@ -36,13 +47,15 @@ export default function Root() {
3647 < Form id = 'search-form' role = 'search' >
3748 < input
3849 id = 'q'
50+ className = { searching ? 'loading' : '' }
3951 aria-label = 'Search contacts'
4052 placeholder = 'Search'
4153 type = 'search'
4254 name = 'q'
4355 defaultValue = { q }
56+ onChange = { changeHandler }
4457 />
45- < div id = 'search-spinner' aria-hidden hidden = { true } />
58+ < div id = 'search-spinner' aria-hidden hidden = { ! searching } />
4659 < div className = 'sr-only' aria-live = 'polite' > </ div >
4760 </ Form >
4861 < Form method = 'post' >
0 commit comments