# magic-react-table

> This is a simple React table component that allows sorting and styling of the header and body.

Latest version **1.0.1** (published 2019-08-31) · ISC license · 0 weekly downloads

## Install

```sh
npm install magic-react-table
pnpm add magic-react-table
yarn add magic-react-table
bun add magic-react-table
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2019-08-31 |
| First published | 2019-08-31 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 1.4 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sean Sivils |
| Maintainers | sivs |
| Keywords | table, react |

## Links

- npm: https://www.npmjs.com/package/magic-react-table
- npm.io page: https://npm.io/package/magic-react-table

## Dependencies (2)

- [react](https://npm.io/package/react.md) ^16.9.0
- [webpack](https://npm.io/package/webpack.md) ^4.39.3

## Alternatives

- [@lexical/table](https://npm.io/package/@lexical/table.md) — 3.0M weekly downloads
- [mantine-datatable](https://npm.io/package/mantine-datatable.md) — 98.2K weekly downloads
- [react-native-collapsible-tab-view](https://npm.io/package/react-native-collapsible-tab-view.md) — 70.6K weekly downloads
- [@handsontable/vue3](https://npm.io/package/@handsontable/vue3.md) — 16.1K weekly downloads
- [vuewordcloud](https://npm.io/package/vuewordcloud.md) — 7.2K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2019-08-31
- 1.0.0 — 2019-08-31

## README

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## Magic React Table v1.0

Magic React Table is a React Table component that makes tables easy - style it, sort it, search it, and more!

## Installing Magic React Table

To install Magic React Table, just use `npm install magic-react-table --save` in the root of your project.

To use it in your React project, import the `Table` component into a functional or class React component like so: 

`import Table from 'magic-react-table'`

## Using The Table Component

To instantiate a Magic React Table Component, call the `Table` component like any other React component, including any 
of the `props` available (see the **Magic React Table Props** section below for more on the `props` available):

`<Talbe data={staffData} />`

## Magic React Table Props

When using the `Table` component, there are a few `props` that can be used:

| Prop | Type | Required | Description |
| :---: | :---: | :---: | :---: |
| data | _Array of Objects_ | Yes |An array of objects pertaining to the data you wish to be displayed in the table.
| headers | _Array of Strings_ | No | An array of strings to specify custom column headers for the table. (default column headers are the object keys in the data passed via the data prop).  The number of headers in the array must match the number of key/value pairs in the data objects of the array passed to the data prop.
| addClasses | _Array of Strings_ | No | An array of strings to specify any classes to be added to the Table wrapper div.
| options | _Object_ | No | An object with two nested objects (specified by a header and body key) that specifies various styling options within your table.

All examples in this documentation will use the following `staffData` array for the `data` prop:

```jsx harmony
let staffData = [
    {
        "name": "Kathy",
        "title": "Technician, Int",
        "service date": 2005,
        "salary": "$67,000",
        "phone": "888-555-1297"
    },
    {
        "name": "Robert",
        "title": "Supervisor",
        "service date": 2018,
        "salary": "$78,000",
        "phone": "888-555-9913"
    },
    {
        "name": "William",
        "title": "Technician, Jr",
        "service date": 2007,
        "salary": "$55,000",
        "phone": "888-555-5612"
    },
    {
        "name": "Samantha",
        "title": "Accountant",
        "service date": 2011,
        "salary": "$61,000",
        "phone": "888-555-5612"
    }
];
```

## Using The Props
### `data`
The `data` prop is required for the `Table` component which must be passed an array of objects, like the 
`staffData` array of objects above. Because the keys in the first object are used as the default headers for 
the table, the key/value pairs must have the same type of data in the same place in each object. As you can see in 
the `staffData` array, each object has a 'name' key as key one, 'title' as key two, 'service date' key as key three, 
and so on...

###### Example of a basic `Table` component:
`<Table data={staffData} />`
![Table without headers prop](./src/images/table-without-headers.png)
_*Some styling applied to headers for readability_

#
### `headers`
To forgo using the default headers, specified by the keys of the objects provided to the `data` prop, 
pass an array (of strings) of new headers to the `headers` prop. This will replace each respective 
column header based on its position in the array, like so:

`let newHeaders = ['column1', 'column2', 'column3', 'column4', 'column5']`

In the example array `newHeaders`, the string `column1` would replace the header in the first column of the table, 
the string `column2` would replace the header in the second column of the table, and so on...  Please be aware 
that if the length of the array passed to the `header` `prop` does not equal the 
number of keys in each object within the array passed to the `data` `prop`, the `header` `prop` will be ignored
and the default headers will be used.

###### Example of `Table` component with `headers`:
```jsx harmony
const customHeaders = ['Column1', 'Column2', 'Column3', 'Column4', 'Column5'];

<Table data={staffData} headers={customHeaders} />
```
![Table with headers prop](./src/images/table-with-headers.png)
_*Some styling applied to headers for readability_

#
### `addClasses`
The `addClasses` prop allows you to add classes to the table's container `div` by passing in an array of strings that will 
be added as class names.

###### Example of `Table` component with `addClasses`:
```jsx harmony
const tableClasses = ['table1', 'staff_table'];

<Table data={staffData} addClasses={tableClasses} /> 
```
![Table with addClasses prop](./src/images/table-with-classes.png)
_*Some styling applied to headers for readability_

#
### `options`
The `options` prop allows you to add various pre-defined styling options (discussed in detail below) or append various 
standard CSS styling to rows, columns, or cells as specified in the `options` object.

The two keys in the options variable passed to the porp must be `header` and `body`, and must both be objects themselves:
```javascript
const baseOptions = {
    header: {},
    body: {},
};
```
As implied, the `header` object specifies options to be applied to the header of the table, while the `body` object specifies
options to be applied to the body. Below we will take a look at each respective child object (header and body); their built-in 
options, as well as how to apply styling to the table through them.

####`header` object:
The `header` object has a few built-in options that allow the control of some styling functionality within 
the header of the table, as well as a `styles` object that takes a targeted approach to applying CSS styling:

| Key | Type | Default Value | Description |
| :---: | :---: | :---: | :---: |
| initialSortBy | String | n/a | Pass the value of the column header to be sorted by initially. If the `initialSortOrder` prop is not specified, the designated column will be sorted in ascending order. |
| initialSortOrder | String | asc | This must either be `asc` for ascending order or `desc` for descending order. |
| sortable | Boolean | false | If `sortable` is `true`, the column headers will become sortable on click - clicking on a column header that is not sorted will sort it in ascending order. Clicking a column that is sorted in ascending order will resort the column in descending order. Clicking on a column that is sorted in descending order will resort the column in ascending order. |
| capitalize | Boolean | false | If `capitalize` is `true`, each word of all headers in the table will be capitalized. |
| styles | Object | { } | Use defined designators as keys to target header areas, and an object as the value with CSS styling key/value pairs. Valid header key designators are: `row`, `col-all`, `col-even`, `col-odd`, and `col#` (ie. - `col4`) |
###### Example of using `header` option:
```javascript
const tableOptions = {
    header: {
        sortable: true,
        initialSortBy: 'name',
        initialSortOrder: 'desc',
        capitalize: true,
        styles: {
            row: {
                border: '5px solid #0dddc2',
            },
            'col-all': {
                fontWeight: 'bold',
                border: '2px dashed #000',
            },
            'col-odd': {
                backgroundColor: '#5c56ff',
                color: '#fff',
                paddingLeft: '10px',
            },
            'col-even': {
                backgroundColor: '#fc9494',
                textAlign: 'center',
            },
            col2: {
                color: '#00ff00',
            },
        },
    },
    body: {},
};

<Table data={staffData} options={tableOptions} />
```
![Table with addClasses prop](./src/images/table-with-header-options.gif)

####`body` object:
The `body` object has a few built-in options that allow the control of some styling functionality within 
the body of the table, as well as a `styles` object that takes a targeted approach to applying CSS styling:

| Key | Type | Default Value | Description |
| :---: | :---: | :---: | :---: |
| zebra | Boolean | false | If zebra is set to true, the background color, set to #e7e7e7 by default but can be explicitly specified using the zebraColor body option, of even or odd rows' , specified by the zebraStart body option, are changed.|
| zebraStart | String | odd | zebraStart specifies which rows the zebra background color will be applied to: even or odd. |
| zebraColor | String | #e7e7e7 | zebraColor specifies the color that the zebra background. |
| styles | Object | { } | Use defined designators as keys to target body areas, and an object as the value with CSS styling key/value pairs. Valid body key designators are: `row-all`, `row-even`, `row-odd`, `row#` (ie. - `row4`),`col-even`, `col-odd`, `col#` (ie. - `col4`), and `cell#` (ie. - `cell4`) |
###### Example of using `body` option:
```javascript
const tableOptions = {
    header: {
        styles: {
            'col-all': {
                fontWeight: 'bold',
            },
        },
    },
    body: {
        zebra: true,
        zebraStart: 'odd',
        zebraColor: '#2b87ff',
        styles: {
            'row-all': {
                borderTop: '2px solid #b703c4',
                borderBottom: '2px solid #b703c4',
            },
            'row-even': {
                color: '#ff0000',
            },
            'row-odd': {
                color: '#00ff00',
            },
            'col-all': {
                padding: '10px 0px',
            },
            'col-even': {
                textDecoration: 'overline',
            },
            'col-odd': {
                textDecoration: 'underline',
            },
            col4: {
                border: '2px dashed #363636',
            },
            cell6: {
                backgroundColor: 'orange',
            },
        },
    },
};

<Table data={staffData} options={tableOptions} />
```
![Table with addClasses prop](./src/images/table-with-body-options.png)
_*Some styling applied to headers for readability_

---
_Source: https://npm.io/package/magic-react-table · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
