index.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. const { t } = useTranslation();
  73. const gridTrans = t('grid') as any;
  74. const {
  75. rowCount = 10,
  76. rowsPerPage = 5,
  77. primaryKey = 'id',
  78. showToolbar = false,
  79. toolbarConfigs = [],
  80. onChangePage = (
  81. e: MouseEvent<HTMLButtonElement> | null,
  82. nextPageNum: number
  83. ) => {
  84. console.log('nextPageNum', nextPageNum);
  85. },
  86. labelDisplayedRows,
  87. // pageUnit = 'item',
  88. page = 0,
  89. rows = [],
  90. colDefinitions = [],
  91. isLoading = false,
  92. title,
  93. // titleIcon = <CollectionIcon />,
  94. searchForm,
  95. openCheckBox = true,
  96. disableSelect = false,
  97. noData = t('grid.noData'),
  98. showHoverStyle = true,
  99. selected = [],
  100. setSelected = () => {},
  101. } = props;
  102. const _isSelected = (row: { [x: string]: any }) => {
  103. // console.log("row selected test", row[primaryKey]);
  104. return selected.some((s: any) => s[primaryKey] === row[primaryKey]);
  105. };
  106. const _onSelected = (event: React.MouseEvent, row: { [x: string]: any }) => {
  107. let newSelected: any[] = ([] as any[]).concat(selected);
  108. if (_isSelected(row)) {
  109. newSelected = newSelected.filter(s => s[primaryKey] !== row[primaryKey]);
  110. } else {
  111. newSelected.push(row);
  112. }
  113. setSelected(newSelected);
  114. };
  115. const _onSelectedAll = (event: React.ChangeEvent) => {
  116. if ((event.target as HTMLInputElement).checked) {
  117. const newSelecteds = rows;
  118. setSelected(newSelecteds);
  119. return;
  120. }
  121. setSelected([]);
  122. };
  123. // const defaultLabelRows = ({ from = 0, to = 0, count = 0 }) => {
  124. // const plural = pageUnit.charAt(pageUnit.length - 1) === 'y' ? 'ies' : 's';
  125. // const formatUnit =
  126. // pageUnit.charAt(pageUnit.length - 1) === 'y'
  127. // ? pageUnit.slice(0, pageUnit.length - 1)
  128. // : pageUnit;
  129. // const unit = count > 1 ? `${formatUnit}${plural}` : pageUnit;
  130. // return `${count} ${unit}`;
  131. // };
  132. const defaultLabelRows = ({ from = 0, to = 0, count = 0 }) => {
  133. return (
  134. <>
  135. <Typography variant="body2" component="span">
  136. {from} - {to}
  137. </Typography>
  138. <Typography variant="body2" className="rows" component="span">
  139. {gridTrans.of} {count} {gridTrans.rows}
  140. </Typography>
  141. </>
  142. );
  143. };
  144. return (
  145. <Grid
  146. container
  147. classes={{ root: classes.wrapper, container: classes.container }}
  148. spacing={3}
  149. >
  150. {title && (
  151. <Grid item xs={12} className={classes.tableTitle}>
  152. <Breadcrumbs separator="›" aria-label="breadcrumb">
  153. {title.map(
  154. (v: any, i: number) =>
  155. v && (
  156. <Typography
  157. key={v}
  158. className={i === title.length - 1 ? 'last' : ''}
  159. variant="h6"
  160. color="textPrimary"
  161. >
  162. {v}
  163. </Typography>
  164. )
  165. )}
  166. </Breadcrumbs>
  167. </Grid>
  168. )}
  169. {searchForm && (
  170. <Grid item xs={12}>
  171. {searchForm}
  172. </Grid>
  173. )}
  174. {(showToolbar || toolbarConfigs.length > 0) && (
  175. <Grid item>
  176. <CustomToolbar
  177. toolbarConfigs={toolbarConfigs}
  178. selected={selected}
  179. ></CustomToolbar>
  180. </Grid>
  181. )}
  182. <Grid item xs={12} className={classes.noBottomPadding}>
  183. <Table
  184. openCheckBox={openCheckBox}
  185. primaryKey={primaryKey}
  186. rows={rows}
  187. selected={selected}
  188. colDefinitions={colDefinitions}
  189. onSelectedAll={_onSelectedAll}
  190. onSelected={_onSelected}
  191. isSelected={_isSelected}
  192. disableSelect={disableSelect}
  193. noData={noData}
  194. showHoverStyle={showHoverStyle}
  195. isLoading={isLoading}
  196. ></Table>
  197. {rowCount ? (
  198. <TablePagination
  199. component="div"
  200. colSpan={3}
  201. count={rowCount}
  202. page={page}
  203. labelDisplayedRows={labelDisplayedRows || defaultLabelRows}
  204. rowsPerPage={rowsPerPage}
  205. rowsPerPageOptions={[]}
  206. onChangePage={onChangePage}
  207. className={classes.pagenation}
  208. ActionsComponent={TablePaginationActions}
  209. />
  210. ) : null}
  211. </Grid>
  212. </Grid>
  213. );
  214. };
  215. export default MilvusGrid;