Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7c424a2

Browse files
Implement column formatter (#13)
fix #12
1 parent 6b7d1f9 commit 7c424a2

File tree

7 files changed

+78
-17
lines changed

7 files changed

+78
-17
lines changed

‎package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"jest-babel": "^1.0.1",
4242
"lerna": "^2.0.0",
4343
"react-test-renderer": "^15.6.1",
44+
"sinon": "^3.2.1",
4445
"webpack": "^3.5.4",
4546
"webpack-dev-server": "^2.7.1"
4647
},

‎packages/react-bootstrap-table2/src/body.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ import Row from './row';
66
const Body = ({ columns, data, keyField }) => (
77
<tbody>
88
{
9-
data.map(row => (
10-
<Row key={ row[keyField] } row={ row } columns={ columns } />
9+
data.map((row, index) => (
10+
<Row
11+
key={ row[keyField] }
12+
row={ row }
13+
rowIndex={ index }
14+
columns={ columns }
15+
/>
1116
))
1217
}
1318
</tbody>

‎packages/react-bootstrap-table2/src/cell.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33

44

5-
const Cell = ({ value }) => (
6-
<td>{ value }</td>
7-
);
5+
const Cell = ({ row, rowIndex, column }) => {
6+
let content = row[column.dataField];
7+
if (column.formatter) {
8+
content = column.formatter(content, row, rowIndex, column.formatExtraData);
9+
}
10+
return (
11+
<td>{ content }</td>
12+
);
13+
};
814

915
Cell.propTypes = {
10-
value: PropTypes.any.isRequired
16+
row: PropTypes.object.isRequired,
17+
rowIndex: PropTypes.number.isRequired,
18+
column: PropTypes.object.isRequired
1119
};
1220

1321
export default Cell;

‎packages/react-bootstrap-table2/src/row.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@ import PropTypes from 'prop-types';
33

44
import Cell from './cell';
55

6-
const Row = ({ row, columns }) => (
6+
const Row = ({ row, rowIndex,columns }) => (
77
<tr>
88
{
99
columns.map(column =>
10-
<Cell key={ row[column.dataField] } value={ row[column.dataField] } />
11-
)
10+
(
11+
<Cell
12+
key={ row[column.dataField] }
13+
row={ row }
14+
rowIndex={ rowIndex }
15+
column={ column }
16+
/>
17+
))
1218
}
1319
</tr>
1420
);
1521

1622
Row.propTypes = {
1723
row: PropTypes.object.isRequired,
24+
rowIndex: PropTypes.number.isRequired,
1825
columns: PropTypes.array.isRequired
1926
};
2027

‎packages/react-bootstrap-table2/test/bootstrap-table.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('BootstrapTable', () => {
4141
});
4242
});
4343

44-
describe('when hover is true', () => {
44+
describe('when hover props is true', () => {
4545
beforeEach(() => {
4646
wrapper = shallow(<BootstrapTable keyField="id" columns={ columns } data={ data } hover />);
4747
});
@@ -51,7 +51,7 @@ describe('BootstrapTable', () => {
5151
});
5252
});
5353

54-
describe('when striped is true', () => {
54+
describe('when striped props is true', () => {
5555
beforeEach(() => {
5656
wrapper = shallow(<BootstrapTable keyField="id" columns={ columns } data={ data } striped />);
5757
});
@@ -61,7 +61,7 @@ describe('BootstrapTable', () => {
6161
});
6262
});
6363

64-
describe('when condensed is true', () => {
64+
describe('when condensed props is true', () => {
6565
beforeEach(() => {
6666
wrapper = shallow(<BootstrapTable keyField="id" columns={ columns } data={ data } condensed />);
6767
});
@@ -71,7 +71,7 @@ describe('BootstrapTable', () => {
7171
});
7272
});
7373

74-
describe('when bordered is false', () => {
74+
describe('when bordered props is false', () => {
7575
beforeEach(() => {
7676
wrapper = shallow(<BootstrapTable keyField="id" columns={ columns } data={ data } bordered={ false } />);
7777
});
Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,60 @@
11
import React from 'react';
2+
import sinon from 'sinon';
23
import { shallow } from 'enzyme';
34

45
import Cell from '../src/cell';
56

67
describe('Cell', () => {
78
let wrapper;
8-
const value = 'test';
9+
const row = {
10+
id: 1,
11+
name: 'A'
12+
};
913

1014
describe('simplest cell', () => {
15+
const column = {
16+
dataField: 'id',
17+
text: 'ID'
18+
};
19+
1120
beforeEach(() => {
12-
wrapper = shallow(<Cell value={ value } />);
21+
wrapper = shallow(<Cell row={ row}rowIndex={1}column={column } />);
1322
});
1423

1524
it('should render successfully', () => {
1625
expect(wrapper.length).toBe(1);
17-
expect(wrapper.contains(<td>{ value }</td>)).toBe(true);
26+
expect(wrapper.contains(<td>{ row[column.dataField] }</td>)).toBe(true);
27+
});
28+
});
29+
30+
describe('when formatter prop is defined', () => {
31+
const rowIndex = 1;
32+
const column = {
33+
dataField: 'id',
34+
text: 'ID',
35+
formatExtraData: []
36+
};
37+
const formatterResult = (<h3>{ row[column.dataField] }</h3>);
38+
const formatter = sinon.stub()
39+
.withArgs(row[column.dataField], row, rowIndex, column.formatExtraData)
40+
.returns(formatterResult);
41+
column.formatter = formatter; // defined column formatter
42+
43+
beforeEach(() => {
44+
wrapper = shallow(<Cell row={ row } rowIndex={ rowIndex } column={ column } />);
45+
});
46+
47+
afterEach(() => { formatter.reset(); });
48+
49+
it('should render successfully', () => {
50+
expect(wrapper.length).toBe(1);
51+
expect(wrapper.contains(<td><h3>{ row[column.dataField] }</h3></td>)).toBe(true);
52+
});
53+
54+
it('should call custom formatter correctly', () => {
55+
expect(formatter.callCount).toBe(1);
56+
expect(formatter.calledWith(row[column.dataField],
57+
row, rowIndex, column.formatExtraData)).toBe(true);
1858
});
1959
});
2060
});

‎packages/react-bootstrap-table2/test/row.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Row', () => {
2121

2222
describe('simplest row', () => {
2323
beforeEach(() => {
24-
wrapper = shallow(<Row columns={ columns } row={ row } />);
24+
wrapper = shallow(<Row rowIndex={1}columns={ columns } row={ row } />);
2525
});
2626

2727
it('should render successfully', () => {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /