@@ -26,39 +26,65 @@ yarn add react-api-search
2626## Usage
2727
2828``` tsx
29- import React , { useState } from ' react' ;
3029import SearchBar from ' react-api-search' ;
3130
32- const MyComponent = () => {
33- const [selectedItem, setSelectedItem] = useState (null );
34-
35- const fetchSearchResults = async (query : string ) => {
36- const response = await fetch (' https://api.example.com/search?q=${query}' );
37- const data = await response .json ();
38- return data .results ; // return an array of results
39- };
40-
41- const renderSearchResult = (item : any ) => <div >{ item .name } </div >;
31+ type Post = {
32+ id: number ;
33+ title: string ;
34+ body: string ;
35+ };
4236
43- const handleItemSelect = (item : any ) => {
44- setSelectedItem (item );
45- };
37+ const fetchPosts = async (query : string ): Promise <Post []> => {
38+ const response = await fetch (
39+ ` https://jsonplaceholder.typicode.com/posts?q=${encodeURIComponent (query )} `
40+ );
41+ if (! response .ok ) throw new Error (' Failed to fetch posts' );
42+ const data = await response .json ();
43+ return data ;
44+ };
4645
46+ function App() {
4747 return (
48- <div >
49- <SearchBar
50- fetchData = { fetchSearchResults }
51- renderItem = { renderSearchResult }
52- onSelect = { handleItemSelect }
53- placeholder = ' Search for items...'
54- debounceDelay = { 300 }
55- />
56- { selectedItem && <div >You selected: { selectedItem .name } </div >}
48+ <div style = { { padding: ' 20px' }} >
49+ <h1 >Posts Search</h1 >
50+ <div style = { { width: ' 40rem' }} >
51+ <SearchBar <Post >
52+ placeholder = ' Search for posts...'
53+ fetchData = { fetchPosts }
54+ loadingElement = {
55+ <div className = ' custom-loading' >Please wait, fetching data...</div >
56+ }
57+ emptyElement = {
58+ <div >No posts match your search. Try something else!</div >
59+ }
60+ errorElement = {
61+ <div >Oops, something went wrong. Please try again later.</div >
62+ }
63+ renderItem = { (post ) => (
64+ <div
65+ style = { { padding: ' 10px' , borderBottom: ' 1px solid #ccc' }}
66+ onMouseEnter = { (e ) => {
67+ e .currentTarget .style .backgroundColor = ' #f0f0f0' ; // hover background
68+ }}
69+ onMouseLeave = { (e ) => {
70+ e .currentTarget .style .backgroundColor = ' transparent' ; // reset background
71+ }}
72+ >
73+ <h3 style = { { margin: 0 }} >{ post .title } </h3 >
74+ <p style = { { margin: ' 5px 0' , fontSize: ' 0.9em' , color: ' #555' }} >
75+ { post .body }
76+ </p >
77+ </div >
78+ )}
79+ onSelect = { (post ) => alert (` Selected post: ${post .title } ` )}
80+ debounceDelay = { 500 }
81+ />
82+ </div >
5783 </div >
5884 );
59- };
85+ }
6086
61- export default MyComponent ;
87+ export default App ;
6288```
6389
6490## Props
@@ -94,24 +120,6 @@ export default MyComponent;
94120| dropDownBorderRadius | string | Border radius of the dropdown. | '8px' |
95121| scrollBarColor | string | Color of the scrollbar inside the dropdown. | #ccc |
96122
97- ## Example
98-
99- ### Basic Example
100-
101- ``` tsx
102- <SearchBar
103- fetchData = { async (query : string ) => {
104- const response = await fetch (' https://api.example.com/search?q=${query}' );
105- return response .json ();
106- }}
107- renderItem = { (item ) => <div >{ item .name } </div >}
108- onSelect = { (item ) => console .log (' Selected:' , item )}
109- placeholder = ' Search for items...'
110- loadingElement = { <div >Loading...</div >}
111- emptyElement = { <div >No results found</div >}
112- />
113- ```
114-
115123## License
116124
117125This project is licensed under the MIT License. See the [ LICENSE] ( https://github.com/ghosnkarl/react-api-search/blob/main/LICENSE ) file for more information.
0 commit comments