From 4d19b67197a8507aeb6125020020d7467286c7bb Mon Sep 17 00:00:00 2001 From: rdunk Date: Fri, 12 Jan 2024 15:52:57 +0000 Subject: [PATCH] feat: add configurable row type --- README.md | 32 ++++++++++++++----- src/index.ts | 87 +++++++++++++++++++++++++++------------------------- 2 files changed, 70 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 690029d..c7cf86e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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. diff --git a/src/index.ts b/src/index.ts index 5ceb0c7..09fe088 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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(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: {