Skip to content

Commit 5f0c97b

Browse files
add option to set custom item icon (#42)
1 parent b100773 commit 5f0c97b

File tree

5 files changed

+71
-4
lines changed

5 files changed

+71
-4
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ searchJs.open()
117117
async function getFromApi(keyword = '') {
118118
let res = await fetch('https://dummyjson.com/products/search?q=' + keyword)
119119
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+
})
121124
}
122125

123126
const searchJs = SearchJS({
@@ -145,6 +148,7 @@ const searchJs = SearchJS({
145148
| `data` | YES | data to search |
146149
| `data.title` | YES | data title |
147150
| `data.description` | NO | data description |
151+
| `data.icon` | NO | data icon (can use html string, svg string etc...) |
148152
| `element` | NO | element to append search-js |
149153
| `theme` | NO | color code or theme name (`#FF2E1F`, `github-light`, `github-dark`) |
150154
| `darkMode` | NO | default `false`. set `true` for dark mode |

index.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
font-family: 'Source Sans Pro', sans-serif;
1313
background-color: #424243;
1414
}
15+
.product-image {
16+
width: 50px;
17+
height: 50px;
18+
object-fit: contain;
19+
background: #ededed;
20+
border-radius: 5px;
21+
}
1522
</style>
1623
</head>
1724
<body>
@@ -21,7 +28,10 @@
2128
async function getFromApi(keyword = '') {
2229
let res = await fetch('https://dummyjson.com/products/search?q='+keyword)
2330
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+
})
2535
}
2636

2737
const searchJs = SearchJS({

src/components/Item.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class Item {
5555
render({ item, icon, hideRemoveButton = false }: ItemComponentPayload): string {
5656
const dataPayload = Encoder.encode(item)
5757
return `<div class="item" ${ATTR_DATA_PAYLOAD}='${dataPayload}'>
58-
<div class="item-icon">${icon}</div>
58+
<div class="item-icon">${item.icon ?? icon}</div>
5959
<div style="flex: 1">
6060
<div class="item-title">${item.title}</div>
6161
${item.description ? `<div class="item-description">${item.description}</div>` : ``}

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ declare global {
107107
SearchJS: (config: SearchJSConfig) => SearchJSApp
108108
}
109109
}
110-
window.SearchJS = SearchJS
110+
111+
if (typeof window !== 'undefined') {
112+
window.SearchJS = SearchJS
113+
}
111114

112115
export default SearchJS
113116
export * from './types'

src/types/index.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,72 @@ export enum SearchJSTheme {
88
export interface SearchJSItem {
99
title: string
1010
description?: string
11+
icon?: string
1112
[propName: string]: any
1213
}
1314

1415
export interface SearchJSConfig {
16+
/**
17+
* element to append search-js
18+
*/
1519
element?: HTMLElement
20+
21+
/**
22+
* color code or theme name (`#FF2E1F`, `github-light`, `github-dark`)
23+
*/
1624
theme?: string
25+
26+
/**
27+
* width of search-js compoment
28+
*/
1729
width?: string
30+
31+
/**
32+
* height of search-js compoment
33+
*/
1834
height?: string
35+
36+
/**
37+
* enable dark mode
38+
*/
1939
darkMode?: boolean
40+
41+
/**
42+
* position from the top
43+
* default: 85px
44+
*/
2045
positionTop?: string
46+
47+
/**
48+
* data to show on the list
49+
*/
2150
data?: Array<SearchJSItem>
51+
52+
/**
53+
* customize search input
54+
*/
2255
search?: {
2356
icon?: string
2457
placeholder?: string
2558
}
59+
60+
/**
61+
* search after x delay in ms
62+
* default - 500ms
63+
*/
2664
onSearchDelay?: number
65+
66+
/**
67+
* handle on search input
68+
* @param keyword
69+
* @returns Array<SearchJSItem> | Promise<Array<SearchJSItem>>
70+
*/
2771
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+
*/
2878
onSelected: (data: SearchJSItem) => void
2979
}

0 commit comments

Comments
 (0)