6.0.1 • Published 11 months ago

@jetblack/material-data-table v6.0.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
11 months ago

@jetblack/material-data-table

A simple data table for mui.

Installation

The package can be installed from npmjs:

npm install @jetblack/material-data-table

Storybook

The project has a storybook.

git clone https://github.com/rob-blackbourn/jetblack-material-data-table.git
cd jetblack-material-data-table
npm install
npm run storybook

Status

This is work in progress.

Features

  • Optional custom get, format and render
  • Optional search
  • Optional sort
  • Optional pagination
  • Row details
  • Selection

Usage

The DataTable has the following props:

NameTypeDefaultDescription
columnsarrayrequiredThe columns
rowsarrayrequiredThe rows
selectedarray[]The list of selected rows
isSelectablebooleanfalseWhether the rows are selectable
onSelectionChangedfunctionnullA function to call when the selection is changed
disabledbooleanfalseWhether to disable selection
compareRowfunctionnullA callback function to compare a rows in selected
filterTextstring''Text to filter the rows
paginatebooleantrueWhether to paginate the table
rowsPerPagenumber10The number of rows to display when paginating
rowsPerPageOptionsarray[ 10, 20, 100]The choices of number of rows to display
rowDetailfunctionnullA row detail panel
sizestring'medium'The table size
paddingstring'default'The table padding
stickyHeaderbooleanfalseWhether to use sticky headers
classNamestringnullThe class name
stylestylesnullThe component styles
columnSortMapobjectnullA map of column id to 'asc' or 'desc'
contextanynullUser data passed through to callbacks
sxobjectsnullObject for styling

DataTable.rows

The rows prop is a list of objects where each object is a now in the table.

DataTable.columns

The columns prop is a list of Column objects with the following properties:

NameTypeDefaultDescription
idstringrequiredThe column id
titlestringnullThe column title
hidebooleannullIf true the column is hidden
alignstring'left'The column alignment
getValuefunctionnullA custom function to select cell data
formatValuefunctionnullA custom function to format cell data
renderValuefunctionnullA custom function to render a cell
searchfunctionnulla custom function used by the filter
comparefunctionnullA custom function used when sorting

Column.id

This is required. It is a unique identifier for the column, an if no accessor is supplied for the it is used as the key for the row.

Column.title

This is an optional string which will be used as the title of the column.

Column.hide

This is an optional boolean which, when true, hides the column.

Column.align

This is the optional column alignment which is passed straight through to TableCell.

Column.getValue

This is an optional function to get the column value in its native form (it doesn't need to be formatted to a string). It has the following prototype:

(row: Row, column: Column, columns: Column[], rows: Row[], context: any) => any

If this function is not specified it defaults to:

function (row: Row, col: Column, columns: Column[], rows: Row[], context: any): any {
  return row[column.id]
}

Column.formatValue

This is an optional function which takes the row value and formats it to a string. it has the following prototype:

(value: any, row: Row, column: Column, columns: Column[], rows: Row[], context: any) => string

If this function is not specified a simple string conversion is applied.

The formatted value is used for rendering if a renderValue function is not defined, an for searching.

Column.renderValue

This is an optional function which takes the row value and returns either a string or a react component. It has the following prototype.

(value: any, row: Row, column: Column, columns: Column[], rows: Row[], context: any) => React.ReactNode | string

If this function is not specified the formatted value is used.

Column.search

This is an optional function which takes the search text and returns true if the text matches the column value. It has the following prototype.

(searchText: string, row: Row, column: Column, columns: Column[], rows: Row[], context: any) => boolean

If the function is not specified the formatted value is used for comparison.

Column.compare

This is an optional function which is used for sorting. It has the following prototype:

(lhs: Row, rhs: Row, column: Column, columns: Column[], rows: Row[], context: any) => -1 | 0 | 1

If the function is not specified the column values are used in the comparison.

DataTable.selected

This is an optional list of rows which are selected. It defaults to an empty list. If selection is required, this property must be maintained by the state of the parent component.

DataTable.isSelectable

This is an optional boolean specifying whether the selection checkboxes should be displayed. It defaults to false.

DataTable.onSelectionChanged

This is an optional callback function which takes the list of selected rows.

DataTable.compareRow

This is an optional callback function used to check if a row is in the selected rows array. The default is to check that the object is the same (i.e. row === row), which will fail if the row objects are recreated.

DataTable.filterText

This is an optional string which is used to filter the rows in the table.

DataTable.paginate

This is an optional boolean which controls whether the table provides pagination. It defaults to true.

DataTable.rowsPerPage

The number of rows shown in a page. This defaults to 10.

DataTable.rowsPerPageOptions

An optional list of the number of rows that can be shown on a page.

DataTable.rowDetail

An optional property which provides a row detail page. It has the following prototype:

(row: Row, columns: Column[], rows: Row[], context: any) => React.ReactNode

DataTable.size

An optional property specifying the size of the page.

DataTable.padding

An optional property controlling the table padding.

DataTable.stickyHeader

An optional property controlling the header position.

DataTable.columnSortMap

An optional property to control the initial sort state. It has the following type:

export interface ColumnSortMap {
  [columnId: string]: 'asc' | 'desc'
}

Examples

Simple

Here is a simple example:

import * as React from "react"

import { DataTable } from '@jetblack/material-data-table'

const RenderDataTable = () => {
    const columns = [
      {
        id: "name",
        title: "Name",
        align: "left",
      },
      {
        id: "band",
        title: "Band",
        align: "left",
        getValue: row => row["band"],
      },
      {
        id: "founded",
        title: "Founded",
        align: "right",
        formatValue: (value, row) => `${row.band} founded in ${value}`,
      }
    ]
    const rows = [
      { name: "Jimmy Page", band: "Led Zeppelin", founded: 1968 },
      { name: "Marc Bolan", band: "T. Rex", founded: 1967 },
    ]

    return (
      <div>
        <DataTable columns={columns} rows={rows} />
      </div>
    )
}

export default RenderDataTable

Row Details

This example has row details:

import * as React from 'react'

import { DataTable } from '@jetblack/material-data-table'

const RowDetailDataTable = () => {
  const columns = [
    {
      id: 'name',
      title: 'Name',
      align: 'left',
    },
    {
      id: 'band',
      title: 'Band',
      align: 'left',
      getValue: row => row['band'],
    },
    {
      id: 'founded',
      title: 'Founded',
      align: 'right',
      formatValue: (value, row) => `${row.band} founded in ${value}`,
    },
  ]
  const rows = [
    { name: 'Jimmy Page', band: 'Led Zeppelin', founded: 1968 },
    { name: 'Marc Bolan', band: 'T. Rex', founded: 1967 },
  ]

  return (
    <div>
      <DataTable
        columns={columns}
        rows={rows}
        rowDetail={(row, columns) => <div>This is about {row.band}</div>}
        rowsPerPage={5}
        rowsPerPageOptions={[5, 10]}
      />
    </div>
  )
}

export default RowDetailDataTable

Selection

We need to maintain state of the selected rows.

import * as React from 'react'

import { DataTable } from '@jetblack/material-data-table'

class SelectableDataTable extends React.Component {
  state = {
    columns: [
      {
        id: 'name',
        title: 'Name',
        align: 'left',
      },
      {
        id: 'band',
        title: 'Band',
        align: 'left',
        getValue: row => row['band'],
      },
      {
        id: 'founded',
        title: 'Founded',
        align: 'right',
        formatValue: (value, row,) => `${row.band} founded in ${value}`,
      },
    ],
    rows: [
      { name: 'Jimmy Page', band: 'Led Zeppelin', founded: 1968 },
      { name: 'Marc Bolan', band: 'T. Rex', founded: 1967 },
    ],
    selected: []
  }

  render() {
    const { columns, rows, selected } = this.state
    return (
      <DataTable
        size='small'
        padding='none'
        columns={columns}
        rows={rows}
        selected={selected}
        onSelectionChanged={selected => this.setState({ selected })}
        isSelectable={true}
        rowsPerPage={5}
        rowsPerPageOptions={[5, 10]}
      />
    )
  }
}

export default SelectableDataTable

Filtering

We need to provide a filter text box, and maintain the state of the filter text.

import * as React from 'react'

import IconButton from "@mui/material/IconButton"
import ClearIcon from "@mui/icons-material/Clear"
import InputAdornment from "@mui/material/InputAdornment"
import TextField from "@mui/material/TextField"

import { DataTable } from '../index'


const FilterTextField = ({text, title, onChange}) => (
  <TextField
    sx={{ width: 300 }}
    label={title}
    value={text}
    InputProps={{
      endAdornment: (
        <InputAdornment position="end">
          <IconButton onClick={() => onChange("")}>
            <ClearIcon />
          </IconButton>
        </InputAdornment>
      ),
    }}
    onChange={(event) => onChange(event.target.value)}
  />
)

class FilteredDataTable extends React.Component {
  state = {
    columns: [
      {
        id: 'name',
        title: 'Name',
        align: 'left',
      },
      {
        id: 'band',
        title: 'Band',
        align: 'left',
        getValue: (row, _column, _columns) => row['band'],
      },
      {
        id: 'founded',
        title: 'Founded',
        align: 'right',
        formatValue: (value, row, _column, _columns) => `${row.band} founded in ${value}`,
      },
    ],
    rows: [
      { name: 'Jimmy Page', band: 'Led Zeppelin', founded: 1968 },
      { name: 'Marc Bolan', band: 'T. Rex', founded: 1967 },
    ],
    filterText: ''
  }

  render() {
    const { columns, rows, filterText } = this.state
    return (
      <div>
        <div>
          <FilterTextField
            title="Filter"
            text={filterText}
            onChange={(filterText) => this.setState({ filterText })}
          />
        </div>

        <DataTable
          columns={columns}
          rows={rows}
          filterText={filterText}
          paginate={false}
        />
      </div>
    )
  }
}

export default FilteredDataTable
6.0.1

11 months ago

6.0.0

2 years ago

6.0.0-rc.1

2 years ago

5.6.0

2 years ago

6.0.0-rc.0

2 years ago

5.5.0

2 years ago

6.0.0-rc.2

2 years ago

5.4.0

2 years ago

5.3.1

2 years ago

5.3.0

2 years ago

5.2.0

2 years ago

5.1.0

2 years ago

5.0.0

2 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.2

3 years ago

0.3.0

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago