Next.js template
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PaginationComponentRQ.jsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {
  2. Box,
  3. Button,
  4. Divider,
  5. FormControl,
  6. Grid,
  7. InputLabel,
  8. MenuItem,
  9. Paper,
  10. Select,
  11. TextField,
  12. Typography,
  13. } from '@mui/material';
  14. import { useTranslation } from 'next-i18next';
  15. import { useState } from 'react';
  16. import useDebounce from '../../../hooks/use-debounce';
  17. import { usePagination } from '../../../hooks/use-pagination';
  18. import { compare } from '../../../utils/helpers/sortHelpers';
  19. const PaginationComponentRQ = () => {
  20. const [pageIndex, setPageIndex] = useState(1);
  21. const [filter, setFilter] = useState('');
  22. const [sort, setSort] = useState('');
  23. const { t } = useTranslation('pagination');
  24. const { data: paginationData } = usePagination(pageIndex);
  25. const debouncedFilter = useDebounce(filter, 500);
  26. const handleFilterTextChange = (event) => {
  27. const filterText = event.target.value;
  28. setFilter(filterText);
  29. };
  30. const handleSortChange = (event) => {
  31. const sort = event.target.value;
  32. setSort(sort);
  33. };
  34. const dataToDisplay = paginationData?.data
  35. .filter((item) =>
  36. item.name.toLowerCase().startsWith(debouncedFilter.toLowerCase())
  37. )
  38. .sort((a, b) => compare(a.name, b.name, sort))
  39. .map((item, index) => (
  40. // ! DON'T USE index for key, this is for example only
  41. <Grid item sx={{ p: 2 }} xs={12} sm={6} md={4} lg={3} key={index}>
  42. {/* TODO separate into component */}
  43. <Paper sx={{ p: 3, height: '100%' }} elevation={3}>
  44. <Typography sx={{ fontWeight: 600 }}>{t('Name')}</Typography>
  45. <Typography display="inline"> {item.name}</Typography>
  46. <Divider />
  47. <Typography sx={{ fontWeight: 600 }}>{t('Age')}</Typography>
  48. <Typography display="inline"> {item.age}</Typography>
  49. <Divider />
  50. <Typography sx={{ fontWeight: 600 }}>{t('Gender')}</Typography>
  51. <Typography display="inline"> {item.gender}</Typography>
  52. <Divider />
  53. </Paper>
  54. </Grid>
  55. ));
  56. return (
  57. <Paper
  58. sx={{
  59. display: 'flex',
  60. flexDirection: 'column',
  61. justifyContent: 'start',
  62. py: 2,
  63. minHeight: 400,
  64. marginTop: 5,
  65. }}
  66. elevation={5}
  67. >
  68. <Typography sx={{ my: 4 }} variant="h4" gutterBottom align="center">
  69. {t('Title')}
  70. </Typography>
  71. <Box
  72. sx={{
  73. display: 'flex',
  74. justifyContent: 'space-between',
  75. flexWrap: 'wrap',
  76. mx: 2,
  77. }}
  78. >
  79. <Box
  80. sx={{
  81. display: 'flex',
  82. justifyContent: 'space-between',
  83. width: '100%',
  84. }}
  85. >
  86. <FormControl sx={{ flexGrow: 1 }}>
  87. <InputLabel id="sort-label">Sort</InputLabel>
  88. <Select
  89. label="Sort"
  90. labelId="sort-label"
  91. id="sort-select-helper"
  92. value={sort}
  93. onChange={handleSortChange}
  94. >
  95. <MenuItem value="asc">Name - A-Z</MenuItem>
  96. <MenuItem value="desc">Name - Z-A</MenuItem>
  97. </Select>
  98. </FormControl>
  99. <TextField
  100. sx={{ flexGrow: 1 }}
  101. variant="outlined"
  102. label="Filter"
  103. placeholder="Filter"
  104. value={filter}
  105. onChange={handleFilterTextChange}
  106. />
  107. </Box>
  108. </Box>
  109. <Grid container>{dataToDisplay}</Grid>
  110. <Box
  111. sx={{
  112. width: '100%',
  113. textAlign: 'center',
  114. marginTop: 3,
  115. }}
  116. >
  117. <Button
  118. disabled={pageIndex === 1}
  119. onClick={() => setPageIndex(pageIndex - 1)}
  120. sx={{
  121. marginRight: 5,
  122. }}
  123. >
  124. {t('Btns.PrevBtn')}
  125. </Button>
  126. <Button
  127. disabled={pageIndex * 4 > paginationData?.dataCount}
  128. onClick={() => setPageIndex(pageIndex + 1)}
  129. sx={{
  130. marginRight: 5,
  131. }}
  132. >
  133. {t('Btns.NextBtn')}
  134. </Button>
  135. </Box>
  136. </Paper>
  137. );
  138. };
  139. export default PaginationComponentRQ;