85 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-07-09 16:56:40 +02:00
import React, { memo } from 'react';
import { withRouter } from 'react-router';
import PropTypes from 'prop-types';
2019-07-09 18:11:09 +02:00
import { useListView } from '../../contexts/ListView';
2019-07-09 16:56:40 +02:00
import CustomInputCheckbox from '../CustomInputCheckbox';
import { Icon, Thead } from './styledComponents';
2019-07-09 18:11:09 +02:00
function TableHeader({ headers, isBulkable }) {
const {
firstSortableElement,
onChangeParams,
searchParams: { _sort },
} = useListView();
const [sortBy, sortOrder] = _sort.split(':');
2019-07-09 16:56:40 +02:00
return (
<Thead isBulkable={isBulkable}>
<tr>
{isBulkable && (
<th>
<CustomInputCheckbox
entriesToDelete={[]}
isAll
name="all"
onChange={() => {}}
value
/>
</th>
)}
{headers.map(header => {
//
return (
<th
key={header.name}
onClick={() => {
if (header.sortable) {
2019-07-09 18:11:09 +02:00
const isCurrentSort = header.name === sortBy;
const nextOrder =
isCurrentSort && sortOrder === 'ASC' ? 'DESC' : 'ASC';
2019-07-09 16:56:40 +02:00
let value = `${header.name}:${nextOrder}`;
2019-07-09 18:11:09 +02:00
if (isCurrentSort && sortOrder === 'DESC') {
value = `${firstSortableElement}:ASC`;
2019-07-09 16:56:40 +02:00
}
onChangeParams({
target: {
name: '_sort',
value,
},
});
}
}}
>
<span>
{header.label}
{sortBy === header.name && (
<Icon
className="fa fa-sort-asc"
isAsc={sortOrder === 'ASC'}
/>
)}
</span>
</th>
);
})}
<th></th>
</tr>
</Thead>
);
}
TableHeader.defaultProps = {
isBulkable: true,
headers: [],
};
TableHeader.propTypes = {
headers: PropTypes.array,
isBulkable: PropTypes.bool,
};
export default withRouter(memo(TableHeader));