1
1
import React , { useRef , useCallback , useState } from 'react' ;
2
-
3
2
import { eventMap } from '@testing-library/dom/dist/event-map' ;
3
+ import { ChevronUpIcon , ChevronDownIcon } from '@primer/octicons-react' ;
4
4
import throttle from 'lodash.throttle' ;
5
5
import AutoSizer from 'react-virtualized-auto-sizer' ;
6
6
import { TrashcanIcon } from '@primer/octicons-react' ;
@@ -87,7 +87,7 @@ function addLoggingEvents(node, log) {
87
87
}
88
88
89
89
function EventRecord ( { index, style, data } ) {
90
- const { id, event , target } = data [ index ] ;
90
+ const { id, type , name , element , selector } = data [ index ] ;
91
91
92
92
return (
93
93
< div
@@ -98,31 +98,47 @@ function EventRecord({ index, style, data }) {
98
98
>
99
99
< div className = "p-2 flex-none w-16" > { id } </ div >
100
100
101
- < div className = "p-2 flex-none w-32" > { event . EventType } </ div >
102
- < div className = "p-2 flex-none w-32" > { event . name } </ div >
101
+ < div className = "p-2 flex-none w-32" > { type } </ div >
102
+ < div className = "p-2 flex-none w-32" > { name } </ div >
103
103
104
- < div className = "p-2 flex-none w-40" > { target . tagName } </ div >
105
- < div className = "p-2 flex-auto whitespace-no-wrap" >
106
- { target . toString ( ) }
107
- </ div >
104
+ < div className = "p-2 flex-none w-40" > { element } </ div >
105
+ < div className = "p-2 flex-auto whitespace-no-wrap" > { selector } </ div >
108
106
</ div >
109
107
) ;
110
108
}
111
109
112
- const noop = ( ) => { } ;
113
110
function DomEvents ( ) {
111
+ const buffer = useRef ( [ ] ) ;
112
+ const previewRef = useRef ( ) ;
113
+ const listRef = useRef ( ) ;
114
+
115
+ const sortDirection = useRef ( 'asc' ) ;
116
+ const [ appendMode , setAppendMode ] = useState ( 'bottom' ) ;
114
117
const [ { markup, result } , dispatch ] = usePlayground ( {
115
118
onChange : onStateChange ,
116
119
...initialValues ,
117
120
} ) ;
118
121
119
- const buffer = useRef ( [ ] ) ;
120
- const previewRef = useRef ( ) ;
121
- const listRef = useRef ( ) ;
122
-
123
122
const [ eventCount , setEventCount ] = useState ( 0 ) ;
124
123
const [ eventListeners , setEventListeners ] = useState ( [ ] ) ;
125
124
125
+ const getSortIcon = ( ) => (
126
+ < IconButton >
127
+ { sortDirection . current === 'desc' ? (
128
+ < ChevronDownIcon />
129
+ ) : (
130
+ < ChevronUpIcon />
131
+ ) }
132
+ </ IconButton >
133
+ ) ;
134
+
135
+ const changeSortDirection = ( ) => {
136
+ const newDirection = sortDirection . current === 'desc' ? 'asc' : 'desc' ;
137
+ buffer . current = buffer . current . reverse ( ) ;
138
+ setAppendMode ( newDirection === 'desc' ? 'top' : 'bottom' ) ;
139
+ sortDirection . current = newDirection ;
140
+ } ;
141
+
126
142
const reset = ( ) => {
127
143
buffer . current = [ ] ;
128
144
setEventCount ( 0 ) ;
@@ -144,8 +160,19 @@ function DomEvents() {
144
160
if ( node ) {
145
161
previewRef . current = node ;
146
162
const eventListeners = addLoggingEvents ( node , ( event ) => {
147
- event . id = buffer . current . length ;
148
- buffer . current . push ( event ) ;
163
+ const log = {
164
+ id : buffer . current . length + 1 ,
165
+ type : event . event . EventType ,
166
+ name : event . event . name ,
167
+ element : event . target . tagName ,
168
+ selector : event . target . toString ( ) ,
169
+ } ;
170
+ if ( sortDirection . current === 'desc' ) {
171
+ buffer . current . splice ( 0 , 0 , log ) ;
172
+ } else {
173
+ buffer . current . push ( log ) ;
174
+ }
175
+
149
176
setTimeout ( flush , 0 ) ;
150
177
} ) ;
151
178
setEventListeners ( eventListeners ) ;
@@ -170,7 +197,7 @@ function DomEvents() {
170
197
markup = { markup }
171
198
elements = { result . elements }
172
199
accessibleRoles = { result . accessibleRoles }
173
- dispatch = { noop }
200
+ dispatch = { dispatch }
174
201
variant = "minimal"
175
202
/>
176
203
</ div >
@@ -181,12 +208,17 @@ function DomEvents() {
181
208
< div className = "editor p-4 md:h-56 flex-auto overflow-hidden" >
182
209
< div className = "h-56 md:h-full w-full flex flex-col" >
183
210
< div className = "h-8 flex items-center w-full text-sm font-bold" >
184
- < div className = "p-2 w-16" > #</ div >
211
+ < div
212
+ className = "p-2 w-16 cursor-pointer flex justify-between items-center"
213
+ onClick = { changeSortDirection }
214
+ >
215
+ # { getSortIcon ( ) }
216
+ </ div >
185
217
186
- < div className = "p-2 w-32" > type</ div >
187
- < div className = "p-2 w-32" > name</ div >
218
+ < div className = "p-2 w-32 " > type</ div >
219
+ < div className = "p-2 w-32 " > name</ div >
188
220
189
- < div className = "p-2 w-40" > element</ div >
221
+ < div className = "p-2 w-40 " > element</ div >
190
222
< div className = "flex-auto p-2 flex justify-between" >
191
223
< span > selector</ span >
192
224
< div >
@@ -203,15 +235,15 @@ function DomEvents() {
203
235
</ div >
204
236
205
237
< div className = "flex-auto relative overflow-hidden" >
206
- { eventCount === 0 ? (
238
+ { buffer . current . length === 0 ? (
207
239
< div className = "flex w-full h-full opacity-50 items-end justify-center" >
208
240
< EmptyStreetImg height = "80%" />
209
241
</ div >
210
242
) : (
211
243
< AutoSizer >
212
244
{ ( { width, height } ) => (
213
245
< StickyList
214
- mode = "bottom"
246
+ mode = { appendMode }
215
247
ref = { listRef }
216
248
height = { height }
217
249
itemCount = { eventCount }
0 commit comments