Skip to content

Commit

Permalink
support row type configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rdunk committed Mar 16, 2021
1 parent e7f1c99 commit a0e087e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
44 changes: 34 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
# sanity-plugin-table

[Sanity](https://www.sanity.io/) plugin that implements a `table` schema type and input component.
<div align="center">
<h1>Sanity Plugin Table</h1>
<p>
<img alt="NPM version" src="https://img.shields.io/npm/v/sanity-plugin-table?color=000&style=flat-square">
<img alt="NPM downloads" src="https://img.shields.io/npm/dm/sanity-plugin-table?color=000&style=flat-square">
<img alt="GitHub Release Date" src="https://img.shields.io/github/release-date/rdunk/sanity-plugin-table?color=000&style=flat-square">
<img alt="License" src="https://img.shields.io/npm/l/sanity-plugin-table.svg?color=000&style=flat-square">
</p>
</p>
<p>
<h3><a href="https://www.sanity.io/" _target="blank">Sanity</a> plugin that implements a table schema type and input component.</h3>
<br>
<br>
</div>

> **Notice**: Version 2.x of this plugin includes a breaking change, see the [Configuration](#configuration) section to migrate existing data.
<br>

![example](https://user-images.githubusercontent.com/8467307/48703530-e369be00-ebeb-11e8-8299-14812461aee8.gif)

## Installing
## Install

Install using the [Sanity CLI](https://www.sanity.io/docs/cli).

```
sanity install table
```bash
$ sanity install table
```

## Usage
Expand All @@ -25,14 +40,23 @@ export default {
{
name: 'sizeChart',
title: 'Size Chart',
type: 'table', // Specify table type
}
type: 'table', // Specify 'table' type
},
],
};
```

## Similar Packages
* [sanity-datatable](https://github.com/fredjens/sanity-datatable/) by [fredjens](https://github.com/fredjens/)
## Configuration

You can create a configuration file to provide the `_type` used for table row data. This is especially useful if you are upgrading or have existing data, as version `2.x` of this plugin uses `tableRow` by default, replacing `row` used in version `1.x`.

In your studio, create a `config/table.json` file (relative to the root directory). The `rowType` string value can be whatever you like. Use `row` if you are migrating existing data from version 1 to 2.

```json
{
"rowType": "row"
}
```

## License

Expand Down
2 changes: 1 addition & 1 deletion config.dist.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"rowName": "tableRow"
"rowType": "tableRow"
}
4 changes: 2 additions & 2 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class TableInput extends React.Component {
// Add a single row with a single empty cell (1 row, 1 column)
const newValue = {
rows: [{
_type: config.rowName,
_type: config.rowType,
_key: uuid(),
cells: [''],
}],
Expand All @@ -56,7 +56,7 @@ export default class TableInput extends React.Component {
const columnCount = value.rows[0].cells.length;
// Add as many cells as we have columns
newValue.rows.push({
_type: config.rowName,
_type: config.rowType,
_key: uuid(),
cells: Array(columnCount).fill(''),
});
Expand Down
2 changes: 1 addition & 1 deletion src/schema/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import config from 'config:table'

export default {
title: 'Table Row',
name: config.rowName,
name: config.rowType,
type: 'object',
fields: [
{
Expand Down
2 changes: 1 addition & 1 deletion src/schema/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
type: 'array',
of: [
{
type: config.rowName,
type: config.rowType,
},
],
},
Expand Down

0 comments on commit a0e087e

Please sign in to comment.