File tree Expand file tree Collapse file tree 5 files changed +71
-4
lines changed Expand file tree Collapse file tree 5 files changed +71
-4
lines changed Original file line number Diff line number Diff line change @@ -117,7 +117,10 @@ searchJs.open()
117
117
async function getFromApi (keyword = ' ' ) {
118
118
let res = await fetch (' https://dummyjson.com/products/search?q=' + keyword)
119
119
let json = await res .json ()
120
- return json .products
120
+ return json .products .map ((p ) => {
121
+ p .icon = ` <img class="product-image" src="${ p .thumbnail } " />`
122
+ return p
123
+ })
121
124
}
122
125
123
126
const searchJs = SearchJS ({
@@ -145,6 +148,7 @@ const searchJs = SearchJS({
145
148
| ` data ` | YES | data to search |
146
149
| ` data.title ` | YES | data title |
147
150
| ` data.description ` | NO | data description |
151
+ | ` data.icon ` | NO | data icon (can use html string, svg string etc...) |
148
152
| ` element ` | NO | element to append search-js |
149
153
| ` theme ` | NO | color code or theme name (` #FF2E1F ` , ` github-light ` , ` github-dark ` ) |
150
154
| ` darkMode ` | NO | default ` false ` . set ` true ` for dark mode |
Original file line number Diff line number Diff line change 12
12
font-family : 'Source Sans Pro' , sans-serif;
13
13
background-color : # 424243 ;
14
14
}
15
+ .product-image {
16
+ width : 50px ;
17
+ height : 50px ;
18
+ object-fit : contain;
19
+ background : # ededed ;
20
+ border-radius : 5px ;
21
+ }
15
22
</ style >
16
23
</ head >
17
24
< body >
21
28
async function getFromApi ( keyword = '' ) {
22
29
let res = await fetch ( 'https://dummyjson.com/products/search?q=' + keyword )
23
30
let json = await res . json ( )
24
- return json . products
31
+ return json . products . map ( ( p ) => {
32
+ p . icon = `<img class="product-image" src="${ p . thumbnail } " />`
33
+ return p
34
+ } )
25
35
}
26
36
27
37
const searchJs = SearchJS ( {
Original file line number Diff line number Diff line change @@ -55,7 +55,7 @@ export class Item {
55
55
render ( { item, icon, hideRemoveButton = false } : ItemComponentPayload ) : string {
56
56
const dataPayload = Encoder . encode ( item )
57
57
return `<div class="item" ${ ATTR_DATA_PAYLOAD } ='${ dataPayload } '>
58
- <div class="item-icon">${ icon } </div>
58
+ <div class="item-icon">${ item . icon ?? icon } </div>
59
59
<div style="flex: 1">
60
60
<div class="item-title">${ item . title } </div>
61
61
${ item . description ? `<div class="item-description">${ item . description } </div>` : `` }
Original file line number Diff line number Diff line change @@ -107,7 +107,10 @@ declare global {
107
107
SearchJS : ( config : SearchJSConfig ) => SearchJSApp
108
108
}
109
109
}
110
- window . SearchJS = SearchJS
110
+
111
+ if ( typeof window !== 'undefined' ) {
112
+ window . SearchJS = SearchJS
113
+ }
111
114
112
115
export default SearchJS
113
116
export * from './types'
Original file line number Diff line number Diff line change @@ -8,22 +8,72 @@ export enum SearchJSTheme {
8
8
export interface SearchJSItem {
9
9
title : string
10
10
description ?: string
11
+ icon ?: string
11
12
[ propName : string ] : any
12
13
}
13
14
14
15
export interface SearchJSConfig {
16
+ /**
17
+ * element to append search-js
18
+ */
15
19
element ?: HTMLElement
20
+
21
+ /**
22
+ * color code or theme name (`#FF2E1F`, `github-light`, `github-dark`)
23
+ */
16
24
theme ?: string
25
+
26
+ /**
27
+ * width of search-js compoment
28
+ */
17
29
width ?: string
30
+
31
+ /**
32
+ * height of search-js compoment
33
+ */
18
34
height ?: string
35
+
36
+ /**
37
+ * enable dark mode
38
+ */
19
39
darkMode ?: boolean
40
+
41
+ /**
42
+ * position from the top
43
+ * default: 85px
44
+ */
20
45
positionTop ?: string
46
+
47
+ /**
48
+ * data to show on the list
49
+ */
21
50
data ?: Array < SearchJSItem >
51
+
52
+ /**
53
+ * customize search input
54
+ */
22
55
search ?: {
23
56
icon ?: string
24
57
placeholder ?: string
25
58
}
59
+
60
+ /**
61
+ * search after x delay in ms
62
+ * default - 500ms
63
+ */
26
64
onSearchDelay ?: number
65
+
66
+ /**
67
+ * handle on search input
68
+ * @param keyword
69
+ * @returns Array<SearchJSItem> | Promise<Array<SearchJSItem>>
70
+ */
27
71
onSearch ?: ( keyword : string ) => Array < SearchJSItem > | Promise < Array < SearchJSItem > >
72
+
73
+ /**
74
+ * handle the action after the item is selected
75
+ * @param data
76
+ * @returns void
77
+ */
28
78
onSelected : ( data : SearchJSItem ) => void
29
79
}
You can’t perform that action at this time.
0 commit comments