|
| 1 | +import * as React from 'react'; |
| 2 | +import { |
| 3 | + DataGridPremium, |
| 4 | + GridApiPremium, |
| 5 | + gridFocusCellSelector, |
| 6 | + gridSortedRowIdsSelector, |
| 7 | + useGridApiRef, |
| 8 | +} from '@mui/x-data-grid-premium'; |
| 9 | +import type { RefObject } from '@mui/x-internals/types'; |
| 10 | +import { useDemoData } from '@mui/x-data-grid-generator'; |
| 11 | +import Button from '@mui/material/Button'; |
| 12 | +import Paper from '@mui/material/Paper'; |
| 13 | +import Stack from '@mui/material/Stack'; |
| 14 | +import Typography from '@mui/material/Typography'; |
| 15 | + |
| 16 | +// Mirrors the priority used for the Ctrl+C shortcut: selected cells, then |
| 17 | +// selected rows, then the focused cell. It's built entirely from public APIs, |
| 18 | +// so it can be triggered from anywhere, including a button or a touch device. |
| 19 | +function getSelectionAsText(apiRef: RefObject<GridApiPremium | null>): string { |
| 20 | + const api = apiRef.current; |
| 21 | + if (!api) { |
| 22 | + return ''; |
| 23 | + } |
| 24 | + |
| 25 | + const selectedCells = api.getSelectedCellsAsArray(); |
| 26 | + if (selectedCells.length > 1) { |
| 27 | + const cellSelectionModel = api.getCellSelectionModel(); |
| 28 | + const sortedRowIds = gridSortedRowIdsSelector({ current: api }).filter( |
| 29 | + (id) => cellSelectionModel[id], |
| 30 | + ); |
| 31 | + return sortedRowIds |
| 32 | + .map((rowId) => |
| 33 | + Object.keys(cellSelectionModel[rowId]) |
| 34 | + .map((field) => |
| 35 | + cellSelectionModel[rowId][field] |
| 36 | + ? String(api.getCellParams(rowId, field).formattedValue) |
| 37 | + : '', |
| 38 | + ) |
| 39 | + .join('\t'), |
| 40 | + ) |
| 41 | + .join('\n'); |
| 42 | + } |
| 43 | + |
| 44 | + const selectedRowIds = api.getSelectedRows(); |
| 45 | + if (selectedRowIds.size > 0) { |
| 46 | + return api.getDataAsCsv({ |
| 47 | + includeHeaders: false, |
| 48 | + shouldAppendQuotes: false, |
| 49 | + escapeFormulas: false, |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + const focusedCell = gridFocusCellSelector({ current: api }); |
| 54 | + if (focusedCell) { |
| 55 | + return String( |
| 56 | + api.getCellParams(focusedCell.id, focusedCell.field).formattedValue, |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + return ''; |
| 61 | +} |
| 62 | + |
| 63 | +export default function ClipboardCopyButton() { |
| 64 | + const apiRef = useGridApiRef(); |
| 65 | + const { data } = useDemoData({ |
| 66 | + dataSet: 'Commodity', |
| 67 | + rowLength: 10, |
| 68 | + maxColumns: 6, |
| 69 | + }); |
| 70 | + const [copiedText, setCopiedText] = React.useState(''); |
| 71 | + |
| 72 | + const handleCopy = () => { |
| 73 | + const text = getSelectionAsText(apiRef); |
| 74 | + navigator.clipboard.writeText(text); |
| 75 | + setCopiedText(text); |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <Stack sx={{ width: '100%' }} spacing={1}> |
| 80 | + <Button |
| 81 | + sx={{ alignSelf: 'flex-start' }} |
| 82 | + variant="outlined" |
| 83 | + onClick={handleCopy} |
| 84 | + > |
| 85 | + Copy selection |
| 86 | + </Button> |
| 87 | + <div style={{ height: 400 }}> |
| 88 | + <DataGridPremium apiRef={apiRef} checkboxSelection cellSelection {...data} /> |
| 89 | + </div> |
| 90 | + <Typography variant="body2" color="text.secondary"> |
| 91 | + Clipboard content: |
| 92 | + </Typography> |
| 93 | + <Paper variant="outlined" sx={{ p: 1.5, minHeight: 48 }}> |
| 94 | + <Typography |
| 95 | + component="pre" |
| 96 | + variant="body2" |
| 97 | + sx={{ |
| 98 | + m: 0, |
| 99 | + fontFamily: 'monospace', |
| 100 | + whiteSpace: 'pre-wrap', |
| 101 | + wordBreak: 'break-all', |
| 102 | + }} |
| 103 | + > |
| 104 | + {copiedText || ( |
| 105 | + <Typography component="span" color="text.disabled" variant="body2"> |
| 106 | + Nothing copied yet |
| 107 | + </Typography> |
| 108 | + )} |
| 109 | + </Typography> |
| 110 | + </Paper> |
| 111 | + </Stack> |
| 112 | + ); |
| 113 | +} |
0 commit comments