ToolBar.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { FC, useMemo } from 'react';
  2. import {
  3. Grid,
  4. Typography,
  5. makeStyles,
  6. Theme,
  7. createStyles,
  8. IconButton,
  9. } from '@material-ui/core';
  10. import CustomButton from '../customButton/CustomButton';
  11. import Icons from '../icons/Icons';
  12. import { ToolBarConfig, ToolBarType } from './Types';
  13. import SearchInput from '../textField/SearchInput';
  14. import TableSwitch from './TableSwitch';
  15. import { throwErrorForDev } from '../../utils/Common';
  16. import CustomIconButton from '../customButton/CustomIconButton';
  17. const useStyles = makeStyles((theme: Theme) =>
  18. createStyles({
  19. countLabel: {
  20. display: 'flex',
  21. alignItems: 'center',
  22. justifyContent: 'flex-end',
  23. color: theme.palette.common.black,
  24. opacity: 0.4,
  25. },
  26. btn: {
  27. // marginLeft: theme.spacing(1),
  28. marginRight: '12px',
  29. },
  30. gridEnd: {
  31. display: 'flex',
  32. justifyContent: 'flex-end',
  33. alignItems: 'center',
  34. },
  35. })
  36. );
  37. const CustomToolBar: FC<ToolBarType> = props => {
  38. const { toolbarConfigs, selected = [] } = props;
  39. const classes = useStyles();
  40. // remove hidden button
  41. const leftConfigs = useMemo(() => {
  42. return toolbarConfigs.filter(
  43. (c: ToolBarConfig) =>
  44. !c.hidden &&
  45. c.icon !== 'search' &&
  46. c.type !== 'switch' &&
  47. c.position !== 'right'
  48. );
  49. }, [toolbarConfigs]);
  50. const rightConfigs = useMemo(() => {
  51. return toolbarConfigs.filter(
  52. c => c.icon === 'search' || c.type === 'switch' || c.position === 'right'
  53. );
  54. }, [toolbarConfigs]);
  55. return (
  56. <>
  57. <Grid container spacing={2}>
  58. <Grid item xs={8}>
  59. {leftConfigs.map((c, i) => {
  60. const isSelect = c.type === 'select' || c.type === 'groupSelect';
  61. if (isSelect) {
  62. return c.component;
  63. }
  64. const Icon = c.icon ? Icons[c.icon!]() : '';
  65. const disabled = c.disabled ? c.disabled(selected) : false;
  66. const isIcon = c.type === 'iconBtn';
  67. const btn = (
  68. <CustomButton
  69. key={i}
  70. size="small"
  71. onClick={c.onClick}
  72. startIcon={Icon}
  73. color="primary"
  74. disabled={disabled}
  75. variant="contained"
  76. tooltip={c.tooltip}
  77. className={classes.btn}
  78. >
  79. <Typography variant="button">{c.label}</Typography>
  80. </CustomButton>
  81. );
  82. const iconBtn = (
  83. <CustomIconButton
  84. key={i}
  85. onClick={c.onClick}
  86. tooltip={c.tooltip}
  87. disabled={disabled}
  88. >
  89. {Icon}
  90. </CustomIconButton>
  91. );
  92. return isIcon ? iconBtn : btn;
  93. })}
  94. </Grid>
  95. {rightConfigs.length > 0 && (
  96. <Grid className={classes.gridEnd} item xs={4}>
  97. {rightConfigs.map((c, i) => {
  98. if (c.icon === 'search') {
  99. if (!c.onSearch) {
  100. return throwErrorForDev(
  101. `if icon is search onSearch event handler is required`
  102. );
  103. }
  104. return (
  105. <SearchInput
  106. onClear={c.onClear}
  107. onSearch={c.onSearch}
  108. searchText={c.searchText}
  109. key={i}
  110. />
  111. );
  112. }
  113. switch (c.type) {
  114. case 'switch':
  115. if (!c.onAppClick || !c.onListClick) {
  116. return throwErrorForDev(
  117. `if type is switch need onAppClick onListClick event handler`
  118. );
  119. }
  120. return (
  121. <TableSwitch
  122. onAppClick={c.onAppClick}
  123. onListClick={c.onListClick}
  124. key={i}
  125. />
  126. );
  127. case 'select':
  128. case 'groupSelect':
  129. if (!c.component) {
  130. return throwErrorForDev(`component prop is required`);
  131. }
  132. return c.component;
  133. default:
  134. const Icon = c.icon ? Icons[c.icon]() : '';
  135. return Icon ? (
  136. <IconButton onClick={c.onClick} key={i}>
  137. {Icon}
  138. </IconButton>
  139. ) : (
  140. <div key={i}>Need Icon</div>
  141. );
  142. }
  143. })}
  144. </Grid>
  145. )}
  146. </Grid>
  147. </>
  148. );
  149. };
  150. export default CustomToolBar;