9
9
MoreHoriz ,
10
10
MoreVert ,
11
11
} from '@mui/icons-material' ;
12
- import React from 'react' ;
12
+ import React , { useEffect , useRef } from 'react' ;
13
13
14
14
const useLayerPaginationStyles = makeStyles ( {
15
15
ulVertical : {
@@ -49,6 +49,8 @@ type LayerPaginationProps = {
49
49
orientation : LayerPaginationOrientation ;
50
50
} ;
51
51
52
+ const LAYER_PAGINATION_WHEEL_TIMEOUT = 500 ;
53
+
52
54
export default function LayerPagination ( props : LayerPaginationProps ) {
53
55
const StyledBadge = withStyles ( ( ) => ( {
54
56
badge : {
@@ -66,6 +68,41 @@ export default function LayerPagination(props: LayerPaginationProps) {
66
68
props . onClickPage ( page ) ;
67
69
} ,
68
70
} ) ;
71
+
72
+ const pageRefs : Map < number , React . RefObject < HTMLDivElement > > = new Map ( ) ;
73
+ for ( const item of items ) {
74
+ if ( item . type === 'page' && item . page !== null ) {
75
+ pageRefs . set ( item . page , useRef < HTMLDivElement > ( null ) ) ;
76
+ }
77
+ }
78
+
79
+ useEffect ( ( ) => {
80
+ let processing = false ;
81
+ const handleWheel = ( event : WheelEvent ) => {
82
+ event . preventDefault ( ) ;
83
+ if ( processing ) {
84
+ return ;
85
+ }
86
+ processing = true ;
87
+ if ( event . deltaY < 0 ) {
88
+ props . onClickPage ( Math . min ( props . page ! + 1 , props . count ) ) ;
89
+ } else if ( event . deltaY > 0 ) {
90
+ props . onClickPage ( Math . max ( 1 , props . page ! - 1 ) ) ;
91
+ }
92
+ setTimeout ( ( ) => {
93
+ processing = false ;
94
+ } , LAYER_PAGINATION_WHEEL_TIMEOUT ) ;
95
+ } ;
96
+ for ( const ref of pageRefs . values ( ) ) {
97
+ ref . current ?. addEventListener ( 'wheel' , handleWheel , { passive : false } ) ;
98
+ }
99
+ return ( ) => {
100
+ for ( const ref of pageRefs . values ( ) ) {
101
+ ref . current ?. removeEventListener ( 'wheel' , handleWheel ) ;
102
+ }
103
+ } ;
104
+ } ) ;
105
+
69
106
return (
70
107
< nav >
71
108
< ul
@@ -90,10 +127,11 @@ export default function LayerPagination(props: LayerPaginationProps) {
90
127
label = { page - 1 }
91
128
color = { selected ? 'primary' : undefined }
92
129
clickable = { ! selected }
93
- onClick = { ( ) => {
130
+ onClick = { ( ) : void => {
94
131
props . onClickPage ( page ) ;
95
132
} }
96
133
className = { selected ? '' : classes . unselected }
134
+ ref = { pageRefs . get ( page ) ! }
97
135
/>
98
136
</ StyledBadge >
99
137
) ;
0 commit comments