From dc5524e1d737867a2548af673dff106353c5331d Mon Sep 17 00:00:00 2001 From: AllenFang Date: Sun, 20 Aug 2017 19:55:31 +0800 Subject: [PATCH 1/2] implement utils.get to access the nested data --- packages/react-bootstrap-table2/src/body.js | 3 ++- packages/react-bootstrap-table2/src/cell.js | 3 ++- packages/react-bootstrap-table2/src/row.js | 3 ++- packages/react-bootstrap-table2/src/utils.js | 18 ++++++++++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 packages/react-bootstrap-table2/src/utils.js diff --git a/packages/react-bootstrap-table2/src/body.js b/packages/react-bootstrap-table2/src/body.js index 9b69cb92f..33a8c18d7 100644 --- a/packages/react-bootstrap-table2/src/body.js +++ b/packages/react-bootstrap-table2/src/body.js @@ -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) => ( { - let content = row[column.dataField]; + let content = _.get(row, column.dataField); if (column.formatter) { content = column.formatter(content, row, rowIndex, column.formatExtraData); } diff --git a/packages/react-bootstrap-table2/src/row.js b/packages/react-bootstrap-table2/src/row.js index 94a0aba91..fdaad044d 100644 --- a/packages/react-bootstrap-table2/src/row.js +++ b/packages/react-bootstrap-table2/src/row.js @@ -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 => ( curr[path], target); + } catch (e) {} + return result; +} + +export default { + get +}; From b7ef8521d3c508c35cdbc256591ae91dcd4532cc Mon Sep 17 00:00:00 2001 From: AllenFang Date: Sun, 20 Aug 2017 19:55:57 +0800 Subject: [PATCH 2/2] add test specs got utils --- .../react-bootstrap-table2/test/utils.test.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 packages/react-bootstrap-table2/test/utils.test.js diff --git a/packages/react-bootstrap-table2/test/utils.test.js b/packages/react-bootstrap-table2/test/utils.test.js new file mode 100644 index 000000000..feb33e937 --- /dev/null +++ b/packages/react-bootstrap-table2/test/utils.test.js @@ -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); + }); + }); +});