1
0
mirror of https://github.com/ascribe/onion.git synced 2025-01-03 10:25:08 +01:00

Add generic table

This commit is contained in:
Tim Daubenschütz 2015-05-20 14:48:57 +02:00
parent 0b4765aa5c
commit 923db02f78
7 changed files with 110 additions and 27 deletions

View File

@ -15,9 +15,16 @@ let PieceList = React.createClass({
}, },
render() { render() {
let columnMap = {
'thumbnail': 'Thumbnail',
'artist_name': 'Artist',
'title': 'Title'
};
return ( return (
<AltContainer store={PieceListStore}> <AltContainer store={PieceListStore}>
<Table class="piecesTable" /> <Table class="piecesTable" columnMap={columnMap} />
</AltContainer> </AltContainer>
); );
} }

View File

@ -1,18 +1,32 @@
import React from 'react'; import React from 'react';
import TableItem from './table_item'; import TableItem from './table_item';
import TableHeader from './table_header';
let Table = React.createClass({ let Table = React.createClass({
propTypes: {
columnMap: React.PropTypes.object.isRequired
},
render() { render() {
return (
<div className="row as-pieces-table"> if(this.props.itemList && this.props.itemList.length > 0) {
{this.props.pieceList.map((piece, i) => {
return ( return (
<TableItem piece={piece} key={i}/> <div>
); <TableHeader className="ascribe-table-header" columnMap={this.props.columnMap}/>
})} {this.props.itemList.map((item, i) => {
</div> return (
); <TableItem columnMap={this.props.columnMap} columnContent={item} key={i} />
);
})}
</div>
);
} else {
return (
<p>Loading</p>
);
}
} }
}); });

View File

@ -0,0 +1,34 @@
import React from 'react';
import TableColumnMixin from '../mixins/table_column_mixin';
import GeneralUtils from '../utils/general_utils';
let TableHeader = React.createClass({
mixins: [TableColumnMixin],
propTypes: {
columnMap: React.PropTypes.object.isRequired
},
render() {
let columnMapValuesList = GeneralUtils.valuesOfObject(this.props.columnMap);
let columnClasses = this.calcColumnClasses(this.props.columnMap);
return (
<div className="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div className="row">
{columnMapValuesList.map((val, i) => {
return (
<div className={columnClasses} key={i}>
{val}
</div>
);
})}
</div>
</div>
);
}
});
export default TableHeader;

View File

@ -1,26 +1,29 @@
import React from 'react'; import React from 'react';
import TableColumnMixin from '../mixins/table_column_mixin';
let TableItem = React.createClass({ let TableItem = React.createClass({
mixins: [TableColumnMixin],
propTypes: {
columnMap: React.PropTypes.object.isRequired,
columnContent: React.PropTypes.object.isRequired
},
render() { render() {
let piece = this.props.piece; let columnContent = this.props.columnContent;
let columnClasses = 'col-xs-3 col-sm-3 col-md-3 col-lg-3'; let columnClasses = this.calcColumnClasses(this.props.columnMap);
let columnMapKeysList = Object.keys(this.props.columnMap);
return ( return (
<div className="col-xs-12 col-sm-12 col-md-12 col-lg-12"> <div className="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div className="row"> <div className="row">
<div className={columnClasses}> {columnMapKeysList.map((key, i) => {
<img src={piece.thumbnail} height="50" /> return (
</div> <div className={columnClasses} key={i}>
<div className={columnClasses}> {columnContent[key]}
{piece.artist_name} </div>
</div> );
<div className={columnClasses}> })}
{piece.title}
</div>
<div className={columnClasses}>
{piece.availableActions}
</div>
</div> </div>
</div> </div>
); );

View File

@ -0,0 +1,16 @@
import GeneralUtils from '../utils/general_utils';
let TableColumnMixin = {
/**
* Generates the bootstrap grid column declarations automatically using
* the columnMap.
*/
calcColumnClasses(obj) {
let bootstrapClasses = ['col-xs-', 'col-sm-', 'col-md-', 'col-lg-'];
let numOfColumns = GeneralUtils.valuesOfObject(obj).length;
return bootstrapClasses.join( numOfColumns + ' ') + numOfColumns;
}
};
export default TableColumnMixin;

View File

@ -3,12 +3,12 @@ import PieceListActions from '../actions/piece_list_actions';
class PieceListStore { class PieceListStore {
constructor() { constructor() {
this.pieceList = []; this.itemList = []; // rename this to pieceList after figuring out AltContainer transform
this.bindActions(PieceListActions); this.bindActions(PieceListActions);
} }
onUpdatePieceList(pieceList) { onUpdatePieceList(itemList) {
this.pieceList = pieceList; this.itemList = itemList;
} }
}; };

View File

@ -16,6 +16,15 @@ let GeneralUtils = {
}); });
return obj; return obj;
},
/**
* Returns the values of an object.
*/
valuesOfObject(obj) {
return Object
.keys(obj)
.map(key => obj[key]);
} }
}; };