mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2025-10-16 11:55:39 +00:00
parent
7c424a2373
commit
1e32ea245f
3
packages/react-bootstrap-table2/src/body.js
vendored
3
packages/react-bootstrap-table2/src/body.js
vendored
@ -1,6 +1,7 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
import _ from './utils';
|
||||
import Row from './row';
|
||||
|
||||
const Body = ({ columns, data, keyField }) => (
|
||||
@ -8,7 +9,7 @@ const Body = ({ columns, data, keyField }) => (
|
||||
{
|
||||
data.map((row, index) => (
|
||||
<Row
|
||||
key={ row[keyField] }
|
||||
key={ _.get(row, keyField) }
|
||||
row={ row }
|
||||
rowIndex={ index }
|
||||
columns={ columns }
|
||||
|
||||
3
packages/react-bootstrap-table2/src/cell.js
vendored
3
packages/react-bootstrap-table2/src/cell.js
vendored
@ -1,9 +1,10 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
import _ from './utils';
|
||||
|
||||
const Cell = ({ row, rowIndex, column }) => {
|
||||
let content = row[column.dataField];
|
||||
let content = _.get(row, column.dataField);
|
||||
if (column.formatter) {
|
||||
content = column.formatter(content, row, rowIndex, column.formatExtraData);
|
||||
}
|
||||
|
||||
3
packages/react-bootstrap-table2/src/row.js
vendored
3
packages/react-bootstrap-table2/src/row.js
vendored
@ -1,6 +1,7 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
import _ from './utils';
|
||||
import Cell from './cell';
|
||||
|
||||
const Row = ({ row, rowIndex, columns }) => (
|
||||
@ -9,7 +10,7 @@ const Row = ({ row, rowIndex, columns }) => (
|
||||
columns.map(column =>
|
||||
(
|
||||
<Cell
|
||||
key={ row[column.dataField] }
|
||||
key={ _.get(row, column.dataField) }
|
||||
row={ row }
|
||||
rowIndex={ rowIndex }
|
||||
column={ column }
|
||||
|
||||
18
packages/react-bootstrap-table2/src/utils.js
vendored
Normal file
18
packages/react-bootstrap-table2/src/utils.js
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
/* eslint no-empty: 0 */
|
||||
|
||||
function get(target, field) {
|
||||
const pathArray = [field]
|
||||
.join('.')
|
||||
.replace(/\[/g, '.')
|
||||
.replace(/\]/g, '')
|
||||
.split('.');
|
||||
let result;
|
||||
try {
|
||||
result = pathArray.reduce((curr, path) => curr[path], target);
|
||||
} catch (e) {}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default {
|
||||
get
|
||||
};
|
||||
24
packages/react-bootstrap-table2/test/utils.test.js
Normal file
24
packages/react-bootstrap-table2/test/utils.test.js
Normal file
@ -0,0 +1,24 @@
|
||||
import _ from '../src/utils';
|
||||
|
||||
describe('Utils', () => {
|
||||
describe('get', () => {
|
||||
const data = {
|
||||
name: 'A',
|
||||
address: {
|
||||
road: 'BCD',
|
||||
postal: '1234-12345',
|
||||
city: {
|
||||
name: 'B'
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
it('should return correct data', () => {
|
||||
expect(_.get(data, 'name')).toEqual(data.name);
|
||||
expect(_.get(data, 'address.road')).toEqual(data.address.road);
|
||||
expect(_.get(data, 'address.city.name')).toEqual(data.address.city.name);
|
||||
expect(_.get(data, 'address.notExist')).toEqual(undefined);
|
||||
expect(_.get(data, 'address.not.exist')).toEqual(undefined);
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user