Skip to content

Commit

Permalink
✨ 서치바 빈칸 막기
Browse files Browse the repository at this point in the history
  • Loading branch information
SeoYeonii committed Aug 13, 2023
1 parent dce4d27 commit b7c5536
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/pages/Home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
ChangeEvent,
ReactElement,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import { ChangeEvent, ReactElement, useEffect, useRef, useState } from "react";

import { observer } from "mobx-react-lite";

Expand Down Expand Up @@ -141,22 +134,27 @@ const Home = (): ReactElement => {
const { mainStore } = useStore();
const navigate = useNavigate();
const [value, setValue] = useState("");
const [isError, setIsError] = useState(false);
const [errorMsg, setErrorMsg] = useState("");

const handleTextFieldChange = useCallback(
(e: ChangeEvent<HTMLTextAreaElement>) => {
setValue(e.target.value);
},
[],
);
const handleTextFieldChange = (e: ChangeEvent<HTMLTextAreaElement>) => {
if (isError) {
setIsError(false);
setErrorMsg("");
}
setValue(e.target.value);
};

const handleClickSearchButton = useCallback(
(v: string) => {
const handleClickSearchButton = (v: string) => {
if (value.length === 0 || value === "") {
setErrorMsg("검색어는 빈칸 일 수 없어요.");
setIsError(true);
} else {
mainStore.resetSearchWords();
mainStore.addSearchWord(v);
navigate("/search");
},
[navigate],
);
}
};

useEffect(() => {
const wheelHandler = (e: WheelEvent) => {
Expand Down Expand Up @@ -231,6 +229,8 @@ const Home = (): ReactElement => {
<Box className="bottom-area">
<Box className="search-area">
<SearchBarWithButton
isError={isError}
errorMsg={errorMsg}
value={value}
onChange={handleTextFieldChange}
placeholder="키워드 또는 사건번호 입력"
Expand Down
8 changes: 8 additions & 0 deletions src/pages/components/SearchBarWithButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const StyledDiv = styled.div`
gap: 10px;
.MuiIconButton-root {
width: 40px;
height: 40px;
background-color: rgba(255, 188, 16, 1);
color: #ffffff;
box-shadow: 2px 2px 4px 0px rgba(255, 126, 32, 0.25);
Expand Down Expand Up @@ -50,6 +52,8 @@ interface Props {
placeholder?: string;
onClick: (inputValue: string) => void;
hasClear?: boolean;
isError?: boolean;
errorMsg?: string;
}

const SearchBarWithButton = ({
Expand All @@ -59,10 +63,14 @@ const SearchBarWithButton = ({
placeholder = "",
onClick,
hasClear = false,
isError = false,
errorMsg = "",
}: Props): ReactElement => {
return (
<StyledDiv>
<TextField
error={isError}
helperText={errorMsg}
fullWidth
size="small"
value={value}
Expand Down

0 comments on commit b7c5536

Please sign in to comment.