1
- import { useMemo , useState } from 'react' ;
2
- import { createPortal } from 'react-dom' ;
1
+ import { useMemo , useRef , useState } from 'react' ;
2
+ import { createPortal , flushSync } from 'react-dom' ;
3
3
import { faker } from '@faker-js/faker' ;
4
4
import { createLazyFileRoute } from '@tanstack/react-router' ;
5
5
import { css } from '@linaria/core' ;
@@ -9,6 +9,7 @@ import DataGrid, {
9
9
SelectColumn ,
10
10
textEditor ,
11
11
type Column ,
12
+ type DataGridHandle ,
12
13
type SortColumn
13
14
} from '../../src' ;
14
15
import { textEditorClassname } from '../../src/editors/textEditor' ;
@@ -310,6 +311,8 @@ function CommonFeatures() {
310
311
const [ rows , setRows ] = useState ( createRows ) ;
311
312
const [ sortColumns , setSortColumns ] = useState < readonly SortColumn [ ] > ( [ ] ) ;
312
313
const [ selectedRows , setSelectedRows ] = useState ( ( ) : ReadonlySet < number > => new Set ( ) ) ;
314
+ const [ isExporting , setIsExporting ] = useState ( false ) ;
315
+ const gridRef = useRef < DataGridHandle > ( null ) ;
313
316
314
317
const countries = useMemo ( ( ) : readonly string [ ] => {
315
318
return [ ...new Set ( rows . map ( ( r ) => r . country ) ) ] . sort ( new Intl . Collator ( ) . compare ) ;
@@ -342,61 +345,60 @@ function CommonFeatures() {
342
345
} ) ;
343
346
} , [ rows , sortColumns ] ) ;
344
347
345
- const gridElement = (
346
- < DataGrid
347
- rowKeyGetter = { rowKeyGetter }
348
- columns = { columns }
349
- rows = { sortedRows }
350
- defaultColumnOptions = { {
351
- sortable : true ,
352
- resizable : true
353
- } }
354
- selectedRows = { selectedRows }
355
- onSelectedRowsChange = { setSelectedRows }
356
- onRowsChange = { setRows }
357
- sortColumns = { sortColumns }
358
- onSortColumnsChange = { setSortColumns }
359
- topSummaryRows = { summaryRows }
360
- bottomSummaryRows = { summaryRows }
361
- className = "fill-grid"
362
- direction = { direction }
363
- />
364
- ) ;
348
+ function handleExportToCsv ( ) {
349
+ flushSync ( ( ) => {
350
+ setIsExporting ( true ) ;
351
+ } ) ;
352
+
353
+ exportToCsv ( gridRef . current ! . element ! , 'CommonFeatures.csv' ) ;
354
+
355
+ flushSync ( ( ) => {
356
+ setIsExporting ( false ) ;
357
+ } ) ;
358
+ }
359
+
360
+ async function handleExportToPdf ( ) {
361
+ flushSync ( ( ) => {
362
+ setIsExporting ( true ) ;
363
+ } ) ;
364
+
365
+ await exportToPdf ( gridRef . current ! . element ! , 'CommonFeatures.pdf' ) ;
366
+
367
+ flushSync ( ( ) => {
368
+ setIsExporting ( false ) ;
369
+ } ) ;
370
+ }
365
371
366
372
return (
367
373
< >
368
374
< div className = { toolbarClassname } >
369
- < ExportButton onExport = { ( ) => exportToCsv ( gridElement , 'CommonFeatures.csv' ) } >
375
+ < button type = "button" onClick = { handleExportToCsv } >
370
376
Export to CSV
371
- </ ExportButton >
372
- < ExportButton onExport = { ( ) => exportToPdf ( gridElement , 'CommonFeatures.pdf' ) } >
377
+ </ button >
378
+ < button type = "button" onClick = { handleExportToPdf } >
373
379
Export to PDF
374
- </ ExportButton >
380
+ </ button >
375
381
</ div >
376
- { gridElement }
382
+ < DataGrid
383
+ ref = { gridRef }
384
+ rowKeyGetter = { rowKeyGetter }
385
+ columns = { columns }
386
+ rows = { sortedRows }
387
+ defaultColumnOptions = { {
388
+ sortable : true ,
389
+ resizable : true
390
+ } }
391
+ selectedRows = { selectedRows }
392
+ onSelectedRowsChange = { setSelectedRows }
393
+ onRowsChange = { setRows }
394
+ sortColumns = { sortColumns }
395
+ onSortColumnsChange = { setSortColumns }
396
+ topSummaryRows = { summaryRows }
397
+ bottomSummaryRows = { summaryRows }
398
+ className = "fill-grid"
399
+ direction = { direction }
400
+ enableVirtualization = { ! isExporting }
401
+ />
377
402
</ >
378
403
) ;
379
404
}
380
-
381
- function ExportButton ( {
382
- onExport,
383
- children
384
- } : {
385
- onExport : ( ) => Promise < unknown > ;
386
- children : React . ReactNode ;
387
- } ) {
388
- const [ exporting , setExporting ] = useState ( false ) ;
389
- return (
390
- < button
391
- type = "button"
392
- disabled = { exporting }
393
- onClick = { async ( ) => {
394
- setExporting ( true ) ;
395
- await onExport ( ) ;
396
- setExporting ( false ) ;
397
- } }
398
- >
399
- { exporting ? 'Exporting' : children }
400
- </ button >
401
- ) ;
402
- }
0 commit comments