TableHead.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { FC } from 'react';
  2. import React from 'react';
  3. import { TableHeadType } from './Types';
  4. import {
  5. TableHead,
  6. TableRow,
  7. TableCell,
  8. Checkbox,
  9. TableSortLabel,
  10. makeStyles,
  11. Typography,
  12. } from '@material-ui/core';
  13. const useStyles = makeStyles(theme => ({
  14. visuallyHidden: {
  15. border: 0,
  16. clip: 'rect(0 0 0 0)',
  17. height: 1,
  18. margin: -1,
  19. overflow: 'hidden',
  20. padding: 0,
  21. position: 'absolute',
  22. top: 20,
  23. width: 1,
  24. },
  25. tableCell: {
  26. // background: theme.palette.common.t,
  27. paddingLeft: theme.spacing(2),
  28. // borderBottom: 'none',
  29. },
  30. tableHeader: {
  31. color: 'rgba(0, 0, 0, 0.6)',
  32. fontSize: '12.8px',
  33. },
  34. tableRow: {
  35. // borderBottom: '1px solid rgba(0, 0, 0, 0.6);',
  36. },
  37. }));
  38. const EnhancedTableHead: FC<TableHeadType> = props => {
  39. const {
  40. onSelectAllClick,
  41. order,
  42. orderBy,
  43. numSelected,
  44. rowCount,
  45. colDefinitions = [],
  46. handleSort,
  47. openCheckBox,
  48. } = props;
  49. const classes = useStyles();
  50. const createSortHandler = (property: string) => (event: React.MouseEvent) => {
  51. handleSort && handleSort(event, property);
  52. };
  53. return (
  54. <TableHead>
  55. <TableRow className={classes.tableRow}>
  56. {openCheckBox && (
  57. <TableCell padding="checkbox" role="cell">
  58. <Checkbox
  59. color="primary"
  60. indeterminate={numSelected > 0 && numSelected < rowCount}
  61. checked={rowCount > 0 && numSelected === rowCount}
  62. onChange={onSelectAllClick}
  63. inputProps={{ 'aria-label': 'select all desserts', 'role': 'checkbox' }}
  64. />
  65. </TableCell>
  66. )}
  67. {colDefinitions.map(headCell => (
  68. <TableCell
  69. key={headCell.id}
  70. align={headCell.align || 'left'}
  71. padding={headCell.disablePadding ? 'none' : 'normal'}
  72. sortDirection={
  73. orderBy === (headCell.sortBy || headCell.id) ? order : false
  74. }
  75. className={classes.tableCell}
  76. role="cell"
  77. >
  78. {headCell.label && handleSort && !headCell.notSort ? (
  79. <TableSortLabel
  80. active={orderBy === (headCell.sortBy || headCell.id)}
  81. direction={
  82. orderBy === (headCell.sortBy || headCell.id) ? order : 'asc'
  83. }
  84. onClick={createSortHandler(headCell.sortBy || headCell.id)}
  85. >
  86. <Typography variant="body1" className={classes.tableHeader}>
  87. {headCell.label}
  88. </Typography>
  89. {orderBy === (headCell.sortBy || headCell.id) ? (
  90. <Typography className={classes.visuallyHidden}>
  91. {order === 'desc'
  92. ? 'sorted descending'
  93. : 'sorted ascending'}
  94. </Typography>
  95. ) : null}
  96. </TableSortLabel>
  97. ) : (
  98. <Typography variant="body1" className={classes.tableHeader}>
  99. {headCell.label}
  100. </Typography>
  101. )}
  102. </TableCell>
  103. ))}
  104. </TableRow>
  105. </TableHead>
  106. );
  107. };
  108. export default EnhancedTableHead;