npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-tablefy

v2.1.1

Published

A simple React dynamic table with configurations

Downloads

20

Readme

alt text

A Dynamic React Table Component

NPM JavaScript Style Guide

Install

  • With NPM
npm install --save react-tablefy
  • With Yarn
yarn add react-tablefy

Table of Contents

Usage

Basic

Let's say you have an array of objects and you want to display this into a table

  • data.json
[
  {
    "displayImage": "https://vignette.wikia.nocookie.net/gameofthrones/images/1/1f/Night_King_BTW.jpg/revision/latest?cb=20171013162809",
    "name": "Night King",
    "age": 10000,
    "location": "Beyond the wall"
  },

  {
    "displayImage": "https://vignette.wikia.nocookie.net/gameofthrones/images/4/4c/JonSnowTheBells.PNG/revision/latest/scale-to-width-down/329?cb=20190513080305",
    "name": "Jon Snow",
    "age": 23,
    "location": "The Wall"
  }
]
import React, { Component } from "react";
import Table from "react-tablefy";
import data from "./data.json";

class SimpleTableExample extends Component {
  render() {
    return <Table data={data} />;
  }
}

This will output:

alt text

Custom Components

But... The field displayName is an image, and it's rendering as an String, let's say you want to display an image component to this field.

  • 1: Create a config object.

  • 1.1: Create a components object inside config.

  • 1.2: Inside components object set the key you want to display a custom Component (in our example, displayImage).

  • 1.3: We want to show the value of key (displayImage) for each table row inside the Image Component src prop. We need to say that prop src gets a value of displayImage using tablefy variables (tablefy understands that the variables are within parentheses. You can access the value of any key in a table row, using paranteses).

  • 1.3.1: variable (self) DEFAULT returns the current value for the key you are customizing. In this example, it takes the value of displayImage key for that table row and places it inside src prop.

  • 1.3.2: variable (name) will get the value of name key for this table row and place inside alt prop.

  • 2: Set this object to config prop.

import React, { Component } from "react";
import data from "./data.json";
import Table from "react-tablefy";
import Image from "./any-image-component";

class TableWithCustomComponents extends Component {
  render() {
    // Step 1
    const tableConfig = {
      // Step 1.1
      components: {
        // Steps 1.2, 1.3, 1.3.1 & 1.3.2
        displayImage: <Image src="(self)" alt="(name)" />
      }
    };

    // Step 2
    return <Table data={data} config={tableConfig} />;
  }
}

This will output:

alt text

Actions

Components actions

You can pass functions to custom Components, argument names has to be the same as the keys you want to get the value.

1 - Create a function with argument names same as the key values you want to get.

1.1 - In this example, we will print the name of the character present in the table row on the console.

1.2 - Tablefy will search for the key name in the row and pass it to your function.

import React, { Component } from "react";
import data from "./data.json";
import Table from "react-tablefy";
import Image from "./any-image-component";

class TableWithActions extends Component {
  handleClickTableImage(name) {
    console.log(name);
  }

  render() {
    const tableConfig = {
      components: {
        displayImage: (
          <Image onClick={handleClickTableImage} src="(self)" alt="(name)" />
        )
      }
    };

    return <Table data={data} config={tableConfig} />;
  }
}

Row actions

Let's say you want to perform an action by clicking on a row in the table.

1 - add a key called onClickRow to your configuration object assigning to it the function you want to call, using the same parameter principles described in the above example.

1.1 - The function will search for a key call name present on the table row and set this value as parameter on your function.

import React, { Component } from "react";
import data from "./data.json";
import Table from "react-tablefy";
import Image from "./any-image-component";

class TableWithRowAction extends Component {
  handleClickOnTableRow(name) {
    console.log(name);
  }

  render() {
    const tableConfig = {
      components: {
        displayImage: <Image src="(self)" alt="(name)" />
      },

      // Step 1
      onClickRow: handleClickOnTableRow
    };

    return <Table data={data} config={tableConfig} />;
  }
}

Config Object

const tableConfig = {
  components: {
    // Custom components for a given key
    displayImage: <Image src="(self)" />
  },

  // Custom names to table head keys
  labels: {
    displayImage: "Image"
  },

  // If you don't want to show all data set object keys
  // you can choose which fields you want to show
  // just add their names on the keys array
  keys: ["displayName", "age"]
  // This will show only displayName and age on table
};

Styling

With Styled components

import styled from "styled-components";
import Table from "react-tablefy";

const StyledTable = styled(Table)`
  // Your custom styles
  tr {
    background: red;
  }
`;

class WithStyledComponent extends Component {
  render() {
    return <StyledTable data={data} />;
  }
}

With Config Object

import React, { Component } from "react";
import Table from "react-tablefy";

class WithInlineCSS extends Component {
  render() {
    // Step 1
    const tableConfig = {
      styles: {
        // Available styles
        root: {},

        head: {},
        headRow: {},
        headRowCell: {},

        body: {},
        bodyRow: {},
        bodyRowCell: {}
      }
    };

    // Step 2
    return <Table data={data} config={tableConfig} />;
  }
}

With custom class

import React, { Component } from "react";
import Table from "react-tablefy";
import styles from './styles.css'

class WithInlineCSS extends Component {
  render() {
    return <Table
      className={styles.customClass}
      data={data}
    />;
    };
  }
}

TODO

  • Improve writing of this documentation (my english sucks haha)
  • Tests

License

MIT © paesvitor