Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-datasheet-grid

Package Overview
Dependencies
Maintainers
1
Versions
84
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-datasheet-grid

Made with create-react-library

  • 1.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16K
decreased by-3.29%
Maintainers
1
Weekly downloads
 
Created
Source

react-datasheet-grid

NPM JavaScript Style Guide

View Demo and examples

An Airtable-like / Excel-like component to create spreadsheets.

Preview

Feature rich:

  • Dead simple to setup and to use
  • Supports copy / pasting to and from Excel, Google-sheet...
  • Keyboard navigation and shortcuts fully-supported
  • Supports custom widgets
  • Blazing fast, optimized for speed
  • Smooth animations
  • Virtualized, supports hundreds of thousands of rows
  • Extensively customizable, controllable behaviors

Install

npm install --save react-datasheet-grid

Usage

import {
  DataSheetGrid,
  checkboxColumn,
  textColumn,
} from 'react-datasheet-grid'
import 'react-datasheet-grid/dist/index.css'

const Example = () => {
  const [ data, setData ] = useState([
    { active: true, firstName: 'Elon', lastName: 'Musk' },
    { active: false, firstName: 'Jeff', lastName: 'Bezos' },
  ])

  const columns = [
    checkboxColumn({ title: 'Active', key: 'active' }),
    textColumn({ title: 'First name', key: 'firstName' }),
    textColumn({ title: 'Last name', key: 'lastName' }),
  ]

  return (
    <DataSheetGrid
      data={data}
      onChange={setData}
      columns={columns}
    />
  )
}

Columns based spreadsheet

react-datasheet-grid is more like Airtable or Notion and less like Excel in the sense that instead of dealing with individual cells it deals with entire rows, and each column is responsible for a single property of each row.

Columns support widgets likes checkboxes, selectors, money input, or any component you wish to implement. Each individual columns is responsible for formatting, typing, validation, and controlling any other behavior related to the property it handles.

Columns are simple objects:

const columns = [
  { title: 'Column A', width: 1, minWidth: 200, /*...*/ },
  { title: 'Column B', width: 1, minWidth: 200, /*...*/ },
]

This makes it terribly easy to extend and re-use existing columns:

const defaultSize = { width: 1, minWidth: 200 }
const columnA = { title: 'Column A', ...defaultSize, /*...*/ }

const columns = [
  columnA,
  { title: 'Column B', ...columnA },
]

For columns implementation examples got to src/columns.

All available properties are listed bellow.

API reference

Props

PropTypeDefaultMessage
dataany[][]List of rows. Elements are usually objects but can be anything.
onChange(data) => voidnullThis function is called when the data is updated.
columnsColumn[][]List of columns. Details
heightnumber400Maximum height of the grid in pixels. If the content is longer, the grid becomes scrollable.
rowHeightnumber40Height of a single row in pixels. All row have the same height.
headerRowHeightnumberrowHeightHeight of the header row in pixels.
gutterColumnWidthnumber | string'0 0 40px'Width of the gutter column. Accepts the same values as the width property of any column. Giving it a number will not give the gutter a fixed size, read the Column documentation.
createRow() => any() => ({})A function that should return a new row object. This function is called once per row every time the user appends or inserts new rows. Most often used to add default values and / or random ids to new rows.
duplicateRow({ rowData }) => any({ rowData }) => ({ ...rowData })A function that should return a new row object from an existing row. This function is called once per row every time the user duplicates rows. Most often used to override values and / or change uniq ids when duplicating rows.
isRowEmpty({ rowData }) => booleanA function that return true when one of the value of rowData is truthyUser can only delete empty rows. This function allows to customize what is considered an empty row. Most often used to ignore keys that are in the object but are not part of any column (eg. id).
autoAddRowbooleanfalseWhen true, a new row is added at the end of the grid when the user presses enter while editing a cell from the last row.
lockRowsbooleanfalseWhen true, prevents the user from adding or removing rows.
counterComponentA React component with props value and onChangeAdd X rowsUsed to replace the content of the "Add row" button for translations, custom inputs, icons... View default implementation.

Columns definition

For columns implementation examples got to src/columns.

KeyTypeDefaultMessage
titleReactNodenullElement to display in the header row.
widthstring | number1Width of the column, supports the same values as the CSS flex property. '0 0 <width>px' for fixed width of <width> pixels. <number> for responsive columns.
minWidthnumber | null100Minimum width of the column in pixels. Can be null for no minimum value.
maxWidthnumber | nullnullMaximum width of the column in pixels. Can be null for no maximum value.
disableKeysbooleanfalseUsually when the user is editing a cell, pressing the up, down, or return key will exit editing mode. Setting disableKeys to true will prevent this behavior. This is used when the widget needs to handle the up and down keys itself (eg. to increase the value, or select a choice). Usually the widget also needs to handle to return key by calling done.
disabledboolean | (({ rowData }) => boolean)falseDisable the entire column by passing true, or disable it for specific rows by passing a function.
deleteValue({ rowData }) => anyA function that deletes the column value of a row. Used when the user clears a cell or deletes a row.
copyValue({ rowData }) => number | string | nullA function that returns the value of the copied cell. If the user copies multiple cells at once, this function will be called multiple times. It can return a string, a number, or null, but it will always be turned into a string in the end.
pasteValue({ rowData, value }) => anyA function that takes in a row and the value to be paste, and should return the updated row. If the value should be ignored, it should still return the unchanged row. The value is always a string and should therefore be casted to whatever type is needed.
render({ rowData, rowIndex, ... }) => ReactNodeA function to render a cell. Details

Render function

This function is called everytime a cell needs to be rendered.

KeyTypeMessage
rowDataanyThe row object from which tha value of the cell should be extracted.
rowIndexnumberIndex of the row.
columnIndexnumberIndex of the column.
activebooleanTrue when the cell is active / highlighted.
focusbooleanTrue when the cell is focused / being edited.
setRowData(rowData) => voidFunction to call to update the row.
done({ nextRow = true }) => voidThis function can be called to exit edit mode manually. This is mainly used when disableKeys is true but it can have other use-cases. Optionally you can pass the nextRow parameter to false so the active / highlighted cell stays on the current cell.

License

MIT © nick-keller

FAQs

Package last updated on 01 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc