96 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-03-18 14:42:45 +13:00
import React from 'react';
import SilverStripeComponent from 'silverstripe-component.js';
import GridFieldTable from './table';
import GridFieldHeader from './header';
import GridFieldHeaderCell from './header-cell';
import GridFieldRow from './row';
import GridFieldCell from './cell';
import GridFieldAction from './action';
2016-03-18 14:42:45 +13:00
class GridField extends SilverStripeComponent {
constructor(props) {
2016-03-18 14:42:45 +13:00
super(props);
this.deleteCampaign = this.deleteCampaign.bind(this);
this.editCampaign = this.editCampaign.bind(this);
2016-03-18 14:42:45 +13:00
// TODO: This will be an AJAX call and it's response stored in state.
this.mockData = {
campaigns: [
{
title: 'SilverStripe 4.0 release',
description: 'All the stuff related to the 4.0 announcement',
changes: 20
},
{
title: 'March release',
description: 'march release stuff',
changes: 2
},
{
title: 'About us',
description: 'The team',
changes: 1345
}
]
};
}
render() {
const columns = [
{
name: 'title'
},
{
name: 'changes',
width: 2
},
{
name: 'description',
width: 6
}
];
2016-03-18 14:42:45 +13:00
const actions = [
<GridFieldAction icon={'cog'} handleClick={this.editCampaign} />,
<GridFieldAction icon={'cancel'} handleClick={this.deleteCampaign} />
];
// Placeholder to align the headers correctly with the content
const actionPlaceholder = <span key={'actionPlaceholder'} style={{width: actions.length * 36 + 12}} />;
const headerCells = columns.map((column, i) => <GridFieldHeaderCell key={i} width={column.width}>{column.name}</GridFieldHeaderCell>);
const header = <GridFieldHeader>{headerCells.concat(actionPlaceholder)}</GridFieldHeader>;
2016-03-18 14:42:45 +13:00
const rows = this.mockData.campaigns.map((campaign, i) => {
var cells = columns.map((column, i) => {
return <GridFieldCell key={i} width={column.width}>{campaign[column.name]}</GridFieldCell>
2016-03-18 14:42:45 +13:00
});
var rowActions = actions.map((action, j) => {
return Object.assign({}, action, {
key: `action-${i}-${j}`
});
})
return <GridFieldRow key={i}>{cells.concat(rowActions)}</GridFieldRow>;
2016-03-18 14:42:45 +13:00
});
return (
<GridFieldTable header={header} rows={rows}></GridFieldTable>
);
}
deleteCampaign(event) {
// delete campaign
}
editCampaign(event) {
// edit campaign
}
2016-03-18 14:42:45 +13:00
}
export default GridField;