PrivilegeOptions.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {
  2. makeStyles,
  3. Theme,
  4. Typography,
  5. Checkbox,
  6. FormGroup,
  7. FormControlLabel,
  8. } from '@material-ui/core';
  9. import { FC } from 'react';
  10. import { Privilege, PrivilegeOptionsProps } from './Types';
  11. const useStyles = makeStyles((theme: Theme) => ({
  12. checkBox: {
  13. width: theme.spacing(24),
  14. },
  15. formGrp: {
  16. marginBottom: theme.spacing(2),
  17. },
  18. subTitle: {
  19. marginBottom: theme.spacing(0.5),
  20. },
  21. }));
  22. const PrivilegeOptions: FC<PrivilegeOptionsProps> = ({
  23. options,
  24. selection,
  25. onChange,
  26. title,
  27. roleName,
  28. object,
  29. objectName = '*',
  30. }) => {
  31. const classes = useStyles();
  32. return (
  33. <>
  34. <Typography variant="h6" component="h6" className={classes.subTitle}>
  35. {title}
  36. </Typography>
  37. <FormGroup row className={classes.formGrp}>
  38. {options.map((r: string) => (
  39. <FormControlLabel
  40. control={
  41. <Checkbox
  42. onChange={(
  43. e: React.ChangeEvent<HTMLInputElement>,
  44. checked: boolean
  45. ) => {
  46. let newSelection = [...selection];
  47. if (!checked) {
  48. newSelection = newSelection.filter(
  49. (n: Privilege) => n.privilegeName !== r
  50. );
  51. } else {
  52. newSelection.push({
  53. privilegeName: r,
  54. object: object,
  55. objectName: objectName,
  56. roleName: roleName,
  57. });
  58. }
  59. onChange(newSelection);
  60. }}
  61. />
  62. }
  63. key={r}
  64. label={r}
  65. value={r}
  66. checked={selection.filter((s: Privilege) => s.privilegeName === r).length > 0}
  67. className={classes.checkBox}
  68. />
  69. ))}
  70. </FormGroup>
  71. </>
  72. );
  73. };
  74. export default PrivilegeOptions;