image.png

    1. import { useState } from 'react'
    2. import ReactDataGrid, {
    3. SelectColumn,
    4. textEditor
    5. } from 'react-data-grid';
    6. import 'react-data-grid/lib/styles.css';
    7. // import type { Column, CopyEvent, FillEvent, PasteEvent } from 'react-data-grid';
    8. function rowKeyGetter(row) {
    9. return row.id;
    10. }
    11. const columns = [
    12. SelectColumn,
    13. { key: "id", name: "ID", editable: true },
    14. {
    15. key: "title",
    16. name: "名称",
    17. resizable: true,
    18. // frozen: true,
    19. // 编辑单元格 textEditor { row, column, onRowChange, onClose }
    20. renderEditCell: textEditor
    21. },
    22. {
    23. key: "complete",
    24. name: "Complete",
    25. renderEditCell: textEditor,
    26. width: 'max-content',
    27. }
    28. ];
    29. const initialRows = [
    30. { id: 0, title: "Task 1", complete: 340 },
    31. { id: 1, title: "List 2", complete: 40 },
    32. { id: 2, title: "Card 3", complete: 60 }
    33. ];
    34. function GridTable() {
    35. const [rows, setRows] = useState(initialRows);
    36. const [selectedRows, setSelectedRows] = useState((): ReadonlySet<string> => new Set());
    37. return (
    38. <ReactDataGrid
    39. columns={columns}
    40. rows={rows}
    41. rowKeyGetter={rowKeyGetter}
    42. selectedRows={selectedRows}
    43. onSelectedRowsChange={setSelectedRows}
    44. onRowsChange={setRows}
    45. onCellClick={(row, data) => console.log({ row, data })}
    46. // 缩放 column { resizable: true }
    47. onColumnResize={(idx, width) =>
    48. console.log(`Column ${idx} has been resized to ${width}`)
    49. }
    50. />
    51. );
    52. }
    53. export default GridTable