Giter Club home page Giter Club logo

contentful-json-table's Introduction

contentful JSON Table

Who loves tables? Not me but I'd sure like to be able to have my editors create them inline on Contentful!

A Contentful field extension for the Object field type, designed for creating nice tables.

Built with create-contentful-extension โค๏ธ

๐Ÿšจ work in progress!

The extension in use:
Contentful JSON table in use

Same as above but in read-only view:
Contentful JSON table read-only view

Using the extension in production

Data structure

๐Ÿ‘‰ I made a pen that should transform typical table/csv JSON to work with this extension.

It goes a little something like this:

alt text

Usage (React)

This is how I'm using it.
NB: this could be more simplified:

  • I use hidden keys (starting with __) for e.g a heading with a colspan of 2 that has 2 cells below it, cell 1 would have the heading 1 key, cell 2 would have a __heading 1 key
  • I use React.createElement in case I want to specify the element with the el property
import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';

const TableCanOverflow = styled.div`
  display: grid;
  > div {
    overflow-x: auto;
    overflow-y: hidden;
  }
`;

const Table = styled.table`
  border-spacing: 0;
  max-width: 100%;
  width: 100%;
  th,
  td {
    padding: 1rem 2rem 1rem 0;
    text-align: left;
    border-bottom: 1px solid rgba(13, 42, 68, 0.1607843137254902);
  }
`;

// table in this calse is 
// childContentful[fieldName]JsonNode from graphql (I think)

const TableFromJson = ({ table }) => {
  // because we don't always know the
  // keys, we parse the stringified
  // json returned by Contentful
  // and pull the table headings
  // from the keys
  const content = table ? JSON.parse(table.internal.content) : {};
  const { data, tableHeadings } = content;

  return (
    <TableCanOverflow>
      <div>
        <Table>
          <thead>
            <tr>
              {tableHeadings &&
                tableHeadings.map(
                  ({ text, ...rest }) =>
                    text &&
                    text.length &&
                    !text.startsWith('__') && (
                      <th key={text} {...rest}>
                        {text && text}
                      </th>
                    )
                )}
            </tr>
          </thead>
          <tbody>
            {data &&
              data.map((key, i) => (
                <tr key={i}>
                  {tableHeadings.map((heading, i) => {
                    const row = key[heading.text];
                    const { text, el, heading: head, ...rest } = row || {};

                    return (
                      text &&
                      !text.startsWith('__') &&
                      React.createElement(
                        el || 'td',
                        {
                          key: i,
                          dangerouslySetInnerHTML: {
                            __html: text || '',
                          },
                          ...rest,
                        },
                        null
                      )
                    );
                  })}
                </tr>
              ))}
          </tbody>
        </Table>
      </div>
    </TableCanOverflow>
  );
};

TableFromJson.propTypes = {
  table: PropTypes.shape({
    internal: PropTypes.object.isRequired,
  }),
};

export default TableFromJson;

Getting started with local development

npm install
npm run login && npm run configure
npm run start

Requirements

  • Node 8 or higher
  • NPM 5.2 and higher

contentful-json-table's People

Contributors

renrizzolo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

contentful-json-table's Issues

Add links in a cell

Hi,

Loving your extension thanks ! However i'd like to add some links on a text added in a cell.
Can you help me on this ?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.