Skip to content

Commit

Permalink
feat: add configurable row type
Browse files Browse the repository at this point in the history
  • Loading branch information
rdunk committed Jan 12, 2024
1 parent 1ac9515 commit 4d19b67
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 49 deletions.
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ Only the v3 version is maintained by Sanity.io.
## Acknowledgements

Big thanks to the original contributors for their work!
* Original version: [rdunk/sanity-plugin-table](https://github.com/rdunk/sanity-plugin-table).
* Further improvements in fork [MathisBullinger/sanity-plugin-another-table](https://github.com/MathisBullinger/sanity-plugin-another-table).
* Initial V3 port: [bitfo/sanity-plugin-table](https://github.com/bitfo/sanity-plugin-table)

- Original version: [rdunk/sanity-plugin-table](https://github.com/rdunk/sanity-plugin-table).
- Further improvements in fork [MathisBullinger/sanity-plugin-another-table](https://github.com/MathisBullinger/sanity-plugin-another-table).
- Initial V3 port: [bitfo/sanity-plugin-table](https://github.com/bitfo/sanity-plugin-table)

## Disclaimer

Sometimes a table is just what you need.
However, before using the Table plugin, consider if there are other ways to model your data that are:
* easier to edit and validate
* easier to query

Approaching your schemas in a more structured manner can often pay dividends down the line.
- easier to edit and validate
- easier to query

Approaching your schemas in a more structured manner can often pay dividends down the line.

## Install

Expand Down Expand Up @@ -73,6 +75,22 @@ export default defineConfig({
});
```

## Configuration

You can optionally configure the `_type` used for the row object in the table schema by passing a `rowType` when adding the plugin. For most users this is unnecessary, but it can be useful if you are migrating from a legacy table plugin.

```js
export default defineConfig({
// ...
plugins: [
table({
rowType: 'my-custom-row-type',
}),
],
// ...
});
```

## License

[MIT](LICENSE) © ʞunp ʇɹǝdnɹ, Mathis Bullinger, Dave Lucia and Sanity.io
Expand All @@ -87,7 +105,7 @@ on how to run this plugin with hotreload in the studio.

### Release new version

Run ["CI & Release" workflow](https://github.com/sanity-io/sanity-plugin-table/actions/workflows/main.yml).
Run ["CI & Release" workflow](https://github.com/sanity-io/table/actions/workflows/main.yml).
Make sure to select the main branch and check "Release new version".

Semantic release will only release on configured branches, so it is safe to run release on any branch.
87 changes: 45 additions & 42 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,55 @@ export type {

export { TableComponent, TablePreview };

const tableRowSchema = defineType({
title: 'Table Row',
name: 'tableRow',
type: 'object',
fields: [
{
name: 'cells',
type: 'array',
of: [{ type: 'string' }],
},
],
});
export interface TableConfig {
rowType?: string;
}

export const table = definePlugin<TableConfig | undefined>(config => {
const tableRowSchema = defineType({
title: 'Table Row',
name: config?.rowType || 'tableRow',
type: 'object',
fields: [
{
name: 'cells',
type: 'array',
of: [{ type: 'string' }],
},
],
});

const tableSchema = defineType({
title: 'Table',
name: 'table',
type: 'object',
fields: [
{
name: 'rows',
type: 'array',
of: [
{
type: tableRowSchema.name,
},
],
const tableSchema = defineType({
title: 'Table',
name: 'table',
type: 'object',
fields: [
{
name: 'rows',
type: 'array',
of: [
{
type: tableRowSchema.name,
},
],
},
],
components: {
input: TableComponent as any,
preview: TablePreview as any,
},
],
components: {
//TODO remove as any when rc.3 is released
input: TableComponent as any,
preview: TablePreview as any,
},
preview: {
select: {
rows: 'rows',
title: 'title',
preview: {
select: {
rows: 'rows',
title: 'title',
},
prepare: ({ title, rows = [] }) => ({
title,
rows,
}),
},
prepare: ({ title, rows = [] }) => ({
title,
rows,
}),
},
});
});

export const table = definePlugin(() => {
return {
name: 'table',
schema: {
Expand Down

0 comments on commit 4d19b67

Please sign in to comment.