0.0.15 • Published 5 years ago

react-immutable-jss-data-table v0.0.15

Weekly downloads
5
License
MIT
Repository
gitlab
Last release
5 years ago

Show Immutalbe.List data in tables using jss for styling

usage

file sheet.js

export default {
  // default classNames
  DataTable: {
    // root
  },
  thead: {},
  tbody: {},
  tr: {},
  trEven: {},
  trOdd: {},
  trh: {},
  th: {},
  td: {},
  tdEmpty: {},
  // your own classNames
  thumb: {},
};

file pageWithTable.js

import React from 'react';
import DataTable from 'react-immutable-jss-data-table';
import sheet from './sheet.js';
// your jss injectSheet instance
import injectSheet from 'lib/sheet';
import tableColumns from './columns.jsx'

const columns = tableColumns.select('title', 'link', 'user', 'clickMe');

class MyPage extends React.PureComponent {
  render() {
    const { classes, list } = this.props;
    return (
      <div
        className={classes.container}
      >
        <DataTable
          classes={classes}
          columns={columns}
          items={list}
        />
      </div>
    );
  }
}

export default injectSheet(sheet)(MyPage);

define table columns

You can use fieldPath: ['path', 'to', 'val', 'in', 'rowData'] (rowData.getIn(fieldPath) will be used), or define your own getValue function, or even pass React component as render: CustomCellComponent.

import React from 'react';
import I from 'immutable';
import { columnsSelector } from 'react-immutable-jss-data-table';

export const columns = {
  // use rowIndex:
  cell0: {
    getValue: ({ rowIndex }) => `# ${rowIndex}`,
  },
  // add extra class, it will be searched in classes prop or cellClasses prop or used as is
  cell1: {
    className: 'my-own-class-name',
  },
  cell2: {
    // you can cpecify how to extract data from entries (rowData.getIn(fieldPath))
    fieldPath: ['article', 'title'],
    // then you can change the value
    getValue: ({ value }) => `Title: ${value}`,
  },
  cell3: {
    // how to extract data from rowData,
    fieldPath: ['article', 'title'],
    // then you can change the value
    getValue: ({ value, rowData }) => `Title: ${value}`,
    // you can render cell using your own component
    render: ({ value, rowData }) => <li children={'changed value: ' + value} />,
    // React component also can be used
    render: CustomCellComponent,
  },
  // if you whant to show column titles in table, please use 'title' prop
  cell4: {
    fieldPath: ['someField'],
    // column title (th)
    title: 'colum title',
    // you can change default title, f.e. add items amout
    getValueTh: ({ value, items }) => `${value} (${itmes.size})`,
    // render th using your own component
    renderTh: hoToRenderTh,
  },
  // you can change cell inner content markup using getValue:
  link: {
    fieldPath: ['article', 'url'],
    getValue: ({ value }) => <Link to={value} />,
  },
  // use DataTable props (<DataTable user={user}/>)
  user: {
    fieldPath: ['userId'],
    getValue: ({ classes, rowData, value, rowIndex, user }) =>
      <div
        className={classes.thumb}
        children={users.getFullNameById(value)}
      />,
  },
  // change cell markup fully
  clickMe: {
    render: ({ className, classes, rowData, value, rowIndex, props }) =>
      <div
        className={className}
        children={value}
        onClick={() => props.toggleEditRow(rowIndex)}
      />
  },
  // see definition bellow
  select: null,
};
columns.select = columnsSelector(columns);

export default columns;

available options for columns:

  • fieldPath - array used for Immutable.getIn method to extract value from rowData
  • className - custom className, used as additional className for cell
  • getValue - func, returns node or string or number, that will be placed as child in cell node
  • getValueTh - func, returns node or string or number, that will be placed as child in th node
  • render - func returns node, replaces cell root node
  • title - will be used as column title (like table > th) if showColumnsTitles set to true
  • getValueTh - allows to set column title using columnDef, and all DataTable props
  • renderTh - allows to render column, used as title for table, with your own React component
  • any other props, you can use in render, renderTh, getValue, getValueTh, Cell, ...

see properties for more details

see column definition examples

You can set your own component to render rows, or decide wich one to use

import DataTable, { Row } from 'react-immutable-jss-data-table';
const getRowRenderer = (props) => {
  const { rowData, rowIndex, ...allDataTableProps } = props;
  const Renderer = rowIndex === customRowIndex ? MyCustomRow : Row;
  return <Renderer {...props} />
}
<DataTable
  // ...
  Row={getRowRenderer}
/>

Customise empty row component, default RowEmpty can be used for your purposes

import DataTable, { RowEmpty } from 'react-immutable-jss-data-table';

const CustomRowEmpty = (props) =>
  rowIndex == 1 ? <MyEmptyRowComponent /> : <RowEmpty {...props} />;

<DataTable
  // ...
  RowEmpty={CustomRowEmpty}
/>

set your own Th / Cell components

import DataTable, { Th, Cell } from 'react-immutable-jss-data-table';

<DataTable
  // ...
  Th={Th}
  Cell={Cell}
/>

THead component example

import DataTable from 'react-immutable-jss-data-table';

const THead = ({ children, classes, ...otherDataTableProps }) =>
  <div className={classes.thead} children={children} />;


<DataTable
  // ...
  THead={THead}
/>

TBody component example

import DataTable from 'react-immutable-jss-data-table';

const TBody = ({ children, classes, ...otherDataTableProps }) =>
  <div className={classes.tbody} children={children} />;


<DataTable
  // ...
  TBody={TBody}
/>

Exported components and helpers

import DataTable, {
  // default getters
  renderTh,
  getValueTh,
  getValue,
  renderCell,
  // components
  Th,
  TBody,
  THead,
  Row,
  RowEmpty,
  Cell,
  DataTable,
  // sheet example
  sheet,
  // helpers
  getCellClassNames,
  getThClassNames,
  getRowClassNames,
  getColumnDef,
  columnsSelector,
} from 'react-immutable-jss-data-table';

properties

componentproptypedescription
DataTable   
 columnsarrayarray with column definitions
 itemsI.ListImmutable.List with data for table
 classesobjectjss classes
 getRowClassNamesfunctionreturn array with classnames
 getCellClassNamesfunctionreturn array with classnames
 getThClassNamesfunctionreturn array with classnames
 showColumnsTitlesbooleandisplay row.trh with titles.th
 classNamestringadditional table className
 Cellcomponentcomponentfunction
 Thcomponentcomponentfunction
 RowEmtpycomponentcomponentfunction
 Rowcomponentcomponentfunction
 renderThrenderabledefault fn for columnDef.renderTh
 getValueThrenderabledefault fn for columnDef.getValueTh
 getValuerenderabledefault fn for columnDef.getValue for Cell
 renderrenderabledefault fn for columnDef.render for Cell
 defaultValueanydefaultValue for Cell
Row   
 rowIndexinteger
 rowDataI.MapImmutable.Map with data for row
 DataTable props
TBody   
 children
 DataTable props
THead   
 children
 DataTable props
Th   
 titlecolumnDef.title
 columnDefcolumns.map(columnDef => ...) column options, column definition
 getValueThcolumnDef.getValueTh
 renderThcolumnDef.renderTh
 DataTable props
Cell   
 columnDefobjectcolumns.map(columnDef => ...) column options, column definition
 valueanyextracted by
 rowDataI.Mapitems.map(rowData => ...)
 rowIndexinteger
 rendercomponent
 getValuecomponentcolumnDef getValue
 fieldPatharrayrowData.getIn(fieldPath) used later in columnDef getValue
 defaultValueanycell default value, returned by getValue
 DataTable props
RowEmpty   
 DataTable props

getters

getterproptypedescription
getValue
 valueany
 fieldPatharray
 rowDataI.Map
 rowIndexintegerrow order bumber based on 0
 columnDefobjectcolumn options, column definition
 DataTable props
render
 valueany
 fieldPatharray
 rowDataI.Map
 rowIndexintegerrow order bumber based on 0
 classNamestring
 columnDefobjectcolumn options, column definition
 DataTable props
getValueThreturns title for table column
 valueanycolumnDef.title column options
 columnDefobjectcolumn options, column definition
 DataTable props
renderTh
 valueanyreturned by getValueTh
 classNamestring
 columnDefobjectcolumn options, column definition
 DataTable props

columnDef getValue

let value = rowData.getIn(fieldPath);
value = getValue ? getValue({ value, columnDef, rowData, rowIndex, ...props });

columnsSelector

// file ./columns.js
import { columnsSelector } from 'react-immutable-jss-data-table';
const COLUMNS_DEF = { title, link, user, clickMe, select: null };
COLUMNS_DEF.select = columnsSelector(COLUMNS_DEF);
export default COLUMNS_DEF;

// file with table
import DataTable from 'react-immutable-jss-data-table';
import tableColumns from './columns.js';
const columns = tableColumns.select('title', 'link', 'user', 'clickMe');

// in your component with table
<DataTable
  columns={columns}
  items={items}
  ...
/>

getColumnDef

// file ./columns.js
import { getColumnDef } from 'react-immutable-jss-data-table';
export default const COLUMNS_DEF = {
  title, link, user, clickMe,
  select: (...keys) => keys.map(col => getColumnDef(COLUMNS_DEF, col)),
};

// file with table
import DataTable from 'react-immutable-jss-data-table';
import tableColumns from './columns.js';
const columns = tableColumns.select('title', 'link', 'user', 'clickMe');

// in your component with table
<DataTable
  columns={columns}
  items={items}
  ...
/>
0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago