From 48ea0df09d176305d0fc1b02fa7ae01a1fe58ede Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Wed, 15 Feb 2017 09:54:57 -0700 Subject: [PATCH] Normalize all JSX callbacks to support functions --- src/index.js | 46 ++++++++++++++++------------------------------ src/utils.js | 13 ++++++++++++- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/index.js b/src/index.js index 6f91a88..65646b0 100644 --- a/src/index.js +++ b/src/index.js @@ -453,12 +453,10 @@ export default React.createClass({ }} {...rest} > - {typeof column.header === 'function' ? ( - - ) : column.header} + {_.normalizeComponent(column.header, { + data: sortedData, + column: column + })} ) } @@ -537,12 +535,10 @@ export default React.createClass({ {column.pivotColumns.map((pivotColumn, i) => { return ( - {typeof pivotColumn.header === 'function' ? ( - - ) : pivotColumn.header} + {_.normalizeComponent(pivotColumn.header, { + data: sortedData, + column: column + })} {i < column.pivotColumns.length - 1 && ( )} @@ -589,12 +585,10 @@ export default React.createClass({ }} {...rest} > - {typeof column.header === 'function' ? ( - - ) : column.header} + {_.normalizeComponent(column.header, { + data: sortedData, + column: column + })} ) } @@ -627,7 +621,6 @@ export default React.createClass({ {...trProps.rest} > {allVisibleColumns.map((column, i2) => { - const Cell = column.render const show = typeof column.show === 'function' ? column.show() : column.show const width = _.getFirstDefined(column.width, column.minWidth) const maxWidth = _.getFirstDefined(column.width, column.maxWidth) @@ -745,17 +738,10 @@ export default React.createClass({ }} {...tdProps.rest} > - {typeof Cell === 'function' ? ( - Cell.prototype.isReactComponent ? ( - - ) : Cell({ - ...rowInfo, - value: rowInfo.rowValues[column.id] - }) - ) : rowInfo.rowValues[column.id]} + {_.normalizeComponent(column.render, { + ...rowInfo, + value: rowInfo.rowValues[column.id] + }, rowInfo.rowValues[column.id])} ) })} diff --git a/src/utils.js b/src/utils.js index 317634f..88eb801 100644 --- a/src/utils.js +++ b/src/utils.js @@ -17,7 +17,8 @@ export default { isArray, splitProps, compactObject, - isSortingDesc + isSortingDesc, + normalizeComponent } function get (obj, path, def) { @@ -189,3 +190,13 @@ function compactObject (obj) { function isSortingDesc (d) { return !!(d.sort === 'desc' || d.desc === true || d.asc === false) } + +function normalizeComponent (Comp, params, fallback = Comp) { + return typeof Comp === 'function' ? ( + Comp.prototype.isReactComponent ? ( + + ) : Comp(params) + ) : fallback +}