Skip to content

Commit 4d19b67

Browse files
committed
feat: add configurable row type
1 parent 1ac9515 commit 4d19b67

File tree

2 files changed

+70
-49
lines changed

2 files changed

+70
-49
lines changed

README.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ Only the v3 version is maintained by Sanity.io.
1111
## Acknowledgements
1212

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

1819
## Disclaimer
1920

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

25-
Approaching your schemas in a more structured manner can often pay dividends down the line.
24+
- easier to edit and validate
25+
- easier to query
26+
27+
Approaching your schemas in a more structured manner can often pay dividends down the line.
2628

2729
## Install
2830

@@ -73,6 +75,22 @@ export default defineConfig({
7375
});
7476
```
7577

78+
## Configuration
79+
80+
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.
81+
82+
```js
83+
export default defineConfig({
84+
// ...
85+
plugins: [
86+
table({
87+
rowType: 'my-custom-row-type',
88+
}),
89+
],
90+
// ...
91+
});
92+
```
93+
7694
## License
7795

7896
[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.
87105

88106
### Release new version
89107

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

93111
Semantic release will only release on configured branches, so it is safe to run release on any branch.

src/index.ts

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,55 @@ export type {
99

1010
export { TableComponent, TablePreview };
1111

12-
const tableRowSchema = defineType({
13-
title: 'Table Row',
14-
name: 'tableRow',
15-
type: 'object',
16-
fields: [
17-
{
18-
name: 'cells',
19-
type: 'array',
20-
of: [{ type: 'string' }],
21-
},
22-
],
23-
});
12+
export interface TableConfig {
13+
rowType?: string;
14+
}
15+
16+
export const table = definePlugin<TableConfig | undefined>(config => {
17+
const tableRowSchema = defineType({
18+
title: 'Table Row',
19+
name: config?.rowType || 'tableRow',
20+
type: 'object',
21+
fields: [
22+
{
23+
name: 'cells',
24+
type: 'array',
25+
of: [{ type: 'string' }],
26+
},
27+
],
28+
});
2429

25-
const tableSchema = defineType({
26-
title: 'Table',
27-
name: 'table',
28-
type: 'object',
29-
fields: [
30-
{
31-
name: 'rows',
32-
type: 'array',
33-
of: [
34-
{
35-
type: tableRowSchema.name,
36-
},
37-
],
30+
const tableSchema = defineType({
31+
title: 'Table',
32+
name: 'table',
33+
type: 'object',
34+
fields: [
35+
{
36+
name: 'rows',
37+
type: 'array',
38+
of: [
39+
{
40+
type: tableRowSchema.name,
41+
},
42+
],
43+
},
44+
],
45+
components: {
46+
input: TableComponent as any,
47+
preview: TablePreview as any,
3848
},
39-
],
40-
components: {
41-
//TODO remove as any when rc.3 is released
42-
input: TableComponent as any,
43-
preview: TablePreview as any,
44-
},
45-
preview: {
46-
select: {
47-
rows: 'rows',
48-
title: 'title',
49+
preview: {
50+
select: {
51+
rows: 'rows',
52+
title: 'title',
53+
},
54+
prepare: ({ title, rows = [] }) => ({
55+
title,
56+
rows,
57+
}),
4958
},
50-
prepare: ({ title, rows = [] }) => ({
51-
title,
52-
rows,
53-
}),
54-
},
55-
});
59+
});
5660

57-
export const table = definePlugin(() => {
5861
return {
5962
name: 'table',
6063
schema: {

0 commit comments

Comments
 (0)