61 lines
1.4 KiB
JavaScript
Raw Normal View History

/**
*
* TableHeader
*
*/
import React from 'react';
import styles from './styles.scss';
2017-05-11 10:54:44 +02:00
class TableHeader extends React.Component {
2017-04-11 13:44:47 +02:00
changeSort(name) {
if (this.props.sort === name) {
this.props.changeSort(`-${name}`);
} else if (this.props.sort === `-${name}`) {
this.props.changeSort('id');
} else {
this.props.changeSort(name);
}
}
render() {
2017-04-11 13:44:47 +02:00
// Generate headers list
const headers = this.props.headers.map((header, i) => {
// Define sort icon
let icon;
if (this.props.sort === header.name) {
2017-05-11 10:54:44 +02:00
icon = <i className={`ion ion-arrow-up-b ${styles.icon}`} />;
2017-04-11 13:44:47 +02:00
} else if (this.props.sort === `-${header.name}`) {
2017-05-11 10:54:44 +02:00
icon = <i className={`ion ion-arrow-down-b ${styles.icon}`} />;
2017-04-11 13:44:47 +02:00
}
return (
2017-05-11 10:54:44 +02:00
<th // eslint-disable-line jsx-a11y/no-static-element-interactions
key={i}
onClick={() => this.changeSort(header.name)}
>
2017-04-11 13:44:47 +02:00
{header.label} {icon}
</th>
);
});
return (
<thead className={styles.tableHeader}>
2017-05-11 10:54:44 +02:00
<tr className={styles.tableHeader}>
{headers}
<th />
</tr>
</thead>
);
}
}
2017-05-11 10:54:44 +02:00
TableHeader.propTypes = {
changeSort: React.PropTypes.func.isRequired,
2017-05-11 11:20:01 +02:00
headers: React.PropTypes.array.isRequired,
2017-05-11 10:54:44 +02:00
sort: React.PropTypes.string.isRequired,
};
export default TableHeader;