项目作者: langleyfoxall

项目描述 :
Re-usable data table for React with sortable columns, pagination and more.
高级语言: JavaScript
项目地址: git://github.com/langleyfoxall/react-dynamic-data-table.git
创建时间: 2018-09-18T13:17:11Z
项目社区:https://github.com/langleyfoxall/react-dynamic-data-table

开源协议:GNU Lesser General Public License v3.0

下载


React Dynamic Data Table

npm version
npm

This package provides a React Dynamic Data Table component that supports sortable columns,
pagination, field mapping, data manipulation, and more.

Installation

You can install this package with either npm or yarn as shown below.

  1. npm install @code-based/react-dynamic-data-table
  1. yarn add @code-based/react-dynamic-data-table

Remember to import the DynamicDataTable component where it is needed.

  1. import DynamicDataTable from "@code-based/react-dynamic-data-table";

Usage

At its most basic, you can create a new <DynamicDataTable ></DynamicDataTable> with just the rows prop.

  1. <DynamicDataTable rows={this.state.users} ></DynamicDataTable>

The rows prop expects an array of objects, such as the following.

  1. [
  2. { name: "Picard", email: "picard@enterprise-d.com" },
  3. { name: "Kirk", email: "kirk@enterprise-a.com" },
  4. { name: "Sisko", email: "sisko@deep-space-9.com" }
  5. ]

Specifying a CSS class for the table

By default tables are assigned the bootstrap table and table-striped CSS classes.
If you need a different table style, you can override these defaults by providing the
className prop:

  1. <DynamicDataTable
  2. className="table table-sm"
  3. ></DynamicDataTable>

Excluding fields

By default, React Dynamic Data Table will render a table containing all fields present
in the rows prop. To exclude specific fields, you can use the fieldsToExclude props.

In the example below, the email field will be excluded.

  1. <DynamicDataTable
  2. rows={this.state.users}
  3. fieldsToExclude={['email']}
  4. ></DynamicDataTable>

In the example below, all ID fields will be excluded.

  1. <DynamicDataTable
  2. rows={this.state.users}
  3. fieldsToExclude={[/_?id/]}
  4. ></DynamicDataTable>

The fieldsToExclude prop expects an array of strings or regex expressions that represent the fields to exclude.

Mapping fields

By default, React Dynamic Data Table creates table headers based on the field name,
with underscores replaced with spaces and each word’s first letter converted to uppercase.
You can override this behaviour with a field map.

In the example below, you can render the email field as ‘Email Address’.

  1. <DynamicDataTable
  2. rows={this.state.users}
  3. fieldMap={{ email: 'Email address' }}
  4. ></DynamicDataTable>

The fieldMap prop expects an object which maps the rows keys to alternative field names.

Ordering data

The React Dynamic Data Table will display the rows in the order they are provided
in the array. However, it is possible to show, in the column header, that the data
has been sorted.

In the example below, the name column header will show a down arrow indicating
that the data has been sorted by name (ascending).

  1. // this.state.orderByField = 'name';
  2. // this.state.orderByDirection = 'asc';
  3. <DynamicDataTable
  4. rows={this.state.users}
  5. orderByField={this.state.orderByField}
  6. orderByDirection={this.state.orderByDirection}
  7. ></DynamicDataTable>

The orderByField prop expects a string indicating the field to sort by (one of the
keys from the rows object).

The orderByDirection expects either asc or desc, meaning ascending or descending
respectively.

If you wish to let the end-user sort the data table by clicking on the column
headings, you can use the changeOrder prop. This is shown in the example below.

  1. // this.state.orderByField = 'name';
  2. // this.state.orderByDirection = 'asc';
  3. <DynamicDataTable
  4. rows={this.state.users}
  5. orderByField={this.state.orderByField}
  6. orderByDirection={this.state.orderByDirection}
  7. changeOrder={(field, direction) => this.changeOrder(field, direction)}
  8. />
  1. changeOrder(field, direction) {
  2. this.setState({ orderByField: field, orderByDirection: direction }, () => {
  3. const users = /* Get sorted data from API endpoint */
  4. this.setState({ users: users });
  5. });
  6. }

The changeOrder prop expects a callable. This callable should:

  1. Change the orderByField and orderByDirection props, based on the passed field
    and direction parameters respectively.
  2. Change / re-retrieve the rows prop, such that it is sorted based on the passed
    field and direction parameters.

Ordering data is enabled for all fields by default. However, if you wish to restrict
which fields the ordering is enabled for, pass an array of the field names into the
allowOrderingBy prop. An example of this is shown below.

  1. <AjaxDynamicDataTable
  2. rows={this.state.users}
  3. allowOrderingBy={[
  4. 'name', 'email'
  5. ]}
  6. ></AjaxDynamicDataTable>

To have the opposite effect simply use disallowOrderingBy:

  1. <AjaxDynamicDataTable
  2. rows={this.state.users}
  3. disallowOrderingBy={[
  4. 'dob'
  5. ]}
  6. ></AjaxDynamicDataTable>

Typically, the allowOrderingBy and disallowdOrderingBy props should not be used simultaneously, as this could cause unexpected behaviour.

Ordering fields

By default fields will be ordered as they are passed into the table on each row.
To force a specific ordering of columns an array of strings or regex can be passed
with the fieldOrder prop. Anything that is not included within fieldOrder will
be pushed to the end of the ordered fields.

  1. <DynamicDataTable
  2. rows={[
  3. { id: 1, email: 'info@codebased.co.uk', name: 'Codebased' }
  4. ]}
  5. fieldOrder={[
  6. 'id', 'name'
  7. ]}
  8. ></DynamicDataTable>
  9. // Output: id, name, email

Mixing strings and regex is also supported.

  1. <DynamicDataTable
  2. rows={[
  3. { id: 1, email: 'info@codebased.co.uk', first_name: 'Code', last_name: 'Based' }
  4. ]}
  5. fieldOrder={[
  6. 'id', /_name/
  7. ]}
  8. ></DynamicDataTable>
  9. // Output: id, first_name, last_name, email

Column Widths

In some cases there may be a need to make some columns be different widths
by defining a width, rather than the table changing based off of content.

The columnWidths prop expects an object with column names as the keys and either a string or number as the values.

When a number is passed the width will become a percentage. If a string is passed then
it respects whatever unit is set.

  1. <DynamicDataTable
  2. rows={[
  3. { id: 1, email: 'info@codebased.co.uk', first_name: 'Code', last_name: 'Foxall' }
  4. ]}
  5. columnWidths={{
  6. // 10%
  7. id: 10,
  8. // 100px
  9. email: '100px'
  10. }}
  11. ></DynamicDataTable>

Custom order by icons

When ordering by a field on an element will be rendered next to it. By default
these are simple symbols ( and ). These can be changed by passing a valid
node into orderByAscIcon and orderByDescIcon.

  1. <DynamicDataTable
  2. orderByAscIcon="Ascending"
  3. // orderByAscIcon={<p>Ascending</p>}
  4. // orderByAscIcon={<FancyAscendingIcon ></FancyAscendingIcon>}
  5. />

You can optionally specify an icon to appear when a sortable field is not the
currently sorted field using the orderByIcon prop:

  1. <DynamicDataTable
  2. orderByIcon="Sortable"
  3. // orderByAscIcon={<p>Sortable</p>}
  4. // orderByAscIcon={<FancySortableIcon ></FancySortableIcon>}
  5. />

By default the order by icon will appear following a non-breaking space after
the column label. You can instead prepend the icon by specifying the
prependOrderByIcon prop; this is particularly useful if you are using css
float to position the icon as it will not flow under the text:

  1. <DynamicDataTable
  2. orderByIcon={<i className="mt-1 fad fa-sort float-right"></i>}
  3. orderByAscIcon={<i className="mt-1 fad fa-sort-up float-right"></i>}
  4. orderByDescIcon={<i className="mt-1 fad fa-sort-down float-right"></i>}
  5. prependOrderByIcon
  6. />

Pagination

Making pagination work with React Dynamic Data Table requires three extra
props. These are the currentPage, totalPages and changePage props. Once
these props are set correctly, a Bootstrap style pagination will be displayed
below the table.

The currentPage prop expects an integer representing the current page number
(one or above).

The totalPages prop expects an integer representing the total number of
pages in the data set (one or above). Pagination will only be shown if the
total number of pages is greater than one.

The changePage props expect a callable with a page argument, indicating the
new page number to load. This callable should:

  1. Load a new page of data into the rows prop based on the passed page
    argument.
  2. Set the currentPage prop to be equal to the passed page argument.

A example of this is shown below:

  1. // this.state.currentPage = 1;
  2. // this.state.totalPages = 5;
  3. <DynamicDataTable
  4. rows={this.state.users}
  5. currentPage={this.state.currentPage}
  6. totalPages={this.state.totalPages}
  7. changePage={page => this.changePage(page)}
  8. />
  1. changePage(page) {
  2. const users = /* Get page of data from API endpoint */
  3. this.setState({ users: users, currentPage: page });
  4. }

Pagination is dynamic, showing only a select subset of the available pages as actual buttons.
Whether or not an individual button to a page is shown depends on if it is the first or last page (these are always shown), and whether it is within a predefined offset from the current page (a pagination delta).
This delta can be changed by passing a paginationDelta prop into DynamicDataTable as shown below:

  1. <DynamicDataTable
  2. rows={this.state.users}
  3. currentPage={this.state.currentPage}
  4. totalPages={this.state.totalPages}
  5. paginationDelta={6}
  6. ></DynamicDataTable>

Always showing pagination controls

By default the pagination controls are only shown if there are two or more pages of data to be displayed. You can override this behaviour by simply passing the alwaysShowPagination prop:

  1. <DynamicDataTable
  2. alwaysShowPagination
  3. ></DynamicDataTable>

Per page limiting

Changing the number of entries displaying in the data table is easy. The totalRows, perPage, changePerPage and perPageRenderer allow you to customize a per page limit control.

  • totalRows the total number of rows within the dataset
  • perPage the current per page limit (default: 15)
  • changePerPage handles the logic for changing the perPage prop. This recieved a single argument which is the new limit.
  • perPageOptions the results per page options (default: [10, 15, 30, 50, 75, 100])
  • perPageRender can either be a node or a function.

By default a Bootstrap styled select is displayed if changePerPage is a function.

  1. <DynamicDataTable
  2. totalRows={totalRows}
  3. perPage={perPage}
  4. changePerPage={newPerPage => (
  5. this.setState({
  6. perPage: newPerPage
  7. })
  8. )}
  9. perPageRenderer={props => (
  10. <PerPage {...props} ></PerPage>
  11. )}
  12. />

perPageRenderer

The perPageRenderer prop accepts either a node or function. If a valid react element is passed then React.cloneElement is used to bind:

  • totalRows
  • value (see perPage above)
  • onChange (see changePerPage above)
  • perPageOptions

If a function is passed then the props described above are passed in an object.

Row buttons

Row buttons appear on the right hand side of every row in the React Dynamic Data
Table. By default, a ‘View’ button is provided, which simply links the user to
the current URL with the row’s id appended.

You can completely override the row buttons that are displayed by provided a
buttons prop. This prop expects an array of objects, each containing a name
and callback.

The name is string, such as ‘View’, ‘Edit’, ‘Delete’, etc.

The callback is a callable with a two arguments. The first is the event object
for the button clicked and the second is an object representing the current row.

An example of setting custom row buttons is shown below.

  1. <DynamicDataTable
  2. rows={this.state.users}
  3. buttons={[
  4. {
  5. name: 'Edit',
  6. callback: (event, user) => {
  7. // Show edit user view...
  8. }
  9. },
  10. {
  11. name: 'Delete',
  12. callback: (event, user) => {
  13. // Delete user...
  14. }
  15. }
  16. ]}
  17. />

buttons can also be given a custom render at the top level, or for multiple array elements.
It’s worth noting that multiple array elements still respect the dropdown menu.

  1. // Top level example
  2. <DynamicDataTable
  3. buttons={row => (
  4. <a>
  5. <i className="fas fa-fw fa-eye" ></i>
  6. <span>Totally custom button</span>
  7. </a>
  8. )}
  9. />
  10. // Low level example
  11. <DynamicDataTable
  12. buttons={[
  13. {
  14. render: row => (
  15. <a>
  16. <i className="fas fa-fw fa-eye" ></i>
  17. <span>Totally custom button 1</span>
  18. </a>
  19. )
  20. },
  21. {
  22. render: row => (
  23. <a>
  24. <i className="fas fa-fw fa-tick" ></i>
  25. <span>Totally custom button 2</span>
  26. </a>
  27. )
  28. }
  29. ]}
  30. />

Rendering custom rows

If you come across a situation where the automatically generated rows are not suitable for your project
you can use the rowRenderer prop. This prop expects a callable that receives a single argument,
and returns a valid React element, which should be a <tr> element.

The argument passed to the rowRenderer callable is a JavaScript object that contain the following properties.

  1. {
  2. row, // Instance of data row
  3. onClick, // Row on click handler
  4. onMouseUp, // Row on MouseUp handler
  5. onMouseDown, // Row on MouseDown handler
  6. buttons, // Array of buttons
  7. actions, // Array of header actions
  8. fields, // Visible fields
  9. renderCheckboxes, // Boolean indicating whether to render checkboxes
  10. disableCheckbox, // Boolean indicating whether to disable the checkbox per row
  11. checkboxIsChecked, // Boolean indicating if checkbox is checked
  12. onCheckboxChange, // Callable that is called when a per row checkbox is changed
  13. dataItemManipulator // Callable that handles manipulation of every item in the data row
  14. }

For implementation details regarding these properties, see the other relevant areas of the documentation.

Clickable rows

Clickable rows allows an onClick prop to be passed. This should be a callable, that will be passed an event object along with
an instance of the row that is clicked. It also adds the bootstrap table-hover class onto the table.

  1. <DynamicDataTable
  2. rows={this.state.users}
  3. onClick={(event, row) => console.warn(event, row.name)}
  4. />

Mouse Events

For more complex interactions, such as supporting the ability to Middle-click, you can use the onMouseUp and onMouseDown events instead. It also adds the bootstrap table-hover class onto the table. The onMouseDown and onMouseUp props should be callables, that will be passed an event object along withan instance of the row that is clicked.

  1. <DynamicDataTable
  2. rows={this.state.users}
  3. onMouseDown={this.handleMouseDown}
  4. onMouseUp={this.handleMouseUp}
  5. ></DynamicDataTable>

Context Menus

The ability to right click rows can be enabled by using onContextMenu and rowRenderer.
In the example we will use our own @code-based/react-dynamic-context-menu:

  1. <DynamicDataTable
  2. rows={this.state.users}
  3. rowRenderer={options => (
  4. <DynamicContextMenu
  5. key={options.key}
  6. data={options.row}
  7. menuItems={[
  8. {
  9. label: 'Update',
  10. onClick: this.handleUpdate,
  11. },
  12. {
  13. label: 'Delete',
  14. onClick: this.handleDelete,
  15. },
  16. ]}
  17. >
  18. {DynamicDataTable.rowRenderer(options)}
  19. </DynamicContextMenu>
  20. )}
  21. />

DynamicContextMenu clones the child and adds onContextMenu as a prop. This can also be achieved manually.

  1. <DynamicDataTable
  2. rows={this.state.users}
  3. rowRenderer={({ row }) => (
  4. <tr onContextMenu={() => this.onContextMenu(row)}>
  5. <td></td>
  6. </tr>
  7. )}
  8. />

Hoverable table rows

To enable a hover effect on rows even if onClick is not passed into the table you can use the prop hoverable.
This will add a background color on each row when hovered.

  1. <DynamicDataTable
  2. rows={this.state.users}
  3. hoverable
  4. ></DynamicDataTable>

Render no data component

If you wish to render something other than the table when no rows are present you can take advantage of
noDataComponent which accepts a valid react element. This will replace the table until there are rows.

  1. <DynamicDataTable
  2. row={[]}
  3. noDataComponent={(
  4. <p>I replace the table, not just the text inside it.</p>
  5. )}
  6. />

Bulk select checkboxes

If you wish to allow users to bulk select users in a React Dynamic Data Table,
you can specify the renderCheckboxes prop. This will render a series of
checkboxes against each row, on the left side of the table.

  1. <DynamicDataTable
  2. rows={this.state.users}
  3. renderCheckboxes
  4. ></DynamicDataTable>

Bulk select checkboxes are usually combined with bulk actions to perform
actions on one or more rows at once.

Disable checkboxes

Checkboxes can also be disabled for each individual row by passing in disabledCheckboxes which
should container an array of identifiers. If an identifier is in the array then the checkbox will have disabled set to true.

  1. <DynamicDataTable
  2. rows={this.state.users}
  3. renderCheckboxes
  4. disabledCheckboxes={[1]}
  5. ></DynamicDataTable>

Externally manage checkboxes

Combining isCheckboxChecked, onMasterCheckboxChange and onCheckboxChange allows a row’s checkbox state to be managed outside of the datatable while still allowing disabledCheckboxes to work as intended.

In the example below, we are using a Set named checked to store the current status of the data table’s checkboxes.

  1. const checked = new Set
  2. <DynamicDataTable
  3. isCheckboxChecked={({ id }) => checked.has(id)}
  4. onMasterCheckboxChange={(_, rows) => {
  5. let all = true
  6. rows.forEach(({ id }) => checked.has(id) || all = false)
  7. rows.forEach(({ id }) => {
  8. if (all) {
  9. checked.delete(id)
  10. } else if (!checked.has(id)) {
  11. checked.add(id)
  12. }
  13. })
  14. }}
  15. onCheckboxChange={(_, { id }) => {
  16. if (checked.has(id)) {
  17. checked.delete(id)
  18. } else {
  19. checked.add(id)
  20. }
  21. }}
  22. />

isCheckboxChecked

isCheckboxChecked is called on each re-render of the data table allowing for custom logic to determine if a checkbox is checked. It will receive the current row and the visible rows as arguments.

  1. const checked = new Set
  2. <DynamicDataTable
  3. isCheckboxChecked={({ id }) => checked.has(id)}
  4. />

Note: This should only be used if onMasterCheckboxChange and onCheckboxChange are implemented.

onMasterCheckboxChange

onMasterCheckboxChange is called when the master checkbox is clicked. This allows for custom logic for selecting and deselecting multiple rows. It will receive an event object from the input and the visible rows.

The master checkbox refers to the bulk select checkbox found in the top left corner of the data table whenever checkboxes are enabled.

  1. const checked = new Set
  2. <DynamicDataTable
  3. onMasterCheckboxChange={(_, rows) => {
  4. let all = true
  5. rows.forEach(({ id }) => checked.has(id) || all = false)
  6. rows.forEach(({ id }) => {
  7. if (all) {
  8. checked.delete(id)
  9. } else if (!checked.has(id)) {
  10. checked.add(id)
  11. }
  12. })
  13. }}
  14. />

renderMasterCheckbox

renderMasterCheckbox will determine if the master checkbox will be rendered

The master checkbox refers to the bulk select checkbox found in the top left corner of the data table whenever checkboxes are enabled.

  1. <DynamicDataTable
  2. renderMasterCheckbox
  3. ></DynamicDataTable>

onCheckboxChange

onCheckboxChange is called when a row checkbox is clicked. This allows for custom logic for selecting and deselecting a single row. It will receive an event object from the input and the current row.

  1. const checked = new Set
  2. <DynamicDataTable
  3. onCheckboxChange={(_, { id }) => {
  4. if (checked.has(id)) {
  5. checked.delete(id)
  6. } else {
  7. checked.add(id)
  8. }
  9. }}
  10. />

Actions

Actions, when combined with bulk select checkboxes allow you perform
actions of multiple rows at once. When in use, a menu will be rendered
in the top right of the table allowing your users to choose a bulk action
that will be applied to the selected rows.

Actions can also be used without bulk select checkboxes. This could allow for creation of action buttons that are not dependant on existing data, such as a ‘Create User’ button.

To use actions in your React Dynamic Data Table, you must specify the
actions props. This prop expects an array of objects, each containing a name
and callback.

The name is string, such as ‘Delete user(s)’, ‘Duplicate user(s)’ etc.

The callback is a callable with a single argument. The argument will
contain an array of the selected rows.

Examples of how to use actions is shown below.

  1. <DynamicDataTable
  2. rows={this.state.users}
  3. renderCheckboxes
  4. actions={[
  5. {
  6. name: 'Delete user(s)',
  7. callback: (rows) => {
  8. // Delete users...
  9. },
  10. },
  11. ]}
  12. />
  1. <DynamicDataTable
  2. rows={this.state.users}
  3. actions={[
  4. {
  5. name: 'Create user',
  6. callback: () => {
  7. // Toggle create user modal...
  8. },
  9. },
  10. ]}
  11. />

Data Item Manipulation

If you wish to alter row data prior to it being rendered, you may use the dataItemManipulator prop available on the
DynamicDataTable. This prop expects a function which will be passed three parameters, the field, the value and
the row.

This function will be called once for every cell that is to be rendered.

  1. <DynamicDataTable
  2. dataItemManipulator={(field, value, row) => {
  3. switch(field) {
  4. case 'id':
  5. return 'ID:' + value;
  6. case 'reference':
  7. return value.toUpperCase();
  8. }
  9. return value;
  10. }}
  11. />

It is also possible to render React components directly, by returning them from this function.

  1. <DynamicDataTable
  2. dataItemManipulator={(field, value) => {
  3. switch(field) {
  4. case 'reference':
  5. return <ExampleComponent exampleProp={value} ></ExampleComponent>;
  6. }
  7. return value;
  8. }}
  9. />

If you wish, you can dangerously render HTML directly by returning a string from the dataItemManipulator, you will
however need to explicitly specify which fields this should be enabled for. This is done by using the
dangerouslyRenderFields prop.

  1. <DynamicDataTable
  2. dangerouslyRenderFields={['check']}
  3. dataItemManipulator={(field, value) => {
  4. switch(field) {
  5. case 'check':
  6. return "<i class='fa fa-check'></i>";
  7. }
  8. return value;
  9. }}
  10. />

To display extra data at the bottom of the table a function or node can be passed into the React Dynamic Data Table by using the footer prop.

The footer is displayed directly in a tfoot to allow for multiple rows. So don’t forget your trs.

Passing a function

When passing a function into the footer prop it receives an object with:

  • rows: All of the visible rows
  • width: The number of columns in the table

This should return a valid React element.

  1. <DynamicDataTable
  2. footer={({ rows, width }) => (
  3. <tr>
  4. <td colSpan={width}>
  5. Table footer.
  6. <td>
  7. </tr>
  8. )}
  9. />

Passing a node

When passing a node or valid React element it is simply output.

  1. <DynamicDataTable
  2. footer={(
  3. <tr>
  4. <td>
  5. Table footer.
  6. </td>
  7. </tr>
  8. )}
  9. />

Loading message & indicator

By default, the React Dynamic Data Table will not show indication that it is
loading. On slow connections, this may make the table appear unresponsive or
sluggish when initialing loading, changing pages, re-ordering, and so on.

To show a loading message, you can set the loading prop to true. This will
display a default loading message, which can be changed by altering passing
a string into the optional loadingMessage prop. If you wish, you can also
pass a React component into the loadingIndicator prop, which will be displayed
above the textual loading message.

  1. <DynamicDataTable
  2. loading={true}
  3. loadingMessage="User data is now loading..."
  4. loadingIndicator={(
  5. <img src="/loading-animation.gif">
  6. )}
  7. />

Alternatively, if you wish to replace the entire table while data is being loaded,
you can pass a React component into the loadingComponent prop.

  1. <DynamicDataTable
  2. loading={true}
  3. loadingComponent={(
  4. <p>I replace the table, not just the text inside it.</p>
  5. )}
  6. />

To display either of these options loading must be set to true. Note that the
AJAX Dynamic Data Table handles the loading prop internally but can be overriden.

Error message

In the case that something goes wrong, such as data failing to load, you
can display and error message in place of the normal React Dynamic
Data Table output.

In order to display an error message, you just need to set the optional
errorMessage prop. This prop expects a string such as An error has occurred while loading user data.. If the error is resolved, this prop must be reset
to an empty string in order to ensure the data table is displayed.

Editable Columns

If you wish to make certain columns editable you can specify how using the editableColumns prop.
This prop accepts an array of object in the following format:

  1. [
  2. {
  3. name: 'ExampleColumnText',
  4. controlled: false,
  5. type: 'text',
  6. onChange: (event, column, row, index) => console.log(event, column, row, index),
  7. },
  8. {
  9. name: 'ExampleColumnSelect',
  10. controlled: false,
  11. type: 'select',
  12. onChange: (event, column, row, index) => console.log(event, column, row, index),
  13. optionsForRow: (row, column) => [
  14. {
  15. label: 'One',
  16. value: 1
  17. },
  18. {
  19. label: 'Two',
  20. value: 2
  21. }
  22. ]
  23. }]

Text Inputs

If you specify that the type of the column is text the column will contain a text input with a value of the
column from the row data.

Selects

If you wish to use a select instead of a text input you may specify select as the type. The column will now contain
a select input, by default with no options. In order to provide options implement the optionsForRow method.
This method will be called with: The row data and Column name in that order. It should return an array of objects
in this format:

  1. [
  2. {
  3. label: 'Example one',
  4. value: 1
  5. },
  6. {
  7. label: 'Example two',
  8. value: 2
  9. }
  10. ]

Receiving input

In order to receive the users input you can provide the onChange method that will be called when the input is changed.
This method will be called with the following parameters in the given order:
The event from the input, The column name, The row data, The row index.

Controlled and Uncontrolled inputs

A uncontrolled input is an input whose value is controlled by the DOM. This means that it cannot be modified
after the default value has been set by React. You will only receive input from the component and will not be able
to modify the displayed value.

A controlled input will require you to store the value of the input in the state, the value of the input will be
read from state meaning you will have to update state on user input to reflect it in component. In this case
it will mean you will have to alter the data passed in as the rows prop.

Indicating active row(s)

If you supply an rowIsActive predicate prop to the data table, any row
matching the predicate is given the CSS class table-active:

  1. <DynamicDataTable
  2. rowIsActive={(row) => row.id === 3}
  3. />

Development

Linting

This package uses ESLint for linting of JS/X. You can run ESLint at any time by executing npm run-script lint-fix. Install the ESLint extension to get real-time linting in Visual Studio Code.

These linters additionally run in continuous integration as a GitHub Action.

Releases

To release a new version of this package, simply create a new release.