Skip to main content

React-Datasheet

A simple react component to create a spreadsheet.

Demo here: https://nadbm.github.io/react-datasheet/

Examples are located in https://github.com/nadbm/react-datasheet/tree/master/docs/src/examples

Current features:

  • Select cells, cut, copy and paste cells
  • Navigation using keyboard keys
  • Deletion using keyboard keys
  • Callbacks for onCellsChanged, valueRenderer(visible data)
  • dataRenderer(underlying data in the input, takes the value by default)
  • Supply your own editors and view controls with custom renderers
  • Extensive control over generated markup via custom renderers

Using Typescript? View Usage

Installation

Install from npm:

$ npm install react-datasheet --save

Import in your project:

import ReactDataSheet from 'react-datasheet';
// Be sure to include styles at some point, probably during your bootstrapping
import 'react-datasheet/lib/react-datasheet.css';

Usage

React-Datasheet generates a table with the cells. Double-clicking or typing edits the value and if changed, initiates an onCellsChanged callback. Pasting tabular data or deleting a range of cells also calls onCellsChanged.

The data provided should be an array of rows, and each row should include the cells.

Basic Usage

class App extends React.Component {
constructor(props) {
super(props);
this.state = {
grid: [
[{ value: 1 }, { value: 3 }],
[{ value: 2 }, { value: 4 }],
],
};
}
render() {
return (
<ReactDataSheet
data={this.state.grid}
valueRenderer={cell => cell.value}
onCellsChanged={changes => {
const grid = this.state.grid.map(row => [...row]);
changes.forEach(({ cell, row, col, value }) => {
grid[row][col] = { ...grid[row][col], value };
});
this.setState({ grid });
}}
/>
);
}
}

Cells with underlying data

There are two values that each cell shows. The first is via valueRenderer and the second is via dataRenderer. When a cell is in edit mode, it will show the value returned from dataRenderer. It needs to return a string as this value is set in an input field. Each of these callbacks are passed the cell value as well as the cell's coordinates in the spreadsheet. This allows you to apply formatting logic at rendering time, such as all cells in the third column should be formatted as dates.

const grid = [
[{value: 5, expr: '1 + 4'}, {value: 6, expr: '6'}, {value: new Date('2008-04-10')}],
[{value: 5, expr: '1 + 4'}, {value: 5, expr: '1 + 4'}, {value: new Date('2004-05-28')}]
]
const onCellsChanged = (changes) => changes.forEach(({cell, row, col, value}) => console.log("New expression :" + value))
<ReactDataSheet
data={grid}
valueRenderer={(cell, i, j) => j == 2 ? cell.value.toDateString() : cell.value}
dataRenderer={(cell, i, j) => j == 2 ? cell.value.toISOString() : cell.expr}
onCellsChanged={onCellsChanged}
/>

Cells with underlying component

const grid = [
[{
value: 5,
component: (
<button onClick={() => console.log("clicked")}>
Rendered
</button>
)
}]
]
<ReactDataSheet
data={grid}
valueRenderer={(cell) => cell.value}
/>

This renders a single cell with the value 5. Once in edit mode, the button will appear.

Cells with extra attributes

const grid = [
[{value: 1, hint: 'Valid'}, {value: 3, hint: 'Not valid'}],
[{value: 2}, {value: 4}]
]
<ReactDataSheet
data={grid}
valueRenderer={(cell) => cell.value}
attributesRenderer={(cell) => (cell.hint ? { 'data-hint': cell.hint } : {})}
...
/>

This render 2 rows, each one with two cells, the cells in the first row will have an attribute data-hint and the other 2 will not.

Custom renderers

React-Datasheet allows you replace the renderers both for the overall structure (rows, cells, the sheet itself) as well as editors and viewers for individual cells. This allows you to radically refashion the sheet to suit your requirements.

For example, this shows how to add separate headers and a checkbox at the start of each row to control row "selected" state. It also specifies a custom view renderer and a custom editor for the first column of each row:

Note: For brevity, in this example the custom renderers are all defined as arrow functions inside of render, but using a bound function in the parent component or a separate custom component will let you avoid a lot of needless re-renders.

Options

OptionTypeDescription
dataArrayArray of rows and each row should contain the cell objects to display
valueRendererfuncMethod to render the value of the cell function(cell, i, j). This is visible by default
dataRendererfuncMethod to render the underlying value of the cell function(cell, i, j). This data is visible once in edit mode.
overflow'wrap'|'nowrap'|'clip'Grid default for how to render overflow text in cells
onCellsChangedfunconCellsChanged handler: function(arrayOfChanges[, arrayOfAdditions]) {}, where changes is an array of objects of the shape {cell, row, col, value}. See below for more details.
onContextMenufuncContext menu handler : function(event, cell, i, j)
parsePastefuncfunction (string) {} If set, the function will be called with the raw clipboard data. It should return an array of arrays of strings. This is useful for when the clipboard may have data with irregular field or line delimiters. If not set, rows will be split with line breaks and cells with tabs.
isCellNavigablefuncfunction (cell, row, col) {return true} If set, the function is used to determine whether navigation to the indicated cell should be allowed or not. If not then using cursor or tab navigation will skip over not allowed cells until it finds the next allowed cell.
handleCopyfuncfunction ({ event, dataRenderer, valueRenderer, data, start, end, range }) If set, this function is called whenever the user copies cells. The return string of this function is stored on the clipboard.

Advanced options

The following are optional functions or React Component that can completely override the native renderers of react datasheet. To know which props are passed down, see custom renderers

OptionTypeDescription
sheetRendererfuncOptional function or React Component to render the main sheet element. The default renders a table element.
rowRendererfuncOptional function or React Component to render each row element. The default renders a tr element.
cellRendererfuncOptional function or React Component to render each cell element. The default renders a td element.
valueViewerfuncOptional function or React Component to customize the way the value for each cell in the sheet is displayed. Affects every cell in the sheet. See cell options to override individual cells.
dataEditorfuncOptional function or React Component to render a custom editor. Affects every cell in the sheet. See cell options to override individual cells.
selectedobjectOptional. Whether the selection is controlled or uncontrolled. Must be an object of this format: { start: { i: number, j; number }, end: { i: number, j: number } }, or null for no selection.
onSelectfuncOptional. function ({ start, end }) {} Triggered on every selection change. start and end have the same format as the selected prop.

onCellsChanged(arrayOfChanges[, arrayOfAdditions]) handler

React-DataSheet will call this callback whenever data in the grid changes:

  • When the user enters a new value in a cell
  • When the user hits the delete key with one or more selected cells
  • When the user pastes tabular data into the table

The argument to the callback usually will be one array of objects with these properties:

PropertyTypeDescription
cellobjectthe original cell object you provided in the data property. This may be null (see below)
rownumberrow index of changed cell
colnumbercolumn index of changed cell
valueanyThe new cell value. This is usually a string, but a custom editor may provide any type of value.

If the change is the result of a user edit, the array will contain a single change object. If the user pastes data or deletes a range of cells, the array will contain an element for each affected cell.

Additions: If the user pastes data that extends beyond the bounds of the grid (for example, pasting two-row-high data on the last line), there will be a second argument to the handler containing an array of objects that represent the out-of-bounds data. These object will have the same properties, except:

  • There is no cell property
  • either row or col, or both, will be outside the bounds of your original grid. They will correspond to the indices the new data would occupy if you expanded your grid to hold them.

You can choose to ignore the additions, or you can expand your model to accommodate the new data.

Deprecated handlers

Previously React-DataSheet supported two change handlers. These are still supported for backwards compatibility, but will be removed at some point in the future.

OptionTypeDescription
onChangefunconChange handler: function(cell, i, j, newValue) {}
onPastefunconPaste handler: function(array) {} If set, the function will be called with an array of rows. Each row has an array of objects containing the cell and raw pasted value. If the pasted value cannot be matched with a cell, the cell value will be undefined.

Cell Options

The cell object is what gets passed back to the onChange callback. They can contain the following options as well

OptionTypeDefaultDescription
readOnlyBoolfalseCell will never go in edit mode
keyStringundefinedBy default, each cell is given the key of col number and row number. This would override that key
classNameStringundefinedAdditional class names for cells.
componentReactElementundefinedInsert a react element or JSX to this field. This will render on edit mode
forceComponentboolfalseRenders what's in component at all times, even when not in edit mode
disableEventsboolfalseMakes cell unselectable and read only
colSpannumber1The colSpan of the cell's td element
rowSpannumber1The rowSpan of the cell's td element
widthnumber or StringundefinedSets the cell's td width using a style attribute. Number is interpreted as pixels, strings are used as-is. Note: This will only work if the table does not have a set width.
overflow'wrap'|'nowrap'| 'clip'undefinedHow to render overflow text. Overrides grid-level overflow option.
valueViewerfuncundefinedOptional function or React Component to customize the way the value for this cell is displayed. Overrides grid-level valueViewer option.
dataEditorfuncundefinedOptional function or React Component to render a custom editor. Overrides grid-level dataEditor option.

Custom Renderers

Each of the following custom renderers should be either a React Component or a function that takes a props argument and returns a react element (a.k.a stateless functional component). React-DataSheet will supply certain properties to each renderer.

In some cases React-DataSheet will include event handlers as properties to your custom renderer. You must hook up these handlers to your component or aspects of React-DataSheet's built-in behavior will cease to work.

Except for valueViewer and dataEditor, each custom renderer will receive react's regular props.children. Be sure to render {props.children} in your custom renderer.

Sheet Renderer

The sheetRenderer is responsible for laying out the sheet's main parent component. By default, React-DataSheet uses a table element. React-DataSheet will supply these properties:

OptionTypeDescription
dataArrayThe same data array as from main ReactDataSheet component
classNameStringClasses to apply to your top-level element. You can add to these, but your should not overwrite or omit them unless you want to implement your own CSS also.
childrenArray or componentThe regular react props.children. You must render {props.children} within your custom renderer or you won't see your rows and cells.

Row Renderer

The rowRenderer lays out each row in the sheet. By default, React-DataSheet uses a tr element. React-DataSheet will supply these properties:

OptionTypeDescription
rownumberThe current row index
selectedBooltrue in case the current row is selected
cellsArrayThe cells in the current row
childrenArray or componentThe regular react props.children. You must render {props.children} within your custom renderer or you won't see your cells.

Cell Renderer

The cellRenderer creates the container for each cell in the sheet. The default renders a td element. React-DataSheet will supply these properties:

OptionTypeDescription
rownumberThe current row index
colnumberThe current column index
cellObjectThe cell's raw data structure
classNameStringClasses to apply to your cell element. You can add to these, but your should not overwrite or omit them unless you want to implement your own CSS also.
styleObjectGenerated styles that you should apply to your cell element. This may be null or undefined.
selectedBoolIs the cell currently selected
editingBoolIs the cell currently being edited
updatedBoolWas the cell recently updated
attributesRendererfuncAs for the main ReactDataSheet component
onMouseDownfuncEvent handler important for cell selection behavior
onMouseOverfuncEvent handler important for cell selection behavior
onDoubleClickfuncEvent handler important for editing
onContextMenufuncEvent handler to launch default content-menu handling. You can safely ignore this handler if you want to provide your own content menu handling.
childrenArray or componentThe regular react props.children. You must render {props.children} within your custom renderer or you won't your cell's data.

Value Viewer

The valueViewer displays your cell's data with a custom component when in view mode. For example, you might show a "three star rating" component instead the number 3. You can specify a valueViewer for the entire sheet and/or for an individual cell.

React-DataSheet will supply these properties:

OptionTypeDescription
valuenodeThe result of the valueRenderer function
rownumberThe current row index
colnumberThe current column index
cellObjectThe cell's raw data structure

Data Editor

The dataEditor displays your cell's data when in edit mode. You can can use any component you want, as long as you hook up the event handlers that constitute the contract between React-DataSheet and your editor. You can specify a dataEditor for the entire sheet and/or for an individual cell.

OptionTypeDescription
valueString or nodeThe result of the dataRenderer (or valueRenderer if none)
rownumberThe current row index
colnumberThe current column index
cellObjectThe cell's raw data structure
onChangefuncfunction (string) {} callback for when the user changes the value during editing (for example, each time they type a character into an input). onChange does not indicate the final edited value. It works just like a controlled component in a form.
onKeyDownfuncfunction (event) {} An event handler that you can call to use default React-DataSheet keyboard handling to signal reverting an ongoing edit (Escape key) or completing an edit (Enter or Tab). For most editors based on an input element this will probably work. However, if this keyboard handling is unsuitable for your editor you can trigger these changes explicitly using the onCommit and onRevert callbacks.
onCommitfuncfunction (newValue, [event]) {} A callback to indicate that editing is over, here is the final value. If you pass a KeyboardEvent as the second argument, React-DataSheet will perform default navigation for you (for example, going down to the next row if you hit the enter key). You actually don't need to use onCommit if the default keyboard handling is good enough for you.
onRevertfuncfunction () {} A no-args callback that you can use to indicate that you want to cancel ongoing edits. As with onCommit, you don't need to worry about this if the default keyboard handling works for your editor.