Skip to content

Commit 5a6d0b9

Browse files
authored
Merge pull request #3 from ninetynine/canary
v1.0.0-canary.2
2 parents 8dfa8a9 + 26268f1 commit 5a6d0b9

File tree

8 files changed

+68
-43
lines changed

8 files changed

+68
-43
lines changed

dist/table.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/table.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ Props
3535
* Checkboxes
3636
* [`rowIsChecked`](./table/row-is-checked.md): Define a callback to determine if a row checkbox is checked
3737
* [`masterIsChecked`](./table/master-is-checked.md): Define a callback to determine if the master checkbox is checked
38-
* [`onRowCheckboxChange`](./table/on-row-checkbox-change.md): Define a row checkbox change callback
39-
* [`onMasterCheckboxChange`](./table/on-master-checkbox-change.md):
38+
* [`onRowToggle`](./table/on-row-toggle.md): Define a row checkbox change callback
39+
* [`onMasterToggle`](./table/on-master-toggle.md):
4040
Define a master checkbox change callback

docs/table/actions.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
<br />
66
<br />
77

8-
The `actions` prop is used to be appended on the end of each row when using the default implementation of the [`rowRenderer`](./row-renderer.md). This expects a valid React element.
8+
The `actions` prop is used to be appended on the end of each row when using the default implementation of the [`rowRenderer`](./row-renderer.md). This expects a function which gets a single argument being the row object and should return a valid React element.
99

1010
```jsx
11+
import { useSet } from 'react-use'
12+
13+
const [checked, { has, toggle }] = useSet()
14+
1115
<Table
12-
actions={(
13-
<a>
14-
Click Here
16+
actions={id => (
17+
<a onClick={() => toggle(id)}>
18+
{has(id) ? 'Unflag' : 'Flag'}
1519
</a>
1620
)}
1721
/>

docs/table/data-manipulator.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@
55
<br />
66
<br />
77

8-
The `dataManipulator` prop is used to manipulate field values before they are displayed. This expects a function.
8+
The `dataManipulator` prop is used to manipulate field values before they are displayed. This expects a function that should return a valid React element.
99

1010
_Default implementation:_
1111
```jsx
1212
<Table
13-
dataManipulator={({ value }) => (
14-
value !== null && value !== undefined
15-
? value : '-'
16-
)}
13+
dataManipulator={({ value }) => {
14+
if (value === null || value === undefined) {
15+
return '-'
16+
}
17+
18+
if (!React.isValidElement(value) && typeof value === 'object') {
19+
return <i>Hidden</i>
20+
}
21+
22+
return value
23+
}}
1724
/>
1825
```

docs/table/on-master-checkbox-change.md renamed to docs/table/on-master-toggle.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<div align="center">
2-
<pre>onMasterCheckboxChange</pre>
2+
<pre>onMasterToggle</pre>
33
</div>
44

55
<br />
66
<br />
77

8-
When wanting to use a master checkboxe this is one of two requirements. The other is the [`masterIsChecked`](./master-is-checked.md) prop.
8+
When wanting to use a master checkbox this is one of two requirements. The other is the [`masterIsChecked`](./master-is-checked.md) prop.
99

10-
The `onMasterCheckboxChange` prop should be a function. That controls a _master_ checkbox that is displayed in the table header.
10+
The `onMasterToggle` prop should be a function. That controls a _master_ checkbox that is displayed in the table header.
1111

1212
This should be used to toggle multiple row checkboxes. To determine if this checkbox is checked or not use [`masterIsChecked`](./master-is-checked.md).
1313

@@ -18,7 +18,7 @@ const [state, setState] = useSetState({ rows: [] })
1818
const [checked, { has, add }] = useSet()
1919

2020
<Table
21-
onMasterCheckboxChange={() => {
21+
onMasterToggle={() => {
2222
state.rows.forEach(row => {
2323
if (!has(row.id)) {
2424
add(row.id)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<div align="center">
2-
<pre>onRowCheckboxChange</pre>
2+
<pre>onRowToggle</pre>
33
</div>
44

55
<br />
66
<br />
77

88
When wanting to use checkboxes this is one of two requirements. The other is the [`rowIsChecked`](./row-is-checked.md) prop.
99

10-
The `onRowCheckboxChange` prop should be a function that accepts a single argument, which is the [row identifier](./row-identifier.md).
10+
The `onRowToggle` prop should be a function that accepts a single argument, which is the [row identifier](./row-identifier.md).
1111

1212
This should be used to toggle if a row's checkbox is checked or not by using the [`rowIsChecked`](./row-is-checked.md) prop.
1313

@@ -17,6 +17,6 @@ import { useSet } from 'react-use'
1717
const [checked, { toggle }] = useSet()
1818

1919
<Table
20-
onRowCheckedChange={id => toggle(id)}
20+
onRowToggle={id => toggle(id)}
2121
/>
2222
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ninetynine/react-table",
3-
"version": "1.0.0-canary.1",
3+
"version": "1.0.0-canary.2",
44
"description": "A package that provides a managed React table for both static and dynamic data",
55
"author": "Dexter Marks-Barber <[email protected]>",
66
"license": "LGPL-3.0-only",

src/table.jsx

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ const Table = (
2828
masterIsChecked,
2929
//
3030
onRowClick,
31-
onRowCheckboxChange,
31+
onRowToggle,
3232
onPageLimitChange,
3333
onPageChange,
34-
onMasterCheckboxChange,
34+
onMasterToggle,
3535
//
3636
className,
3737
...rest
@@ -174,13 +174,13 @@ const Table = (
174174
{header && (
175175
<thead>
176176
<tr>
177-
{onRowCheckboxChange !== NOOP && (
177+
{onRowToggle !== NOOP && (
178178
<th>
179-
{onMasterCheckboxChange !== NOOP && (
179+
{onMasterToggle !== NOOP && (
180180
<input
181181
type='checkbox'
182182
checked={masterIsChecked()}
183-
onChange={onMasterCheckboxChange}
183+
onChange={onMasterToggle}
184184
/>
185185
)}
186186
</th>
@@ -190,7 +190,7 @@ const Table = (
190190
{header}
191191
</th>
192192
))}
193-
{actions !== null && <th />}
193+
{actions !== NOOP && <th />}
194194
</tr>
195195
</thead>
196196
)}
@@ -205,15 +205,18 @@ const Table = (
205205
row,
206206
fields: state.fields,
207207
dataManipulator,
208-
actions,
209208
isChecked: rowIsChecked(id),
209+
actions: (
210+
actions !== NOOP
211+
? actions(row) : undefined
212+
),
210213
onClick: (
211214
onRowClick !== NOOP
212-
? onRowClick : undefined
215+
? event => onRowClick(id, event) : undefined
213216
),
214217
onToggle: (
215-
onRowCheckboxChange !== NOOP
216-
? () => onRowCheckboxChange(id) : undefined
218+
onRowToggle !== NOOP
219+
? () => onRowToggle(id) : undefined
217220
)
218221
})
219222
)
@@ -226,8 +229,8 @@ const Table = (
226229
rows,
227230
width: (
228231
state.fields.length +
229-
parseInt(actions !== null) +
230-
parseInt(onRowCheckboxChange !== NOOP)
232+
parseInt(actions !== NOOP) +
233+
parseInt(onRowToggle !== NOOP)
231234
)
232235
})
233236
)}
@@ -286,18 +289,27 @@ Table.defaultProps = {
286289
})}
287290
</td>
288291
))}
289-
{actions}
292+
{!!actions && (
293+
<td>{actions}</td>
294+
)}
290295
</tr>
291296
),
292297
emptyRow: (
293298
<tr>
294299
<td>There's nothing here.</td>
295300
</tr>
296301
),
297-
dataManipulator: ({ value }) => (
298-
value !== null && value !== undefined
299-
? value : '-'
300-
),
302+
dataManipulator: ({ value }) => {
303+
if (value === null || value === undefined) {
304+
return '-'
305+
}
306+
307+
if (!React.isValidElement(value) && typeof value === 'object') {
308+
return <i>Hidden</i>
309+
}
310+
311+
return value
312+
},
301313
//
302314
fieldMap: {},
303315
fieldOrder: [],
@@ -308,16 +320,16 @@ Table.defaultProps = {
308320
footer: NOOP,
309321
pageLimit: NOOP,
310322
pages: NOOP,
311-
actions: null,
323+
actions: NOOP,
312324
//
313325
rowIsChecked: NOOP,
314326
masterIsChecked: NOOP,
315327
//
316328
onRowClick: NOOP,
317-
onRowCheckboxChange: NOOP,
329+
onRowToggle: NOOP,
318330
onPageLimitChange: NOOP,
319331
onPageChange: NOOP,
320-
onMasterCheckboxChange: NOOP
332+
onMasterToggle: NOOP
321333
}
322334

323335
Table.propTypes = {
@@ -357,14 +369,16 @@ Table.propTypes = {
357369
pages: PropTypes.oneOfType([
358370
PropTypes.node, PropTypes.func
359371
]),
360-
actions: PropTypes.node,
372+
actions: PropTypes.func,
361373
//
362374
rowIsChecked: PropTypes.func,
363375
masterIsChecked: PropTypes.func,
364376
//
365377
onRowClick: PropTypes.func,
366-
onRowCheckboxChange: PropTypes.func,
378+
onRowToggle: PropTypes.func,
367379
onPageLimitChange: PropTypes.func,
368380
onPageChange: PropTypes.func,
369-
onMasterCheckboxChange: PropTypes.func
381+
onMasterToggle: PropTypes.func
370382
}
383+
384+
export default Table

0 commit comments

Comments
 (0)