최근 검색한 키워드를 SearchInput 아래에 표시되도록 만들고, 해당 영역에 표시된 특정 키워드를 누르면 그 키워드로 검색이 일어나도록 만듭니다. 단, 가장 최근에 검색한 5개의 키워드만 노출되도록 합니다.

페이지를 새로고침해도 마지막 검색 결과 화면이 유지되도록 처리합니다.

기능1) 최근 검색 키워드 표시 할 것 + 최근 검색 5개 키워드 노출

키워드히스토리(KeywordHistory) 컴포넌트 생성

Untitled

더미데이터 활용

아직 키워드 데이터가 없어서 나오는게 없을 것이다. 히스토리가 없는 ul태그가 있다. 최근 검색한 키워드를 채워넣어준다.

class KeywordHistory{
    $keywordHistory = null; // 엘리먼트 생성
    data = null;            // 데이터
    constructor({$target}){
        const $keywordHistory = document.createElement('ul'); 
        this.$keywordHistory = $keywordHistory;
        this.$keywordHistory.className = 'KeywordHistory';
        $target.appendChild(this.$keywordHistory);
        
        
        //아직 키워드 데이터가 없어서 나오는게 없다. 
        //더미데이터로 만들어서 노출시켜본다.
        this.data = [
            '아',
            '고양이',
            'cat'
        ];
        
        this.render();
    }

    render(){
        this.$keywordHistory.innerHTML = this.data.map( keyword =>  `<li><button>${keyword}</button></li>`  ).join('');
    }

   
}

기능2) 키워드 클릭 시 검색

더미데이터 활용 엘리먼트 만들기 + 클릭 시 키워드 데이터 뽑아오기.

console.log(this.$keywordHistory.querySelectorAll('li button'));
//결과
//NodeList(3) [button, button, button] 를 확인할 수 있다.
this.$keywordHistory.querySelectorAll('li button').forEach(($item, index) => {
    $item.addEventListener('click', () => {
        console.log($item); //각 노드를 확인할 수 있다.
    });
});
//예 $item 클릭 시 아래와 같이 나옴.
<button>고양이</button>
this.$keywordHistory.querySelectorAll('li button').forEach(($item, index) => {
    $item.addEventListener('click', () => {
        console.log($item,index,this.data[index])
    });
});