PriviledgeOptions.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. className={classes.checkBox}
  67. />
  68. ))}
  69. </FormGroup>
  70. </>
  71. );
  72. };
  73. export default PrivilegeOptions;