react-spreadsheet

repository·master·Indexed 23 days ago

https://github.com/iddan/react-spreadsheet

A lightweight, customizable, and performant spreadsheet component for React applications. It follows a 'Just Components™' philosophy and provides primary components including Spreadsheet, DataEditor, and DataViewer. The library includes a Matrix type for data representation, utilities for CSV import/export, and a comprehensive selection system for managing cell ranges, rows, and columns.

Tokens
5.8K
Snippets
15
Records
43
Agent score
77%

What's inside react-spreadsheet

  1. Overview of react-spreadsheet

    master
    react-spreadsheet is a simple, customizable, and performant spreadsheet component designed for React applications. It follows the 'Just Components™' philosophy, providing a straightforward API that focuses on common use cases while maintaining flexibility. Note that while it is performant, it is not virtualized.
  2. Understand the react-spreadsheet project structure

    master

    The library is organized into several key areas:

    • src/index.ts: The main entry point that exports the public API.
    • src/Spreadsheet.tsx: The primary component used to render the spreadsheet.
    • src/engine: The engine responsible for spreadsheet formula evaluation.
    • src/stories/: Contains Storybook stories for interactive testing.
    • website/: Contains the Docusaurus-based website code and documentation.
  3. How state management works in react-spreadsheet

    master

    The spreadsheet state is managed using a centralized reducer pattern:

    1. Reducer & Actions: State is managed in src/reducer.ts and actions are defined in src/actions.ts.
    2. Context: The state is distributed via React Context (defined in src/context.ts).
    3. Performance: The library uses use-context-selector to allow components to select only the specific slices of state they need, which prevents unnecessary re-renders across the spreadsheet.
  4. Understand the core data structures in react-spreadsheet

    master

    The library relies on several key data structures to manage spreadsheet state and logic:

    • Matrix: A 2D matrix of cells representing the spreadsheet data.
    • PointRange: Represents a specific range within the spreadsheet.
    • Selection: Represents a rectangular selection of cells.
    • CellBase: Represents an individual cell.
    • Model: Represents the spreadsheet data combined with evaluated formulae cells.
    • PointSet: A set of points used specifically for formulae evaluation.
  5. Customize spreadsheet components

    master
    The Spreadsheet component is designed to be highly customizable. You can override internal components (for example, src/Table.tsx) with your own implementations. To ensure stability when overriding components, the props for these components are defined in src/types.ts, providing a well-defined API for custom components.
  6. Implement a controlled Spreadsheet with onChange

    master

    To persist user changes and react to data modifications (such as validation or persistence), use the onChange prop. The onChange callback is triggered every time a cell is changed.

    In a controlled setup, you should manage the spreadsheet data in your component's state and pass the state setter to onChange.

    import { useState } from "react";
    import Spreadsheet from "react-spreadsheet";
    
    const App = () => {
      const [data, setData] = useState([
        [{ value: "Vanilla" }, { value: "Chocolate" }, { value: "" }],
        [{ value: "Strawberry" }, { value: "Cookies" }, { value: "" }],
      ]);
      return <Spreadsheet data={data} onChange={setData} />;
    };
  7. Deploy the website to GitHub Pages

    master

    You can deploy the website to the gh-pages branch using the yarn deploy command. You must provide your GitHub username via the GIT_USER environment variable. If using SSH for deployment, set USE_SSH=true.

    $ GIT_USER=<Your GitHub username> USE_SSH=true yarn deploy