Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add keyboard search feature #63

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 94 additions & 29 deletions src/components/AdvancedSearch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef, useEffect, useState } from 'react'
import React, { forwardRef, useEffect, useState, useRef } from 'react'
import { Container, Dropdown } from 'semantic-ui-react'
import '../css/mainpagecss.css'

Expand All @@ -10,10 +10,13 @@ import AutoComplete from '../utils/AutoComplete'

const { useImperativeHandle } = React;


const AdvancedSearch = forwardRef((props, ref) => {
const { buildSearchList } = props
let inputElement;

const _searchBoxHandle = useRef(null)
const _inputHandle = useRef(null)

const searchFilterOptions = [
{
key: 0,
Expand All @@ -34,24 +37,25 @@ const AdvancedSearch = forwardRef((props, ref) => {
key: 3,
text: 'Topic',
value: 3
}
},
]

const [search, setSearch] = useState('')
const [filter, setFilter] = useState(0)
const [autoComplete, setAutoComplete] = useState(null)
const [suggestions, setSuggestions] = useState([])
const [isInputInFocus, setIsInputInFocus] = useState(false)
const [suggestionCopy, setsuggestionCopy] = useState([])
let [searchCounter, setsearchCounter] = useState(0)

useImperativeHandle(ref, () => ({
resetSearchState () {
resetSearchState() {
setSearch('');
setFilter(0);
setAutoComplete(null);
setSuggestions([]);
setIsInputInFocus(false);
}
}));
}))

const handleFilter = (unNeccesaryThing, e) => {
setFilter(e.value)
Expand All @@ -60,10 +64,10 @@ const AdvancedSearch = forwardRef((props, ref) => {
const handleSearch = e => {
e && e.preventDefault()
document.activeElement.blur()
buildSearchList(search, filter)
buildSearchList(_inputHandle.current.value, filter)
}

const focusHandler = e => {
const focusHandler = (e) => {
setsearchCounter(0)
// setSuggestions(autoComplete.suggest(search))
setTimeout(() => {
setIsInputInFocus(document.activeElement === inputElement)
Expand All @@ -88,13 +92,17 @@ const AdvancedSearch = forwardRef((props, ref) => {
list = [...[...dataSet].sort((a, b) => (a - b))]
}
if (filter === 1) {
data.forEach(e => {
list.push(e.name.replaceAll('/', '').replaceAll(' ', ' ').toLowerCase())
data.forEach((e) => {
list.push(
e.name.replaceAll('/', '').replaceAll(' ', ' ').toLowerCase()
)
})
}
if (filter === 2) {
data.forEach(e => {
list.push(e.cat.replaceAll('/', '').replaceAll(' ', ' ').toLowerCase())
data.forEach((e) => {
list.push(
e.cat.replaceAll('/', '').replaceAll(' ', ' ').toLowerCase()
)
})
}
if (filter === 3) {
Expand All @@ -117,12 +125,62 @@ const AdvancedSearch = forwardRef((props, ref) => {
autoComplete && setSuggestions(autoComplete.suggest(search))
}, [search])

const func = (e) => {
if (e.code == 'Enter') {
setSearch(suggestionCopy[searchCounter] || _inputHandle.current.value)
}
if (suggestionCopy.length == 0 && searchCounter == 0) {
setsearchCounter(0)
} else {
if (e.code == 'ArrowDown') {
if (searchCounter > suggestionCopy.length - 1) {
setsearchCounter(searchCounter--)
} else {
if (suggestionCopy[searchCounter] == _inputHandle.current.value) {
_inputHandle.current.value =
suggestionCopy[searchCounter + 1] != undefined
? suggestionCopy[searchCounter + 1]
: suggestionCopy[searchCounter]
} else {
_inputHandle.current.value = suggestionCopy[searchCounter]
}
setsearchCounter(searchCounter++)
}
}
if (e.code == 'ArrowUp') {
searchCounter == 0
? setsearchCounter(0)
: setsearchCounter(searchCounter--)
if (suggestionCopy[searchCounter] == _inputHandle.current.value) {
_inputHandle.current.value =
suggestionCopy[searchCounter - 1] != undefined
? suggestionCopy[searchCounter - 1]
: suggestionCopy[searchCounter]
} else {
_inputHandle.current.value = suggestionCopy[searchCounter]
}
}
}
}

useEffect(() => {
_searchBoxHandle.current.addEventListener('keydown', func)
return () => {
_searchBoxHandle.current.removeEventListener('keydown', func)
}
}, [suggestionCopy])

useEffect(() => {
setsearchCounter(0)
setsuggestionCopy(suggestions.map((el) => el))
}, [suggestions])

return (
<Container textAlign='center'>
<form className="search-form" autocomplete="off">
<div id='searchBox'>
<form className="search-form" autocomplete='off'>
<div ref={_searchBoxHandle} id='searchBox'>
<input
value={search}
ref={_inputHandle}
onChange={e => {
if (e.keyCode === 13) {
handleSearch()
Expand All @@ -136,18 +194,26 @@ const AdvancedSearch = forwardRef((props, ref) => {
id="inputBox"
autocomplete="off"
/>
{
(isInputInFocus && suggestions.length > 0 && suggestions[0] !== search) && (<div className="suggestions-dropDown">
{suggestions.map(content => (
content !== search && <p
key={content}
onClick={() => {
setSearch(content)
buildSearchList(content, filter)
}}>{content}</p>
))}
</div>)
}
{isInputInFocus &&
suggestions.length > 0 &&
suggestions[0] !== search && (
<div className='suggestions-dropDown'>
{suggestions.map(
(content) =>
content !== search && (
<p
key={content}
onClick={() => {
setSearch(content)
// buildSearchList(content, filter)
}}
>
{content}
</p>
)
)}
</div>
)}
</div>
<button type='submit' onClick={handleSearch} className='search-btn'>
<FontAwesomeIcon color='white' className='fa-2x' icon={faSearch} />{' '}
Expand All @@ -160,7 +226,6 @@ const AdvancedSearch = forwardRef((props, ref) => {
selection
options={searchFilterOptions}
/>

</form>
</Container>
)
Expand Down