TableHead.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. textTransform: 'capitalize',
  32. color: 'rgba(0, 0, 0, 0.6)',
  33. fontSize: '12.8px',
  34. },
  35. tableRow: {
  36. // borderBottom: '1px solid rgba(0, 0, 0, 0.6);',
  37. },
  38. }));
  39. const EnhancedTableHead: FC<TableHeadType> = props => {
  40. const {
  41. onSelectAllClick,
  42. order,
  43. orderBy,
  44. numSelected,
  45. rowCount,
  46. colDefinitions = [],
  47. onRequestSort,
  48. openCheckBox,
  49. } = props;
  50. const classes = useStyles();
  51. const createSortHandler = (property: string) => (event: React.MouseEvent) => {
  52. onRequestSort(event, property);
  53. };
  54. return (
  55. <TableHead>
  56. <TableRow className={classes.tableRow}>
  57. {openCheckBox && (
  58. <TableCell padding="checkbox">
  59. <Checkbox
  60. color="primary"
  61. indeterminate={numSelected > 0 && numSelected < rowCount}
  62. checked={rowCount > 0 && numSelected === rowCount}
  63. onChange={onSelectAllClick}
  64. inputProps={{ 'aria-label': 'select all desserts' }}
  65. />
  66. </TableCell>
  67. )}
  68. {colDefinitions.map(headCell => (
  69. <TableCell
  70. key={headCell.id}
  71. align={headCell.align || 'left'}
  72. padding={headCell.disablePadding ? 'none' : 'default'}
  73. sortDirection={orderBy === headCell.id ? order : false}
  74. className={classes.tableCell}
  75. >
  76. {headCell.label ? (
  77. <TableSortLabel
  78. active={orderBy === headCell.id}
  79. direction={orderBy === headCell.id ? order : 'asc'}
  80. onClick={createSortHandler(headCell.id)}
  81. >
  82. <Typography variant="body1" className={classes.tableHeader}>
  83. {headCell.label}
  84. </Typography>
  85. {orderBy === headCell.id ? (
  86. <Typography className={classes.visuallyHidden}>
  87. {order === 'desc'
  88. ? 'sorted descending'
  89. : 'sorted ascending'}
  90. </Typography>
  91. ) : null}
  92. </TableSortLabel>
  93. ) : (
  94. <Typography variant="body1" className={classes.tableHeader}>
  95. {headCell.label}
  96. </Typography>
  97. )}
  98. </TableCell>
  99. ))}
  100. </TableRow>
  101. </TableHead>
  102. );
  103. };
  104. export default EnhancedTableHead;