0.0.28 • Published 2 years ago

rare-earth v0.0.28

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

Rare Earth

Rare Earth is a package for making React tables with ease. This library is browser native so does not require any build step.

Table of Contents

  • Installation
  • Usage

Installation

npm install rare-earth

Usage

Include the source in your html

<script defer type="text/babel" src="{% static 'path/to/rare-earth.js' %}"></script>

In your React functions

App();
function App(){
  const container = document.getElementById('react-container');
  const root = ReactDOM.createRoot(container);

  // Might include classes for styles, like Bootstrap
  const tableClasses = ['table', 'table-dark', 'table-striped'];

  // Example Columns
  const columns = {
    index: false,
    order: [
      'example_column_key_1',
      'example_column_key_2',
      'example_column_key_3'
    ],
    attributes: {
      'example_column_key_1': {
        name: 'Example Column Name 1',
        type: 'string',
        allow_null: true,
      },
      'example_column_key_2': {
        name: 'Example Column Name 2',
        type: 'number',
        allow_null: true,
      },
      'example_column_key_3': {
        name: 'Functional Example Concat',
        type: 'string',
        allow_null: true,
        valueFunc: function(record){
          return ((record['example_column_key_1'] == null) || (record['example_column_key_2'] == null)) ? null : record['example_column_key_1'] + record['example_column_key_2'];
        },
        displayFunc: function(record, value){
          return (<button onClick={(event) => console.log("The value is: " + value)}>{((record['example_column_key_1'] == null) || (record['example_column_key_2'] == null)) ? null : record['example_column_key_1'] + record['example_column_key_2']}</button>);
        }
      }
    }
  };

  // Example Records
  records: [
    {
      'example_column_key_1': 'abc',
      'example_column_key_2': 456
    },
    {
      'example_column_key_1': 'abc',
      'example_column_key_2': 123
    }
  ];

  root.render(
    <RareEarth.Table tableClasses={tableClasses} display={display} columns={columns} records={records}/>
  );
}
export default App;

Props

columns

The columns prop is a javascript object with attributes:

  • index
  • order
  • attributes

The index is currently unused but will be a boolean that represent whether an unamed index column will be prepended to the leftmost column.

The order is an array of strings where each string is unique and must match one of the column keys (defined in the attributes attribute). This is the order (left to right) in which the columns will appear. The user will have the ability to manually swap the order of columns by dragging and dropping the column headers.

The attribues attribute is where the columns are defined. The attribues attribute must be an object of key value pairs where each key is a unique string representing the column identifier and the value is another object with the following attributes:

  • name
  • type
  • allow_null
  • valueFunc (optional)
  • displayFunc (optional)
  • compareFunc (optional)
columns.attributes

The name attribute is a string that is the human readable display name of the column. Future: Allow for functions that return react components as headers - to allow for images etc

The type attribute is the expected type of the values for the column.

The allow_null attribute is a boolean indicating whether the column is allowed to have null values.

The valueFunc attribute is a function that can take a record (a record is an object that represents a row) and returns a value of type type. In the event that valueFunc is not defined (or null), the result will be to use recordcolumn_key. The value is what will determine sorting ordering. () => ... (record) => ...

The displayFunc attribute is a function that can take a record and a value (the value generated by valueFunc or the default) and returns a react_component/dom_element/string/number etc that will serve as the display in the column cell. In the event that displayFunc is not defined (or null), the result will be the value. () => ... (record) => ... (record, value) => ...

The compareFunc attribute is a function that takes two values from the column (each from differing rows) and compares them in sorting. See Array.sort compareFunction.

0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.24

2 years ago

0.0.25

2 years ago

0.0.15

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.26

2 years ago

0.0.27

2 years ago

0.0.28

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago