Grid.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { FC, MouseEvent } from 'react';
  2. import React from 'react';
  3. import { makeStyles } from '@material-ui/core/styles';
  4. import Grid from '@material-ui/core/Grid';
  5. import Breadcrumbs from '@material-ui/core/Breadcrumbs';
  6. import TablePagination from '@material-ui/core/TablePagination';
  7. import Typography from '@material-ui/core/Typography';
  8. import CustomToolbar from './ToolBar';
  9. import Table from './Table';
  10. import { MilvusGridType } from './Types';
  11. import { useTranslation } from 'react-i18next';
  12. import TablePaginationActions from './TablePaginationActions';
  13. const userStyle = makeStyles(theme => ({
  14. loading: {
  15. height: '100%',
  16. display: 'flex',
  17. alignItems: 'center',
  18. justifyContent: 'center',
  19. padding: theme.spacing(20),
  20. width: '100%',
  21. },
  22. titleIcon: {
  23. verticalAlign: '-3px',
  24. '& svg': {
  25. fill: '#32363c',
  26. },
  27. },
  28. tableTitle: {
  29. '& .last': {
  30. color: 'rgba(0, 0, 0, 0.54)',
  31. },
  32. },
  33. noData: {
  34. pointerEvents: 'none',
  35. color: '#999',
  36. textAlign: 'center',
  37. height: '50vh',
  38. display: 'grid',
  39. justifyContent: 'center',
  40. alignContent: 'center',
  41. fontSize: '32px',
  42. },
  43. pagenation: {
  44. '& .MuiTablePagination-caption': {
  45. position: 'absolute',
  46. left: 0,
  47. bottom: 0,
  48. top: 0,
  49. display: 'flex',
  50. alignItems: 'center',
  51. '& .rows': {
  52. color: 'rgba(0,0,0,0.33)',
  53. marginLeft: theme.spacing(1),
  54. },
  55. },
  56. },
  57. noBottomPadding: {
  58. paddingBottom: '0 !important',
  59. display: 'flex',
  60. flexDirection: 'column',
  61. },
  62. wrapper: {
  63. height: '100%',
  64. },
  65. container: {
  66. flexWrap: 'nowrap',
  67. flexDirection: 'column',
  68. },
  69. }));
  70. const MilvusGrid: FC<MilvusGridType> = props => {
  71. const classes = userStyle();
  72. // i18n
  73. const { t: commonTrans } = useTranslation();
  74. const gridTrans = commonTrans('grid');
  75. const {
  76. rowCount = 10,
  77. rowsPerPage = 5,
  78. primaryKey = 'id',
  79. showToolbar = false,
  80. toolbarConfigs = [],
  81. onChangePage = (
  82. e: MouseEvent<HTMLButtonElement> | null,
  83. nextPageNum: number
  84. ) => {
  85. console.log('nextPageNum', nextPageNum);
  86. },
  87. labelDisplayedRows,
  88. // pageUnit = 'item',
  89. page = 0,
  90. rows = [],
  91. colDefinitions = [],
  92. isLoading = false,
  93. title,
  94. // titleIcon = <CollectionIcon />,
  95. searchForm,
  96. openCheckBox = true,
  97. disableSelect = false,
  98. noData = gridTrans.noData,
  99. showHoverStyle = true,
  100. headEditable = false,
  101. editHeads = [],
  102. selected = [],
  103. setSelected = () => {},
  104. setRowsPerPage = () => {},
  105. tableCellMaxWidth,
  106. } = props;
  107. const _isSelected = (row: { [x: string]: any }) => {
  108. // console.log("row selected test", row[primaryKey]);
  109. return selected.some((s: any) => s[primaryKey] === row[primaryKey]);
  110. };
  111. const _onSelected = (event: React.MouseEvent, row: { [x: string]: any }) => {
  112. let newSelected: any[] = ([] as any[]).concat(selected);
  113. if (_isSelected(row)) {
  114. newSelected = newSelected.filter(s => s[primaryKey] !== row[primaryKey]);
  115. } else {
  116. newSelected.push(row);
  117. }
  118. setSelected(newSelected);
  119. };
  120. const _onSelectedAll = (event: React.ChangeEvent) => {
  121. if ((event.target as HTMLInputElement).checked) {
  122. const newSelecteds = rows;
  123. setSelected(newSelecteds);
  124. return;
  125. }
  126. setSelected([]);
  127. };
  128. // const defaultLabelRows = ({ from = 0, to = 0, count = 0 }) => {
  129. // const plural = pageUnit.charAt(pageUnit.length - 1) === 'y' ? 'ies' : 's';
  130. // const formatUnit =
  131. // pageUnit.charAt(pageUnit.length - 1) === 'y'
  132. // ? pageUnit.slice(0, pageUnit.length - 1)
  133. // : pageUnit;
  134. // const unit = count > 1 ? `${formatUnit}${plural}` : pageUnit;
  135. // return `${count} ${unit}`;
  136. // };
  137. const defaultLabelRows = ({ from = 0, to = 0, count = 0 }) => {
  138. return (
  139. <>
  140. <Typography variant="body2" component="span">
  141. {from} - {to}
  142. </Typography>
  143. <Typography variant="body2" className="rows" component="span">
  144. {gridTrans.of} {count} {gridTrans.rows}
  145. </Typography>
  146. </>
  147. );
  148. };
  149. return (
  150. <Grid
  151. container
  152. classes={{ root: classes.wrapper, container: classes.container }}
  153. spacing={3}
  154. >
  155. {title && (
  156. <Grid item xs={12} className={classes.tableTitle}>
  157. <Breadcrumbs separator="›" aria-label="breadcrumb">
  158. {title.map(
  159. (v: any, i: number) =>
  160. v && (
  161. <Typography
  162. key={v}
  163. className={i === title.length - 1 ? 'last' : ''}
  164. variant="h6"
  165. color="textPrimary"
  166. >
  167. {v}
  168. </Typography>
  169. )
  170. )}
  171. </Breadcrumbs>
  172. </Grid>
  173. )}
  174. {searchForm && (
  175. <Grid item xs={12}>
  176. {searchForm}
  177. </Grid>
  178. )}
  179. {(showToolbar || toolbarConfigs.length > 0) && (
  180. <Grid item>
  181. <CustomToolbar
  182. toolbarConfigs={toolbarConfigs}
  183. selected={selected}
  184. ></CustomToolbar>
  185. </Grid>
  186. )}
  187. <Grid item xs={12} className={classes.noBottomPadding}>
  188. <Table
  189. openCheckBox={openCheckBox}
  190. primaryKey={primaryKey}
  191. rows={rows}
  192. selected={selected}
  193. colDefinitions={colDefinitions}
  194. onSelectedAll={_onSelectedAll}
  195. onSelected={_onSelected}
  196. isSelected={_isSelected}
  197. disableSelect={disableSelect}
  198. noData={noData}
  199. showHoverStyle={showHoverStyle}
  200. isLoading={isLoading}
  201. setPageSize={setRowsPerPage}
  202. headEditable={headEditable}
  203. editHeads={editHeads}
  204. tableCellMaxWidth={tableCellMaxWidth}
  205. ></Table>
  206. {rowCount ? (
  207. <TablePagination
  208. component="div"
  209. colSpan={3}
  210. count={rowCount}
  211. page={page}
  212. labelDisplayedRows={labelDisplayedRows || defaultLabelRows}
  213. rowsPerPage={rowsPerPage}
  214. rowsPerPageOptions={[]}
  215. onChangePage={onChangePage}
  216. className={classes.pagenation}
  217. ActionsComponent={TablePaginationActions}
  218. />
  219. ) : null}
  220. </Grid>
  221. </Grid>
  222. );
  223. };
  224. export default MilvusGrid;