Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
[FE] fix: category icon click 버그 수정 (#234)
Browse files Browse the repository at this point in the history
* fix: 아이콘 클릭 에러 해결

* refactor: category input 비제어 컴포넌트로 수정
  • Loading branch information
jeonjeunghoon authored Aug 11, 2023
1 parent 7f44998 commit e1fd86b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
1 change: 1 addition & 0 deletions frontend/src/assets/icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export { ReactComponent as DeleteIcon } from './delete.svg';
export { ReactComponent as PencilIcon } from './pencil.svg';
export { ReactComponent as WritingIcon } from './writing.svg';
export { ReactComponent as ImportIcon } from './import.svg';
export { ReactComponent as PlusIcon } from './plus.svg';
1 change: 1 addition & 0 deletions frontend/src/assets/icons/plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 13 additions & 15 deletions frontend/src/components/Category/Category/Category.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ type Props = {

const Category = ({ categoryId, categoryName, isDefaultCategory }: Props) => {
const {
value,
inputRef,
handleOnChange,
escapeInput: escapeRename,
isInputOpen,
setIsInputOpen,
openInput,
resetInput,
isError,
setIsError,
Expand All @@ -31,22 +29,21 @@ const Category = ({ categoryId, categoryName, isDefaultCategory }: Props) => {
const requestChangedName: KeyboardEventHandler<HTMLInputElement> = (e) => {
if (e.key !== 'Enter') return;

if (!isValidCategoryName(value)) {
const categoryName = e.currentTarget.value.trim();

if (!isValidCategoryName(categoryName)) {
setIsError(true);
return;
}

resetInput();
patchCategory({
categoryId,
body: {
categoryName: value.trim(),
categoryName,
},
});
};

const openRenamingInput: MouseEventHandler<SVGSVGElement> = (e) => {
setIsInputOpen(true);
resetInput();
};

return (
Expand All @@ -57,11 +54,9 @@ const Category = ({ categoryId, categoryName, isDefaultCategory }: Props) => {
variant='underlined'
size='small'
placeholder='Change category name ...'
value={value}
ref={inputRef}
isError={isError}
onBlur={resetInput}
onChange={handleOnChange}
onKeyDown={escapeRename}
onKeyUp={requestChangedName}
/>
Expand All @@ -75,11 +70,14 @@ const Category = ({ categoryId, categoryName, isDefaultCategory }: Props) => {
</S.CategoryButton>
{!isDefaultCategory && (
<S.IconContainer>
<S.Button aria-label={`${categoryName} 카테고리 이름 수정`}>
<PencilIcon onClick={openRenamingInput} width={12} height={12} />
<S.Button aria-label={`${categoryName} 카테고리 이름 수정`} onClick={openInput}>
<PencilIcon width={12} height={12} />
</S.Button>
<S.Button aria-label={`${categoryName} 카테고리 삭제`}>
<DeleteIcon onClick={() => deleteCategory(categoryId)} width={12} height={12} />
<S.Button
aria-label={`${categoryName} 카테고리 삭제`}
onClick={() => deleteCategory(categoryId)}
>
<DeleteIcon width={12} height={12} />
</S.Button>
</S.IconContainer>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Accordion from 'components/@common/Accordion/Accordion';
import { styled } from 'styled-components';
import { PlusCircleIcon } from 'assets/icons';
import { PlusIcon } from 'assets/icons';
import { KeyboardEventHandler } from 'react';
import useCategoryInput from '../useCategoryInput';
import Category from '../Category/Category';
Expand All @@ -13,12 +13,10 @@ import { isValidCategoryName } from '../isValidCategoryName';
const CategorySection = () => {
const { categoryDetails, setSelectedCategoryId } = useCategoryDetails();
const {
value,
inputRef,
handleOnChange,
escapeInput: escapeAddCategory,
isInputOpen,
setIsInputOpen,
openInput,
resetInput,
isError,
setIsError,
Expand All @@ -28,7 +26,7 @@ const CategorySection = () => {
const requestAddCategory: KeyboardEventHandler<HTMLInputElement> = async (e) => {
if (e.key !== 'Enter') return;

const categoryName = value.trim();
const categoryName = e.currentTarget.value.trim();

if (!isValidCategoryName(categoryName)) {
setIsError(true);
Expand All @@ -51,17 +49,15 @@ const CategorySection = () => {
variant='underlined'
size='small'
placeholder='Add category ...'
value={value}
ref={inputRef}
isError={isError}
onBlur={resetInput}
onChange={handleOnChange}
onKeyDown={escapeAddCategory}
onKeyUp={requestAddCategory}
/>
) : (
<S.Button onClick={() => setIsInputOpen(true)} aria-label='카테고리 추가 입력 창 열기'>
<PlusCircleIcon width={12} height={12} />
<S.Button aria-label='카테고리 추가 입력 창 열기' onClick={openInput}>
<PlusIcon width={12} height={12} />
</S.Button>
)}
</S.Header>
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/Category/useCategoryInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const useCategoryInput = (initialValue: string) => {

const handleOnChange: ChangeEventHandler<HTMLInputElement> = (e) => setValue(e.target.value);

const openInput = () => setIsInputOpen(true);

const resetInput = () => {
setIsError(false);
setIsInputOpen(false);
Expand All @@ -32,7 +34,7 @@ const useCategoryInput = (initialValue: string) => {
handleOnChange,
escapeInput,
isInputOpen,
setIsInputOpen,
openInput,
resetInput,
isError,
setIsError,
Expand Down

0 comments on commit e1fd86b

Please sign in to comment.