block-content-to-react icon indicating copy to clipboard operation
block-content-to-react copied to clipboard

Question: Table within text

Open schlaup opened this issue 5 years ago • 2 comments

I am looking for some guidance. (I could not find a sample with this use case.)

I am looking to include table data (rows, columns) within text. (In my case it is a price list.)

Imagine a product page with like:

Our product comes in three different styles.

| Style  | Price |
|--------|-------|
| Small  | $10   |
| Medium | $20   |
| Large  | $30   |

More text...

My understanding is that Portable Text has the List type but not Table type, correct?

Could this be edited with Sanity Studio?

If not, I believe one solution would be to add a new type in Sanity like "ProductStyle" with "Name" and "Price" properties.

Could I reference this from Portable Text?

Something like this:

Our product comes in three different styles.

<REFERENCE PRICE TABLE HERE>

More text...

How would I extend "block-content-to-react" to render the table?

schlaup avatar Jul 02 '19 10:07 schlaup

Good question! Some of the use cases may be covered by employing the Table or Datatable Sanity plugins, but that does not help if you need to integrate a table between your text paragraphs. I guess one could do some trick in presentation to achieve something like this, but in my use case, where an editor just wants to add a small table between two random paragraphs... L.E. You can use the Table plugin and add the table type in your bodyPortableText. Then make a serializer for presentation and you're set. I'll be coming back once I have an example.

naz01101011 avatar Mar 29 '21 12:03 naz01101011

Also thinking about the same question now.... not an easy one....

logemann avatar Nov 03 '21 21:11 logemann

Found the solution today, it was there all along... create a PortableText block type with a table field type.

I borrowed from Holly Gronau via Slack. https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1664222574532039?thread_ts=1664201769.927669&cid=C9Z7RC3V1

export default {
  title: "Table for PortableText",
  name: "table",
  type: "object",
  fields: [
    {
      name: "content",
      title: "Cell Content",
      type: "tableBlockContent",
    },
  ],
  preview: {
    select: {
      content: "content",
      subtitle: "_type",
    },
    prepare({ content, subtitle }) {
      switch (content[0]._type) {
        case "block":
          return {
            title: content[0].children[0].text,
            subtitle,
          };

        default:
          return {
            title: `This list item start with '${content[0]._type}''`,
            subtitle,
          };
      }
    },
  },
};

Example of adding the type to your PortableText definition, in case that's new for someone...

export default {
  name: "bodyPortableText",
  type: "array",
  title: "Content body",
  of: [
    {
      type: "block",
      title: "Block",
      styles: [
        { title: "Normal", value: "normal" },
        { title: "H1", value: "h1" },
        { title: "H2", value: "h2" },
      ],
      marks: {
        decorators: [
          { title: "Strong", value: "strong" },
          { title: "Emphasis", value: "em" },
        ],
      },
    },
    { type: "table" },
  ],
};

doublejosh avatar Oct 26 '22 00:10 doublejosh

@doublejosh I think i get your example, but where does the type tableBlockContent come from??

mckelveygreg avatar Nov 29 '22 06:11 mckelveygreg

Ditto @doublejosh I don't get the tableBlockContent part

j2is avatar Jan 11 '23 16:01 j2is

sanity install table

https://www.sanity.io/plugins/sanity-plugin-table

doublejosh avatar Jan 13 '23 02:01 doublejosh

I've found some bugs with the UI where it has an error when trying to add columns. I've had to make a different edit to the table, and then it works again. Haven't tracked that one down yet.

doublejosh avatar Jan 13 '23 02:01 doublejosh

Closing this, as the new @portabletext/react library is the successor to this module. Please see https://github.com/portabletext/react-portabletext

rexxars avatar Jan 31 '23 01:01 rexxars