Skip to content
This repository was archived by the owner on Jan 15, 2022. It is now read-only.

Commit 1fc822d

Browse files
trshaferglittershark
authored andcommitted
prototype solution to custom components
Since react will not render child components until the parent component is rendered (exploratory hypotheis) We need to render the custom component first and then access the component. This uses a Component function of getData(). The getData function could/should be fast because after the render function all the data could be available via props or state I have not tested this with updating data from the custom component. Or if the custom component internally changes the data and they table needs to resort.
1 parent f9ddbe1 commit 1fc822d

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

src/reactable/table.jsx

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class Table extends React.Component {
1313
super(props);
1414

1515
this.state = {
16+
parsedCustomComponents: false,
1617
currentPage: 0,
1718
currentSort: {
1819
column: null,
@@ -53,7 +54,7 @@ export class Table extends React.Component {
5354
}
5455

5556
parseChildData(props) {
56-
let data = [], tfoot;
57+
let data = [], tfoot, customComponentsCount = 0;
5758

5859
// Transform any children back to a data array
5960
if (typeof(props.children) !== 'undefined') {
@@ -112,19 +113,26 @@ export class Table extends React.Component {
112113
__reactableMeta: true
113114
});
114115
break;
116+
default:
117+
// Don't know if there are other acceptable types
118+
// that should be dismissed
119+
// console.log("Table, got custom component", child.type)
120+
customComponentsCount++;
121+
break;
115122
}
116123
}.bind(this));
117124
}
118125

119-
return { data, tfoot };
126+
return { data, tfoot, customComponentsCount };
120127
}
121128

122129
initialize(props) {
123130
this.data = props.data || [];
124-
let { data, tfoot } = this.parseChildData(props);
131+
let { data, tfoot, customComponentsCount } = this.parseChildData(props);
125132

126133
this.data = this.data.concat(data);
127134
this.tfoot = tfoot;
135+
this.customComponentsCount = customComponentsCount;
128136

129137
this.initializeSorts(props);
130138
}
@@ -206,6 +214,26 @@ export class Table extends React.Component {
206214
this.sortByCurrentSort();
207215
}
208216

217+
componentDidMount() {
218+
for (var i = 0; i < this.customComponentsCount; i++) {
219+
let child = this.refs['child-'+i],
220+
childData = child.getData(),
221+
childDataToPush = {};
222+
for (var key in childData){
223+
childDataToPush[key] = {
224+
value: childData[key],
225+
__reactableMeta: true
226+
};
227+
}
228+
this.data.push({
229+
data: childDataToPush,
230+
props: filterPropsFrom(child.props),
231+
__reactableMeta: true
232+
});
233+
};
234+
this.setState({parsedCustomComponents: true});
235+
}
236+
209237
componentWillReceiveProps(nextProps) {
210238
this.initialize(nextProps);
211239
this.updateCurrentSort(nextProps.sortBy);
@@ -297,8 +325,20 @@ export class Table extends React.Component {
297325
this.setState({ currentSort: currentSort });
298326
this.sortByCurrentSort();
299327
}
300-
328+
renderUnparsedDataTable() {
329+
// http://www.mattzabriskie.com/blog/react-referencing-dynamic-children
330+
let index = 0;
331+
let children = React.Children.map(this.props.children, function (child) {
332+
return React.addons.cloneWithProps(child, {ref: 'child-' + (index++) });
333+
});
334+
335+
return <div>{children}</div>;
336+
}
301337
render() {
338+
if (!this.state.parsedCustomComponents && this.customComponentsCount > 0){
339+
return this.renderUnparsedDataTable();
340+
}
341+
302342
let children = [];
303343
let columns;
304344
let userColumnsSpecified = false;

tests/reactable_test.jsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,13 @@ describe('Reactable', function() {
426426
age: React.PropTypes.number,
427427
position: React.PropTypes.string
428428
},
429+
getData: function(){
430+
return {
431+
Name: this.props.name,
432+
Age: this.props.age,
433+
Position: this.props.position,
434+
}
435+
},
429436
render: function(){
430437
return (
431438
<Reactable.Tr>

0 commit comments

Comments
 (0)