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 1 commit
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
67 changes: 58 additions & 9 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 _searchBox=useRef(null)
const _inputHandle=useRef(null)

const searchFilterOptions = [
{
key: 0,
Expand Down Expand Up @@ -42,14 +45,15 @@ const AdvancedSearch = forwardRef((props, ref) => {
const [autoComplete, setAutoComplete] = useState(null)
const [suggestions, setSuggestions] = useState([])
const [isInputInFocus, setIsInputInFocus] = useState(false)
const [new_array,setNew_array]=useState([])
let [counter,setCounter]=useState(0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we give more meaningful names to these states like: keyCounter and something more apt for new_array


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

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 => {
setCounter(0)
// setSuggestions(autoComplete.suggest(search))
setTimeout(() => {
setIsInputInFocus(document.activeElement === inputElement)
Expand Down Expand Up @@ -117,12 +121,53 @@ const AdvancedSearch = forwardRef((props, ref) => {
autoComplete && setSuggestions(autoComplete.suggest(search))
}, [search])

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

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

useEffect(()=>{
setCounter(0)
setNew_array(suggestions.map(el=>el))
},[suggestions])

return (
<Container textAlign='center'>
<form className="search-form" autocomplete="off">
<div id='searchBox'>
<input
value={search}
<div ref={_searchBox} id='searchBox'>
<input ref={_inputHandle}
onChange={e => {
if (e.keyCode === 13) {
handleSearch()
Expand All @@ -143,7 +188,11 @@ const AdvancedSearch = forwardRef((props, ref) => {
key={content}
onClick={() => {
setSearch(content)
buildSearchList(content, filter)
/**buildSearchList is not needed because it is handled in handleSearch(),
* buildSearchList should be called clickable route functionality needed,
* since search by keyboard is enabled buildSearchList need not be called
*/
// buildSearchList(content, filter)
}}>{content}</p>
))}
</div>)
Expand Down