0.3.8 • Published 2 years ago

@wafo/table v0.3.8

Weekly downloads
1
License
MIT
Repository
github
Last release
2 years ago

@wafo/table

Inspired by other table libraries. Allows the user to create interactive and customizable tables that can handle local and remote data.

Demo

Coming soon

Installation

npm install --save @wafo/table

Note: This library has zero dependencies

Basic example

This example shows one of the easiest way this library can be used.

import React from 'react';
import { LocalTable } from '@wafo/table';

const columns = ['Id', 'Name', 'Email'];

const rows = [
  {
    id: 1,
    name: 'Jon Smith',
    email: 'jon@gmail.com',
  },
  // ..., more objects
];

const SomeComponent = () => (
  <div className="some-class">
    <LocalTable
      columns={columns}
      rows={rows}
      tableClass="table"
    />
  </div>
);

The components

There are two main components in this library, one for tables where you have all the data on hand and another for when the data comes in parts (from a server or somewhere else). This two components work on top of the base Table component, it renders a table based on the props it receives.

Table

The base component for the tables. You could use this component to make a simple table or extended to create your own specific one.

Example:

import React from 'react';
import { Table } from '@wafo/table';

const columns = ['Id', 'Name', 'Email'];

const data = [
  {
    id: 1,
    name: 'Jon Smith',
    email: 'jon@gmail.com',
  },
  // ...
];

const SomeComponent = () => (
  <div className="some-class">
    <Table
      columns={columns}
      rows={rows}
      tableClass="table"
      noRowsMessage="No entries to show."
      locale={'es'}
    />
  </div>
);

Props

This props from the base table are shared by the two other ones (since they're extensions of this one).

PropTypeRequiredDefault valueDescription
columnsString No Array of strings to populate the columns header.
rowsObject No Array of objects, used to generate all the rows of the table body.
tableClassStringNo"table"CSS class to be passed onto the <table> tag.
noRowsMessageStringNo"No data to show"Message when there are no rows to display.
localeStringNo"en"To choose the language of the table. Currently only english and spanish available.
onHeaderClickFunctionNof => fCallback function fired when one of the table headers is clicked.
columnsConfigObjectNo{ }Object that allows to change the way data is displayed, and the CSS styling, for each column.
rowsStyleObjectNo{ }Object that allows to change the styling of the rows.

LocalTable

This component works best when you have all the data at hand (maybe when loading data from a file). It has pagination, instant search and column ordering; it also lets you override most of its events in case you need some specific functionality.

Example:

import React, { useState, useEffect } from 'react';
import { LocalTable } from '@wafo/table';

const columns = ['Id', 'Name', 'Email'];

const SomeComponent = () => {
  const [rows, setRows] = useState([]);

  useEffect(() => {
    // Some logic to load your initial data.
    /**
    * const data = [
    *   {
    *     id: 1,
    *     name: 'Jon Smith',
    *     email: 'jon@gmail.com',
    *   },
    *   // ..., more objects
    * ];
    */
    setRows(data);
  }, []);

  const loadData = () => {
    // Some logic to refresh your data.
  }

  return (
    <div className="some-class">
      <LocalTable
        columns={columns}
        rows={rows}
        tableClass="table"
        noRowsMessage="No entries to show."
        updateTable={loadData}
        locale={'es'}
      />
    </div>
  );
};

Props

This table also shares the props of the base table.

PropTypeRequiredDefault valueDescription
tableWrapperClassStringNo"table-wrapper"CSS class to be passed onto the <div> tag that wraps the table.
keepPageBoolNofalseDetermines if the table should show the page one on data change, or if it should try to keep the current page.
controlselement or element NonullReact component (children) to be pased down to the header of the table (top-left). Usefull for adding buttons or filters.
updateTableFunctionNof => fCallback function fired from the "Reload" button of the table.

DataTable

This component works best when the data it's comming from a server (or somewhere else) on parts. It has all the features of the local table with some tweaks thinking of asynchronous data.

Example:

import React, { useCallback, useRef } from 'react';
import { DataTable } from '@wafo/table';

const columns = ['Id', 'Name', 'Email'];

const SomeComponent = () => {
  const [rows, setRows] = useState([]);
  const [totalRows, setTotalRows] = useState(0);

  const tableRef = useRef(null);

  const getData = useCallback(({ size, page, search }) => {
    const data = // ... Some logic to get your data (Fetch, etc)
    setRows(data);
  }, []);

  // Forces the table to send the pagination event
  const forceUpdate = () => tableRef.current.forceUpdate();
  
  return (
    <div className="some-class">
      <DataTable
        ref={tableRef}
        locale="en"
        columns={columns}
        rows={rows}
        totalRows={totalRows}
        onPagination={getData}
        controls={
          <div className="test">
            <button type="button" onClick={forceUpdate}>
              Force reload
            </button>
          </div>
        }
      />
    </div>
  );
};

Props

This table also shares the props of the base table.

PropTypeRequiredDefault valueDescription
tableWrapperClassStringNo"table-wrapper"CSS class to be passed onto the <div> tag that wraps the table.
controlselement or element NonullReact component (children) to be pased down to the header of the table (top-left). Usefull for adding buttons or filters.
updateTableFunctionNof => fCallback function fired from the "Reload" button of the table.
initialPageNumberNo1The initial page of the table state. This would be the first page to be asked from the server.
totalRowsNumberYes0this number it's used to calculate the number of pages and create the pagination.
onPaginationFunctionNof => fCallback function. Fired on first load, pagination events, search events and even when pressing the reload button (if updateTable not provided). Passes an object with size, current page and search value.

configTable object

To-do: This.

Table example

The blue square above the table it's where the "control" elements get rendered.

Table Example

Limitations

  • No nested tables.
  • The search only works on the objects before customization.
0.3.8

2 years ago

0.3.7

2 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.16

4 years ago

0.2.15

5 years ago

0.2.14

5 years ago

0.2.13

5 years ago

0.2.12

5 years ago

0.2.11

5 years ago

0.2.10

5 years ago