83 lines
1.8 KiB
JavaScript
Raw Normal View History

/**
*
* TableRow
*
*/
import React from 'react';
2017-04-11 18:02:55 +02:00
import _ from 'lodash';
2017-04-11 11:34:59 +02:00
import styles from './styles.scss';
2017-05-11 10:54:44 +02:00
class TableRow extends React.Component {
2017-04-11 15:38:15 +02:00
constructor(props) {
super(props);
2017-08-30 17:56:52 +02:00
this.handleClick = this.handleClick.bind(this);
2017-04-11 15:38:15 +02:00
}
2017-04-11 18:02:55 +02:00
/**
* Return a formatted value according to the
* data type and value stored in database
*
* @param type {String} Data type
* @param value {*} Value stored in database
* @returns {*}
*/
getDisplayedValue(type, value) {
switch (type) {
case 'string':
return !_.isEmpty(value.toString()) ? value.toString() : '-';
2017-04-11 18:02:55 +02:00
case 'integer':
return !_.isEmpty(value.toString()) ? value.toString() : '-';
2017-06-20 19:19:10 +02:00
case 'boolean':
return value.toString();
2017-04-11 18:02:55 +02:00
default:
return '-';
}
}
2017-08-30 17:56:52 +02:00
// Redirect to the edit page
handleClick() {
this.context.router.history.push(this.props.destination);
2017-05-11 10:54:44 +02:00
}
render() {
2017-04-11 18:02:55 +02:00
// Generate cells
2017-08-30 17:56:52 +02:00
const cells = this.props.headers.map((header, i) => (
<td key={i}>
{this.getDisplayedValue(
header.type,
this.props.record[header.name]
)}
</td>
));
2017-04-11 18:02:55 +02:00
2017-08-30 17:56:52 +02:00
// Add actions cell.
cells.push(
<td key='action' className={styles.actions}>
<i className="fa fa-pencil" aria-hidden="true"></i>
<i className="fa fa-trash" aria-hidden="true"></i>
</td>
);
return (
2017-08-30 17:56:52 +02:00
<tr className={styles.tableRow} onClick={() => this.handleClick(this.props.destination)}>
{cells}
</tr>
);
}
}
2017-04-11 15:38:15 +02:00
TableRow.contextTypes = {
2017-05-11 10:54:44 +02:00
router: React.PropTypes.object.isRequired,
2017-04-11 15:38:15 +02:00
};
TableRow.propTypes = {
2017-05-11 10:54:44 +02:00
destination: React.PropTypes.string.isRequired,
2017-05-11 11:20:01 +02:00
headers: React.PropTypes.array.isRequired,
record: React.PropTypes.object.isRequired,
};
export default TableRow;