Skip to content

Commit 61efe55

Browse files
committed
Refactor to update the API
1 parent 9bcd78f commit 61efe55

File tree

2 files changed

+84
-45
lines changed

2 files changed

+84
-45
lines changed

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "react-json-table",
3+
"version": "0.0.1",
4+
"description": "A simple but reactive table react component to display JSON data.",
5+
"main": "rjt.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/arqex/react-json-table.git"
12+
},
13+
"keywords": [
14+
"react",
15+
"table",
16+
"json"
17+
],
18+
"author": "Javier Marquez",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/arqex/react-json-table/issues"
22+
},
23+
"homepage": "https://github.com/arqex/react-json-table"
24+
}

rjt.js

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,34 @@ var List = React.createClass({
1717
},
1818

1919
render: function(){
20-
var contents = [this.renderItems()];
20+
var cols = this.normalizeColumns(),
21+
contents = [this.renderItems( cols )]
22+
;
2123

2224
if( this.getSetting('header') )
23-
contents.unshift( this.renderHeader() );
25+
contents.unshift( this.renderHeader( cols ) );
2426

2527
return $.table({ className: "jsonTable" }, contents );
2628
},
2729

28-
renderHeader: function(){
30+
renderHeader: function( cols ){
2931
var me = this,
30-
cells = this.props.fields.map( function(field){
31-
var name;
32-
if( typeof field == 'string' ){
33-
name = field;
34-
}
35-
else {
36-
name = field.name;
37-
}
38-
32+
cells = cols.map( function(col){
3933
return $.th(
40-
{ className: 'jsonColumn', key: name, onClick: me.onClickHeader, "data-name": name},
41-
name
34+
{ className: 'jsonColumn', key: col.key, onClick: me.onClickHeader, "data-key": col.key },
35+
col.label
4236
);
4337
})
4438
;
4539

4640
return $.thead({ key: 'th'},
47-
$.tr({ className: 'jsonHeader', key: 'h', "data-name": 'header' }, cells )
41+
$.tr({ className: 'jsonHeader', key: 'h', "data-key": 'header' }, cells )
4842
);
4943
},
5044

51-
renderItems: function(){
45+
renderItems: function( cols ){
5246
var me = this,
5347
items = this.props.items,
54-
fields = this.normalizeFields(),
5548
i = 1
5649
;
5750

@@ -62,8 +55,8 @@ var List = React.createClass({
6255
return React.createElement(Item, {
6356
key: me.getKey( item ),
6457
item: item,
65-
fields: fields,
66-
i: i,
58+
columns: cols,
59+
i: i++,
6760
onClickItem: me.onClickItem,
6861
onClickCell: me.onClickCell
6962
});
@@ -72,27 +65,53 @@ var List = React.createClass({
7265
return $.tbody({}, rows);
7366
},
7467

75-
normalizeFields: function(){
76-
var getItemField = function( item, field ){
77-
return item[ field ];
78-
};
68+
getItemField: function( item, field ){
69+
return item[ field ];
70+
},
7971

80-
return this.props.fields.map( function( field ){
81-
var name, content;
82-
if( typeof field == 'string' ){
72+
normalizeColumns: function(){
73+
var getItemField = this.getItemField,
74+
cols = this.props.columns,
75+
items = this.props.items
76+
;
77+
78+
if( !cols ){
79+
if( !items || !items.length )
80+
return [];
81+
82+
return Object.keys( items[0] ).map( function( key ){
83+
return { key: key, label: key, cell: getItemField };
84+
});
85+
}
86+
87+
return cols.map( function( col ){
88+
var key;
89+
if( typeof col == 'string' ){
8390
return {
84-
name: field,
85-
content: getItemField
91+
key: col,
92+
label: col,
93+
cell: getItemField
8694
};
8795
}
8896

89-
if( typeof field == 'object' ){
90-
return field;
97+
if( typeof col == 'object' ){
98+
key = col.key || col.label;
99+
100+
// This is about get default column definition
101+
// we use label as key if not defined
102+
// we use key as label if not defined
103+
// we use getItemField as cell function if not defined
104+
return {
105+
key: key,
106+
label: col.label || key,
107+
cell: col.cell || getItemField
108+
};
91109
}
92110

93111
return {
112+
key: 'unknown',
94113
name:'unknown',
95-
content: 'Unknown'
114+
cell: 'Unknown'
96115
};
97116
});
98117
},
@@ -113,10 +132,6 @@ var List = React.createClass({
113132
return true;
114133
},
115134

116-
componentDidUpdate: function(){
117-
console.log('updated');
118-
},
119-
120135
onClickItem: function( e, item ){
121136
if( this.props.onClickItem ){
122137
this.props.onClickItem( e, item );
@@ -125,32 +140,32 @@ var List = React.createClass({
125140

126141
onClickHeader: function( e ){
127142
if( this.props.onClickHeader ){
128-
this.props.onClickHeader( e, e.target.dataset.name );
143+
this.props.onClickHeader( e, e.target.dataset.key );
129144
}
130145
},
131146

132-
onClickCell: function( e, field, item ){
147+
onClickCell: function( e, key, item ){
133148
if( this.props.onClickCell ){
134-
this.props.onClickCell( e, field, item );
149+
this.props.onClickCell( e, key, item );
135150
}
136151
}
137152
});
138153

139154
var Item = React.createClass({
140155
render: function() {
141156
var me = this,
142-
cells = this.props.fields.map( function( field ){
143-
var content = field.content,
144-
name = field.name
157+
cells = this.props.columns.map( function( col ){
158+
var content = col.cell,
159+
key = col.key
145160
;
146161

147162
if( typeof content == 'function' )
148-
content = content( me.props.item, name );
163+
content = content( me.props.item, key );
149164

150165
return $.td( {
151166
className: 'jsonCell',
152-
key: name,
153-
"data-name": name,
167+
key: key,
168+
"data-key": key,
154169
onClick: me.onClickCell
155170
}, content );
156171
})
@@ -169,7 +184,7 @@ var Item = React.createClass({
169184
},
170185

171186
onClickCell: function( e ){
172-
this.props.onClickCell( e, e.target.dataset.name, this.props.item );
187+
this.props.onClickCell( e, e.target.dataset.key, this.props.item );
173188
},
174189

175190
onClickItem: function( e ){

0 commit comments

Comments
 (0)