Skip to content

Commit a3c2cf8

Browse files
authored
Merge pull request #235 from chotacabras/master
feat: search bar feedback
2 parents 3083c78 + 3286c3e commit a3c2cf8

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/components/light-search-bar.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ OptionItem.propTypes = {
157157
const LightSearchBar = () => {
158158
const [value, setValue] = useState()
159159
const [data, setData] = useState([])
160+
const [empty, setEmpty] = useState(true)
161+
const [writing, setWriting] = useState(true)
160162
const { tcrAddress } = useContext(LightTCRViewContext)
161163
const [makeItemSearchQuery, itemSearchQuery] = useLazyQuery(ITEM_SEARCH_QUERY)
162164

@@ -171,6 +173,7 @@ const LightSearchBar = () => {
171173
first: MAX_ITEM_COUNT
172174
}
173175
})
176+
setWriting(false)
174177
}, 700)
175178

176179
useEffect(() => {
@@ -197,9 +200,23 @@ const LightSearchBar = () => {
197200
)
198201
})
199202

200-
const onSearch = value => debouncedCallback(value)
203+
const onSearch = value => {
204+
debouncedCallback(value)
205+
setWriting(true)
206+
if (value === '') setEmpty(true)
207+
else setEmpty(false)
208+
}
201209
const onChange = itemID => setValue(itemID)
202210

211+
const shownOptions = () => {
212+
if (empty) return []
213+
if (writing || itemSearchQuery.loading)
214+
return <Select.Option key="Loading">Loading...</Select.Option>
215+
if (!writing && options.length === 0)
216+
return <Select.Option key="NoResult">No results</Select.Option>
217+
else return options
218+
}
219+
203220
return (
204221
<StyledSelect
205222
id="items-search-bar"
@@ -218,7 +235,7 @@ const LightSearchBar = () => {
218235
</>
219236
}
220237
>
221-
{options}
238+
{shownOptions()}
222239
</StyledSelect>
223240
)
224241
}

src/components/search-bar.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ OptionItem.propTypes = {
165165
const SearchBar = () => {
166166
const [value, setValue] = useState()
167167
const [data, setData] = useState([])
168+
const [empty, setEmpty] = useState(true)
169+
const [writing, setWriting] = useState(true)
168170
const [enhancedDataSource, setEnhancedDataSource] = useState([])
169171
const {
170172
itemSubmissionLogs: dataSource,
@@ -264,10 +266,14 @@ const SearchBar = () => {
264266
: []
265267

266268
setData(results)
269+
setWriting(false)
267270
}, 700)
268-
const onSearch = useCallback(value => debouncedCallback(value), [
269-
debouncedCallback
270-
])
271+
const onSearch = value => {
272+
debouncedCallback(value)
273+
setWriting(true)
274+
if (value === '') setEmpty(true)
275+
else setEmpty(false)
276+
}
271277

272278
const options = data.map(d => {
273279
// Iterate through the item fields and find the first text field
@@ -294,6 +300,14 @@ const SearchBar = () => {
294300

295301
const onChange = useCallback(itemID => setValue(itemID), [])
296302

303+
const shownOptions = () => {
304+
if (empty) return []
305+
if (writing) return <Select.Option key="Loading">Loading...</Select.Option>
306+
if (!writing && options.length === 0)
307+
return <Select.Option key="NoResult">No results</Select.Option>
308+
else return options
309+
}
310+
297311
return (
298312
<StyledSelect
299313
id="items-search-bar"
@@ -312,7 +326,7 @@ const SearchBar = () => {
312326
</>
313327
}
314328
>
315-
{options}
329+
{shownOptions()}
316330
</StyledSelect>
317331
)
318332
}

0 commit comments

Comments
 (0)