Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions src/components/category/CategoryModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ export default function CategoryModal({ onClose }: { onClose: () => void }) {
}, [fetchedCategories]);

const handlePlusClick = () => {
setIsCreating(prev => !prev);
// 모든 수정 중 상태 취소
setIsCreating(prev => {
const next = !prev;
if (next) {
setCategoryStates(prev => prev.map(state => ({ ...state, isEditing: false })));
}

return next;
});

setErrorMsg('');
setNewCategory('');
};
Expand Down Expand Up @@ -167,11 +176,9 @@ export default function CategoryModal({ onClose }: { onClose: () => void }) {
),
);
return;
} else if (categoryName === newCategoryName){
} else if (categoryName === newCategoryName) {
setCategoryStates(prev =>
prev.map((state, i) =>
i === index ? { ...state, isEditing: false } : state,
),
prev.map((state, i) => (i === index ? { ...state, isEditing: false } : state)),
);
return;
}
Expand All @@ -194,12 +201,17 @@ export default function CategoryModal({ onClose }: { onClose: () => void }) {
},
);
} else {
// isEditing이 false -> true로 바꾸고 수정 모드로 진입
// isEditing이 false -> 현재 index만 true로, 나머지는 false로 설정
setCategoryStates(prev =>
prev.map((state, i) =>
i === index ? { ...state, editValue: state.name, isEditing: true, error: '' } : state,
),
prev.map((state, i) => ({
...state,
editValue: i === index ? state.name : state.editValue,
isEditing: i === index ? true : false,
error: '',
})),
);
// 생성 중 상태 취소
setIsCreating(false);
}
};

Expand Down Expand Up @@ -241,7 +253,7 @@ export default function CategoryModal({ onClose }: { onClose: () => void }) {
}, [isCreating]);

const editingIdx = categoryStates.findIndex(state => state.isEditing);

useEffect(() => {
// editingIdx가 바뀔 때만 실행되도록 최적화
if (editingIdx !== -1) {
Expand Down Expand Up @@ -323,6 +335,7 @@ export default function CategoryModal({ onClose }: { onClose: () => void }) {
onChange={e => handleEditInputChange(idx, e.target.value)}
onKeyDown={e => handleCategoryModifyKeyDown(e, category.name, idx)}
className="w-[180px] border-b border-gray-300 focus:border-gray-500 focus:outline-none"
placeholder={category.name}
ref={el => {
editInputRef.current[idx] = el;
}}
Expand Down