Skip to content

Commit 4ea5ec7

Browse files
committed
refactor: 불필요한 React import 제거 및 빌드오류 해결
1 parent ae2be31 commit 4ea5ec7

File tree

5 files changed

+33
-46
lines changed

5 files changed

+33
-46
lines changed

src/shared/components/datePicker/CalendarDropdownAdapter.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as React from 'react';
21
import { CustomDropdown } from './CustomDropdown';
32
import type { DropdownProps } from 'react-day-picker';
43

@@ -11,7 +10,7 @@ export function CalendarDropdownAdapter({
1110
return (
1211
<CustomDropdown
1312
value={value as number}
14-
options={options as any}
13+
options={options ?? []}
1514
aria-label={ariaLabel}
1615
onChange={(next) => {
1716
const evt = {

src/shared/components/datePicker/CustomDropdown.tsx

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,75 @@
1-
import * as React from 'react';
21
import { cn } from '@/shared/lib';
2+
import { useCallback, useEffect, useRef, useState } from 'react';
33
import type { DropdownOption } from 'react-day-picker';
44

55
export interface CustomDropdownProps {
66
value?: number;
77
onChange: (next: number) => void;
88
options: DropdownOption[];
99
'aria-label'?: string;
10+
className?: string;
1011
}
1112

1213
export function CustomDropdown({
1314
value,
1415
onChange,
1516
options,
1617
'aria-label': ariaLabel,
18+
className,
1719
}: CustomDropdownProps) {
18-
const [open, setOpen] = React.useState(false);
19-
const buttonRef = React.useRef<HTMLButtonElement>(null);
20-
const listRef = React.useRef<HTMLUListElement>(null);
20+
const [open, setOpen] = useState(false);
21+
const buttonRef = useRef<HTMLButtonElement>(null);
22+
const listRef = useRef<HTMLUListElement>(null);
2123

22-
const selectedIndex = Math.max(
23-
0,
24-
options.findIndex((o) => o.value === value),
25-
);
26-
const [activeIndex, setActiveIndex] = React.useState(
27-
selectedIndex === -1 ? 0 : selectedIndex,
28-
);
24+
const calcSelectedIndex = useCallback(() => {
25+
const i = options.findIndex((o) => o.value === value);
26+
return i >= 0 ? i : 0;
27+
}, [options, value]);
2928

30-
React.useEffect(() => {
31-
if (selectedIndex >= 0) {
32-
setActiveIndex(selectedIndex);
33-
} else if (options.length > 0) {
34-
setActiveIndex(0);
35-
} else {
36-
setActiveIndex(0);
37-
}
38-
}, [selectedIndex, options.length]);
29+
const [activeIndex, setActiveIndex] = useState<number>(calcSelectedIndex());
30+
31+
useEffect(() => {
32+
setActiveIndex(calcSelectedIndex());
33+
}, [calcSelectedIndex]);
3934

4035
const label =
4136
options.find((o) => o.value === value)?.label ?? options[0]?.label ?? '';
4237

43-
React.useEffect(() => {
38+
useEffect(() => {
4439
if (!open) return;
4540

4641
const handler = (e: Event) => {
47-
const t = e.target as Node;
42+
const t = (e as Event).target as Node | null;
43+
if (!t) return;
4844
if (!buttonRef.current?.contains(t) && !listRef.current?.contains(t)) {
4945
setOpen(false);
5046
}
5147
};
5248

53-
const hasPointer =
49+
const usePointer =
5450
typeof window !== 'undefined' && 'PointerEvent' in window;
5551

56-
if (hasPointer) {
57-
document.addEventListener('pointerdown', handler as EventListener, {
58-
capture: true,
59-
});
52+
if (usePointer) {
53+
document.addEventListener('pointerdown', handler as EventListener, true);
6054
return () =>
6155
document.removeEventListener(
6256
'pointerdown',
6357
handler as EventListener,
64-
{ capture: true } as any,
58+
true,
6559
);
6660
} else {
67-
document.addEventListener('mousedown', handler as EventListener, {
68-
capture: true,
69-
});
70-
document.addEventListener('touchstart', handler as EventListener, {
71-
capture: true,
72-
});
61+
document.addEventListener('mousedown', handler as EventListener, true);
62+
document.addEventListener('touchstart', handler as EventListener, true);
7363
return () => {
7464
document.removeEventListener(
7565
'mousedown',
7666
handler as EventListener,
77-
{ capture: true } as any,
67+
true,
7868
);
7969
document.removeEventListener(
8070
'touchstart',
8171
handler as EventListener,
82-
{ capture: true } as any,
72+
true,
8373
);
8474
};
8575
}
@@ -122,7 +112,7 @@ export function CustomDropdown({
122112
};
123113

124114
return (
125-
<div className='relative inline-block'>
115+
<div className={cn('relative inline-block', className)}>
126116
<button
127117
ref={buttonRef}
128118
type='button'

src/shared/components/datePicker/PopoverContent.client.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use client';
2-
3-
import * as React from 'react';
42
import * as PopoverPrimitive from '@radix-ui/react-popover';
53
import { cn } from '@/shared/lib';
4+
import { forwardRef } from 'react';
65

7-
export const PopoverContent = React.forwardRef<
6+
export const PopoverContent = forwardRef<
87
React.ElementRef<typeof PopoverPrimitive.Content>,
98
React.ComponentPropsWithoutRef<typeof PopoverPrimitive.Content>
109
>(function PopoverContent(

src/shared/components/datePicker/calendar.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import * as React from 'react';
21
import { DayPicker, DayButton } from 'react-day-picker';
32
import { cn } from '@/shared/lib';
43
import { Button } from './button';
54
import { Icon } from '@/shared/icons';
65
import { createCalendarClassNames } from './calendar.classes';
76
import { CalendarDropdownAdapter } from './CalendarDropdownAdapter';
7+
import { useEffect, useRef } from 'react';
88

99
function Calendar({
1010
className,
@@ -60,8 +60,8 @@ function CalendarDayButton({
6060
modifiers,
6161
...props
6262
}: React.ComponentProps<typeof DayButton>) {
63-
const ref = React.useRef<HTMLButtonElement>(null);
64-
React.useEffect(() => {
63+
const ref = useRef<HTMLButtonElement>(null);
64+
useEffect(() => {
6565
if (modifiers.focused) ref.current?.focus();
6666
}, [modifiers.focused]);
6767

src/shared/components/datePicker/popover.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as React from 'react';
21
import * as PopoverPrimitive from '@radix-ui/react-popover';
32
import dynamic from 'next/dynamic';
43

0 commit comments

Comments
 (0)