Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/table.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Props
* Fields
* [`fieldMap`](./table/field-map.md): Map fields to be displayed as headers
* [`fieldOrder`](./table/field-order.md): Define the order in which fields are displayed
* [`fieldWidths`](./table/field-widths.md): Define the width for each field
* [`fieldsToExclude`](./table/fields-to-exclude.md): Blacklist fields to display
* [`fieldsToInclude`](./table/fields-to-include.md): Whitelist fields to display
* Extras
Expand Down
26 changes: 26 additions & 0 deletions docs/table/field-widths.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div align="center">
<pre>fieldWidth</pre>
</div>

<br />
<br />

The `fieldWidth` prop should be an object of attributes and integers. This allows table cell widths to be defined for each field.

```jsx
<Table
rows={[
{
id: 1,
name: 'Leanne Graham',
},
{
id: 2,
name: 'Ervin Howell',
}
]}
fieldWidth={{
id: 20
}}
/>
```
30 changes: 30 additions & 0 deletions src/helpers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,33 @@ export const Pages = ({ value, total = 1, delta = 5, onChange }) => {
</div>
)
}

export const Row = ({ key, row, fields, fieldWidths, dataManipulator, actions, isChecked, onClick, onToggle }) => (
<tr
key={key}
onClick={onClick}
>
{onToggle && (
<td>
<input
type='checkbox'
checked={isChecked}
onChange={onToggle}
/>
</td>
)}
{fields.map(field => (
<td
key={field}
width={fieldWidths[field]}
>
{dataManipulator({
field, value: row[field], row
})}
</td>
))}
{!!actions && (
<td>{actions}</td>
)}
</tr>
)
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { PageLimit, Pages } from './helpers'
import { Row, PageLimit, Pages } from './helpers'

import Table from './table'
import AjaxTable from './ajax-table'

export {
Row,
PageLimit,
Pages,
Table,
Expand Down
46 changes: 15 additions & 31 deletions src/table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useSetState, useMount, useUpdateEffect } from 'react-use'
import PropTypes from 'prop-types'
import classNames from 'classnames'

import { NOOP, PageLimit, Pages } from './helpers'
import { NOOP, Row, PageLimit, Pages } from './helpers'

const Table = (
{
Expand All @@ -15,6 +15,7 @@ const Table = (
//
fieldMap,
fieldOrder,
fieldWidths,
fieldsToExclude,
fieldsToInclude,
//
Expand Down Expand Up @@ -136,7 +137,8 @@ const Table = (

// Return headers
const $headers = () => (
state.fields.map(field => (
state.fields.map(field => ([
field,

// Field field is mapped then use the new name
// Otherwise attempt to make it friendly to read
Expand All @@ -146,7 +148,7 @@ const Table = (
.replace(/([A-Z])([A-Z])/g, (_, a, b) => `${a} ${b}`)
.toLowerCase()
.replace(/([ -_]|^)(.)/g, (_, a, b) => `${a ? ' ' : ''} ${b.toUpperCase()}`)
))
]))
)

// Only get fields once mounted
Expand Down Expand Up @@ -185,8 +187,11 @@ const Table = (
)}
</th>
)}
{$headers().map(header => (
<th key={header}>
{$headers().map(([field, header]) => (
<th
key={header}
width={fieldWidths[field]}
>
{header}
</th>
))}
Expand All @@ -204,6 +209,7 @@ const Table = (
key: id,
row,
fields: state.fields,
fieldWidths,
dataManipulator,
isChecked: rowIsChecked(id),
actions: (
Expand Down Expand Up @@ -262,38 +268,14 @@ const Table = (
)
}

Table.row = Row
Table.pageLimit = PageLimit
Table.pages = Pages

Table.defaultProps = {
rows: [],
rowIdentifier: row => row.id,
rowRenderer: ({ key, row, fields, dataManipulator, actions, isChecked, onClick, onToggle }) => (
<tr
key={key}
onClick={onClick}
>
{onToggle && (
<td>
<input
type='checkbox'
checked={isChecked}
onChange={onToggle}
/>
</td>
)}
{fields.map(field => (
<td key={field}>
{dataManipulator({
field, value: row[field], row
})}
</td>
))}
{!!actions && (
<td>{actions}</td>
)}
</tr>
),
rowRenderer: Row,
emptyRow: (
<tr>
<td>There's nothing here.</td>
Expand All @@ -313,6 +295,7 @@ Table.defaultProps = {
//
fieldMap: {},
fieldOrder: [],
fieldWidths: {},
fieldsToExclude: [],
fieldsToInclude: [],
//
Expand Down Expand Up @@ -346,6 +329,7 @@ Table.propTypes = {
PropTypes.instanceOf(RegExp)
])
),
fieldWidths: PropTypes.object,
fieldsToExclude: PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.string,
Expand Down