From 18baab692305f4b25fc30d96df26ac06b78b7061 Mon Sep 17 00:00:00 2001 From: rahul Date: Sat, 11 Dec 2021 08:21:20 +0530 Subject: [PATCH 1/7] add keyboard search feature --- src/components/AdvancedSearch.js | 67 +++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/src/components/AdvancedSearch.js b/src/components/AdvancedSearch.js index bf46fe8..38d08e8 100644 --- a/src/components/AdvancedSearch.js +++ b/src/components/AdvancedSearch.js @@ -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' @@ -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, @@ -42,6 +45,8 @@ 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) useImperativeHandle(ref, () => ({ resetSearchState () { @@ -49,7 +54,6 @@ const AdvancedSearch = forwardRef((props, ref) => { setFilter(0); setAutoComplete(null); setSuggestions([]); - setIsInputInFocus(false); } })); @@ -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) @@ -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 (
- ) From 34a0e4008f9e21e78a0e3676bd3cb303b97a6144 Mon Sep 17 00:00:00 2001 From: rahul r Date: Wed, 15 Dec 2021 09:26:13 +0530 Subject: [PATCH 2/7] update variable names --- src/components/AdvancedSearch.js | 294 +++++++++++++++++-------------- 1 file changed, 157 insertions(+), 137 deletions(-) diff --git a/src/components/AdvancedSearch.js b/src/components/AdvancedSearch.js index 38d08e8..8ae37e4 100644 --- a/src/components/AdvancedSearch.js +++ b/src/components/AdvancedSearch.js @@ -1,218 +1,238 @@ -import React, { forwardRef, useEffect, useState,useRef } from 'react' -import { Container, Dropdown } from 'semantic-ui-react' -import '../css/mainpagecss.css' +import React, { forwardRef, useEffect, useState, useRef } from "react"; +import { Container, Dropdown } from "semantic-ui-react"; +import "../css/mainpagecss.css"; -import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' -import { faSearch } from '@fortawesome/free-solid-svg-icons' +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { faSearch } from "@fortawesome/free-solid-svg-icons"; -import data from '../data/finalData.json' -import AutoComplete from '../utils/AutoComplete' +import data from "../data/finalData.json"; +import AutoComplete from "../utils/AutoComplete"; const { useImperativeHandle } = React; const AdvancedSearch = forwardRef((props, ref) => { - const { buildSearchList } = props + const { buildSearchList } = props; let inputElement; - const _searchBox=useRef(null) - const _inputHandle=useRef(null) + const _searchBoxHandle = useRef(null); + const _inputHandle = useRef(null); const searchFilterOptions = [ { key: 0, - text: 'Tech Stack', - value: 0 + text: "Tech Stack", + value: 0, }, { key: 1, - text: 'Organisation Name', - value: 1 + text: "Organisation Name", + value: 1, }, { key: 2, - text: 'Category', - value: 2 + text: "Category", + value: 2, }, { key: 3, - text: 'Topic', - value: 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 [new_array,setNew_array]=useState([]) - let [counter,setCounter]=useState(0) + 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 () { - setSearch(''); + resetSearchState() { + setSearch(""); setFilter(0); setAutoComplete(null); setSuggestions([]); - } + }, })); const handleFilter = (unNeccesaryThing, e) => { - setFilter(e.value) - } - - const handleSearch = e => { - e && e.preventDefault() - document.activeElement.blur() - buildSearchList(_inputHandle.current.value, filter) - } - const focusHandler = e => { - setCounter(0) + setFilter(e.value); + }; + + const handleSearch = (e) => { + e && e.preventDefault(); + document.activeElement.blur(); + buildSearchList(_inputHandle.current.value, filter); + }; + const focusHandler = (e) => { + setsearchCounter(0); // setSuggestions(autoComplete.suggest(search)) setTimeout(() => { - setIsInputInFocus(document.activeElement === inputElement) + setIsInputInFocus(document.activeElement === inputElement); }, 0); - } + }; useEffect(() => { - document.body.addEventListener('click', focusHandler) - inputElement = document.getElementById('inputBox') + document.body.addEventListener("click", focusHandler); + inputElement = document.getElementById("inputBox"); return () => { - document.body.removeEventListener('click', focusHandler) - } - }, []) + document.body.removeEventListener("click", focusHandler); + }; + }, []); useEffect(() => { - let list = [] + let list = []; if (filter === 0) { let dataSet = new Set(); - data.forEach(e => { - dataSet.add(...e.tech) - }) - list = [...[...dataSet].sort((a, b) => (a - b))] + data.forEach((e) => { + dataSet.add(...e.tech); + }); + 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) { let dataSet = new Set(); - data.forEach(e => { - dataSet.add(...e.top) - }) - list = [...[...dataSet].sort((a, b) => (a - b))] + data.forEach((e) => { + dataSet.add(...e.top); + }); + list = [...[...dataSet].sort((a, b) => a - b)]; } - setAutoComplete(new AutoComplete(list)) - setSuggestions([]) - setSearch('') - }, [filter]) + setAutoComplete(new AutoComplete(list)); + setSuggestions([]); + setSearch(""); + }, [filter]); useEffect(() => { - autoComplete && setSuggestions(autoComplete.suggest('')) - }, [autoComplete]) + autoComplete && setSuggestions(autoComplete.suggest("")); + }, [autoComplete]); useEffect(() => { - 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] + 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]; } - setCounter(counter++) + setsearchCounter(searchCounter++); } } - 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] + 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(()=>{ -_searchBox.current.addEventListener('keydown',func) -return ()=>{ - _searchBox.current.removeEventListener('keydown',func) -} -},[new_array]) + useEffect(() => { + _searchBoxHandle.current.addEventListener("keydown", func); + return () => { + _searchBoxHandle.current.removeEventListener("keydown", func); + }; + }, [suggestionCopy]); -useEffect(()=>{ - setCounter(0) - setNew_array(suggestions.map(el=>el)) -},[suggestions]) + useEffect(() => { + setsearchCounter(0); + setsuggestionCopy(suggestions.map((el) => el)); + }, [suggestions]); return ( - + - Date: Wed, 15 Dec 2021 12:43:40 +0530 Subject: [PATCH 4/7] Update AdvancedSearch.js --- src/components/AdvancedSearch.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/components/AdvancedSearch.js b/src/components/AdvancedSearch.js index c711391..c7f742c 100644 --- a/src/components/AdvancedSearch.js +++ b/src/components/AdvancedSearch.js @@ -176,9 +176,9 @@ const AdvancedSearch = forwardRef((props, ref) => { }, [suggestions]); return ( - -
-