11"use client" ;
22
3- import { useVirtualizer } from "@tanstack/react-virtual" ;
4-
53import clsx from "clsx" ;
6- import { useRef } from "react" ;
74
85import type { ListUniversity } from "@/types/university" ;
96import UniversityCard from "../../ui/UniverSityCard" ;
@@ -13,69 +10,16 @@ type UniversityCardsProps = {
1310 style ?: React . CSSProperties ;
1411 className ?: string ;
1512 showCapacity ?: boolean ;
16- enableVirtualization ?: boolean ;
1713} ;
1814
19- const ITEM_HEIGHT = 101 ;
20-
21- const UniversityCards = ( {
22- colleges,
23- style,
24- className,
25- showCapacity = true ,
26- enableVirtualization = true ,
27- } : UniversityCardsProps ) => {
28- // 훅은 항상 컴포넌트 상단에서 호출해야 함 (React Hooks 규칙)
29- const parentRef = useRef < HTMLDivElement > ( null ) ;
30-
31- const virtualizer = useVirtualizer ( {
32- count : colleges . length ,
33- getScrollElement : ( ) => parentRef . current ,
34- estimateSize : ( ) => ITEM_HEIGHT ,
35- overscan : 5 ,
36- } ) ;
37-
38- // 가상화가 비활성화된 경우 일반 렌더링
39- if ( ! enableVirtualization ) {
40- return (
41- < div className = { clsx ( "flex flex-col gap-2.5" , className ) } style = { style } >
42- { colleges . map ( ( college ) => (
43- < div key = { college . id } className = "pb-2.5" >
44- < UniversityCard university = { college } showCapacity = { showCapacity } />
45- </ div >
46- ) ) }
47- </ div >
48- ) ;
49- }
50-
51- // 가상화 사용
15+ const UniversityCards = ( { colleges, style, className, showCapacity = true } : UniversityCardsProps ) => {
5216 return (
53- < div ref = { parentRef } className = { clsx ( "h-[calc(100vh-200px)] overflow-auto" , className ) } style = { style } >
54- < div
55- style = { {
56- height : `${ virtualizer . getTotalSize ( ) } px` ,
57- width : "100%" ,
58- position : "relative" ,
59- } }
60- >
61- { virtualizer . getVirtualItems ( ) . map ( ( virtualItem ) => (
62- < div
63- key = { colleges [ virtualItem . index ] . id }
64- style = { {
65- position : "absolute" ,
66- top : 0 ,
67- left : 0 ,
68- width : "100%" ,
69- height : `${ virtualItem . size } px` ,
70- transform : `translateY(${ virtualItem . start } px)` ,
71- } }
72- >
73- < div className = "pb-2.5" >
74- < UniversityCard university = { colleges [ virtualItem . index ] } showCapacity = { showCapacity } />
75- </ div >
76- </ div >
77- ) ) }
78- </ div >
17+ < div className = { clsx ( "flex flex-col gap-2.5" , className ) } style = { style } >
18+ { colleges . map ( ( college ) => (
19+ < div key = { college . id } className = "pb-2.5" >
20+ < UniversityCard university = { college } showCapacity = { showCapacity } />
21+ </ div >
22+ ) ) }
7923 </ div >
8024 ) ;
8125} ;
0 commit comments