Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/MOVIEJOJO7/cat-talk into fe…
Browse files Browse the repository at this point in the history
…ature/#1
  • Loading branch information
LikeFireAndSky committed Nov 10, 2023
2 parents f91319a + e808990 commit cac90d6
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions Components/Search/SearchOpenChat.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
'use client';

import React, { useState } from 'react';
import React, { useState, useCallback } from 'react';
import { AllOpenChat } from '@/app/search/search.type';
import ShowAllOpenChat from './ShowAllOpenChat';

const SearchOpenChat = ({ allOpenChat }: { allOpenChat: AllOpenChat }) => {
const [userInput, setUserInput] = useState('');
const [openChats] = useState<AllOpenChat>(allOpenChat);
const [searched, setSearched] = useState<AllOpenChat>(allOpenChat);

const getUserInput = (e: React.ChangeEvent<HTMLInputElement>) => {
setUserInput(e.target.value.toLowerCase().replace(/(\s*)/g, ''));
};

const searched = openChats.filter((item) =>
item.name.toLowerCase().replace(/(\s*)/g, '').includes(userInput),
);
const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
search();
}
};

const search = useCallback(() => {
const result = allOpenChat.filter((item) =>
item.name.toLowerCase().replace(/(\s*)/g, '').includes(userInput),
);
setSearched(result);
}, [userInput, allOpenChat]);

return (
<>
<input onChange={getUserInput} />
{searched.map((item) => (
<ShowAllOpenChat key={item.id} openChat={item} />
))}
<input onChange={getUserInput} onKeyPress={handleKeyPress} />
{searched.length ? (
searched.map((item) => (
<ShowAllOpenChat key={item.id} openChat={item} />
))
) : (
<h1>검색 결과가 없습니다.</h1>
)}
</>
);
};
Expand Down

0 comments on commit cac90d6

Please sign in to comment.