-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DataGrid] Allow to copy the selected rows to the clipboard (#1929)
- Loading branch information
Showing
15 changed files
with
286 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useGridClipboard'; |
91 changes: 91 additions & 0 deletions
91
packages/grid/_modules_/grid/hooks/features/clipboard/useGridClipboard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import * as React from 'react'; | ||
import { GridApiRef } from '../../../models/api/gridApiRef'; | ||
import { useGridApiEventHandler } from '../../root/useGridApiEventHandler'; | ||
import { GRID_KEYDOWN } from '../../../constants/eventsConstants'; | ||
import { buildCSV } from '../export/serializers/csvSerializer'; | ||
import { useGridSelector } from '../core/useGridSelector'; | ||
import { visibleGridColumnsSelector } from '../columns/gridColumnsSelector'; | ||
import { gridCheckboxSelectionColDef } from '../../../models/colDef'; | ||
import { GridClipboardApi } from '../../../models/api'; | ||
import { useGridApiMethod } from '../../root/useGridApiMethod'; | ||
|
||
function writeToClipboardPolyfill(data: string) { | ||
const span = document.createElement('span'); | ||
span.style.whiteSpace = 'pre'; | ||
span.style.userSelect = 'all'; | ||
span.style.opacity = '0px'; | ||
span.textContent = data; | ||
|
||
document.body.appendChild(span); | ||
|
||
const range = document.createRange(); | ||
range.selectNode(span); | ||
const selection = window.getSelection(); | ||
selection!.removeAllRanges(); | ||
selection!.addRange(range); | ||
|
||
try { | ||
document.execCommand('copy'); | ||
} finally { | ||
document.body.removeChild(span); | ||
} | ||
} | ||
|
||
export const useGridClipboard = (apiRef: GridApiRef): void => { | ||
const visibleColumns = useGridSelector(apiRef, visibleGridColumnsSelector); | ||
|
||
const copySelectedRowsToClipboard = React.useCallback( | ||
(includeHeaders = false) => { | ||
const selectedRows = apiRef.current.getSelectedRows(); | ||
const filteredColumns = visibleColumns.filter( | ||
(column) => column.field !== gridCheckboxSelectionColDef.field, | ||
); | ||
|
||
if (selectedRows.size === 0 || filteredColumns.length === 0) { | ||
return; | ||
} | ||
|
||
const data = buildCSV({ | ||
columns: visibleColumns, | ||
rows: selectedRows, | ||
includeHeaders, | ||
getCellParams: apiRef.current.getCellParams, | ||
delimiterCharacter: '\t', | ||
}); | ||
|
||
if (navigator.clipboard) { | ||
navigator.clipboard.writeText(data).catch(() => { | ||
writeToClipboardPolyfill(data); | ||
}); | ||
} else { | ||
writeToClipboardPolyfill(data); | ||
} | ||
}, | ||
[apiRef, visibleColumns], | ||
); | ||
|
||
const handleKeydown = React.useCallback( | ||
(event: KeyboardEvent) => { | ||
const isModifierKeyPressed = event.ctrlKey || event.metaKey || event.altKey; | ||
if (event.key.toLowerCase() !== 'c' || !isModifierKeyPressed) { | ||
return; | ||
} | ||
|
||
// Do nothing if there's a native selection | ||
if (window.getSelection()?.toString() !== '') { | ||
return; | ||
} | ||
|
||
apiRef.current.copySelectedRowsToClipboard(event.altKey); | ||
}, | ||
[apiRef], | ||
); | ||
|
||
useGridApiEventHandler(apiRef, GRID_KEYDOWN, handleKeydown); | ||
|
||
const clipboardApi: GridClipboardApi = { | ||
copySelectedRowsToClipboard, | ||
}; | ||
|
||
useGridApiMethod(apiRef, clipboardApi, 'GridClipboardApi'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/grid/_modules_/grid/models/api/gridClipboardApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* The Clipboard API interface that is available in the grid [[apiRef]]. | ||
*/ | ||
export interface GridClipboardApi { | ||
/** | ||
* Copies the selected rows to the clipboard. | ||
* The fields will separated by the TAB character. | ||
* @param {boolean} includeHeaders Whether to include the headers or not. Default is `false`. | ||
* @ignore - do not document. | ||
*/ | ||
copySelectedRowsToClipboard: (includeHeaders?: boolean) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.