89 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-07-09 16:56:40 +02:00
import React, { memo } from 'react';
import PropTypes from 'prop-types';
2019-10-31 11:57:40 +01:00
import useListView from '../../hooks/useListView';
2019-07-09 16:56:40 +02:00
import CustomInputCheckbox from '../CustomInputCheckbox';
2019-11-21 16:49:06 +01:00
import { Arrow, Thead } from './styledComponents';
2019-07-09 16:56:40 +02:00
/* eslint-disable jsx-a11y/control-has-associated-label */
2019-07-09 18:11:09 +02:00
function TableHeader({ headers, isBulkable }) {
const {
2019-07-09 19:38:39 +02:00
data,
entriesToDelete,
2019-07-09 18:11:09 +02:00
firstSortableElement,
2019-07-09 19:38:39 +02:00
onChangeBulkSelectall,
2019-07-09 18:11:09 +02:00
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
2019-07-09 19:38:39 +02:00
entriesToDelete={entriesToDelete}
2019-07-09 16:56:40 +02:00
isAll
name="all"
2019-07-09 19:38:39 +02:00
onChange={onChangeBulkSelectall}
value={
data.length === entriesToDelete.length &&
entriesToDelete.length > 0
}
2019-07-09 16:56:40 +02:00
/>
</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,
},
});
}
}}
>
2019-09-13 16:04:15 +02:00
<span className={header.sortable ? 'sortable' : ''}>
2019-07-09 16:56:40 +02:00
{header.label}
2019-11-21 16:49:06 +01:00
2019-07-09 16:56:40 +02:00
{sortBy === header.name && (
2019-11-21 16:49:06 +01:00
<Arrow className={`${sortOrder === 'ASC' && 'isAsc'}`} />
2019-07-09 16:56:40 +02:00
)}
</span>
</th>
);
})}
<th />
2019-07-09 16:56:40 +02:00
</tr>
</Thead>
);
}
TableHeader.defaultProps = {
isBulkable: true,
headers: [],
};
TableHeader.propTypes = {
headers: PropTypes.array,
isBulkable: PropTypes.bool,
};
2019-07-09 19:38:39 +02:00
export default memo(TableHeader);