Skip to content

Commit

Permalink
fix: 주용이 의견 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
rtttr1 committed Nov 20, 2024
1 parent d553507 commit 39f5c8a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 43 deletions.
22 changes: 18 additions & 4 deletions src/common/hook/useMultiSelect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';

type DataHasIdKey<T> = {
[key in keyof T]: T[key];
Expand All @@ -10,6 +10,7 @@ type DataHasIdKey<T> = {
*/
export const useMultiSelect = <T extends object>(identifier: keyof T, data: DataHasIdKey<T>[]) => {
const [ids, setIds] = useState<number[]>([]);
const [canSelect, setCanSelect] = useState(false);

const handleItemClick = (id: number) => {
if (ids.includes(id)) {
Expand All @@ -35,9 +36,22 @@ export const useMultiSelect = <T extends object>(identifier: keyof T, data: Data
}
};

const handleReset = () => {
setIds([]);
useEffect(() => {
const handleKeyPress = (e: KeyboardEvent) => {
if (e.key === 'Escape' && canSelect) {
setCanSelect(false);

setIds([]);
}
};
window.addEventListener('keydown', handleKeyPress);

return () => window.removeEventListener('keydown', handleKeyPress);
}, [canSelect]);

const handleCanSelect = () => {
setCanSelect(!canSelect);
};

return { ids, handleItemClick, handleAllClick, handleReset };
return { ids, canSelect, handleItemClick, handleAllClick, handleCanSelect };
};
21 changes: 2 additions & 19 deletions src/page/deleted/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { useEffect, useState } from 'react';

import Button from '@/common/component/Button/Button';
import Flex from '@/common/component/Flex/Flex';
import Select from '@/common/component/Select/Select';
Expand Down Expand Up @@ -27,22 +25,7 @@ const tmpData: File[] = [

const DeletedPage = () => {
const { isOpen, toggle } = useOverlay();
const { ids, handleItemClick, handleAllClick, handleReset } = useMultiSelect<File>('fileId', tmpData);

const [canSelect, setCanSelect] = useState(false);

useEffect(() => {
const handleKeyPress = (e: KeyboardEvent) => {
if (e.key === 'Escape' && canSelect) {
setCanSelect(false);

handleReset();
}
};
window.addEventListener('keydown', handleKeyPress);

return () => window.removeEventListener('keydown', handleKeyPress);
}, [canSelect, handleReset]);
const { ids, canSelect, handleItemClick, handleAllClick, handleCanSelect } = useMultiSelect<File>('fileId', tmpData);

return (
<ContentBox
Expand All @@ -61,7 +44,7 @@ const DeletedPage = () => {
<Button variant="tertiary">영구삭제</Button>
</Flex>
) : (
<Button onClick={() => setCanSelect(true)} variant="tertiary">
<Button onClick={handleCanSelect} variant="tertiary">
선택
</Button>
)}
Expand Down
23 changes: 3 additions & 20 deletions src/page/handover/HandoverPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useState } from 'react';

import IcSearch from '@/common/asset/svg/ic_search.svg?react';
import Button from '@/common/component/Button/Button';
Expand All @@ -16,34 +16,17 @@ import { FILTER_OPTION, NOTE_DUMMY } from '@/page/handover/constant/noteList';
import ContentBox from '@/shared/component/ContentBox/ContentBox';

const HandoverPage = () => {
const [canSelect, setCanSelect] = useState(false);
const [sortOption, setSortOption] = useState('');
const [searchValue, setSearchValue] = useState('');

const { isOpen, close, toggle } = useOverlay();
const ref = useOutsideClick<HTMLDivElement>(close);
const { ids, handleItemClick, handleAllClick, handleReset } = useMultiSelect<(typeof NOTE_DUMMY)[0]>(

const { ids, canSelect, handleItemClick, handleAllClick, handleCanSelect } = useMultiSelect<(typeof NOTE_DUMMY)[0]>(
'id',
NOTE_DUMMY
);

useEffect(() => {
const handleKeyPress = (e: KeyboardEvent) => {
if (e.key === 'Escape' && canSelect) {
setCanSelect(false);

handleReset();
}
};
window.addEventListener('keydown', handleKeyPress);

return () => window.removeEventListener('keydown', handleKeyPress);
}, [canSelect, handleReset]);

const handleCanSelect = () => {
setCanSelect(!canSelect);
};

const handleSortOption = (id: string) => {
setSortOption(id);

Expand Down

0 comments on commit 39f5c8a

Please sign in to comment.