NextJS 12 + TypeScript를 사용한 재료 테이블
26766 단어 webdevtypescriptnextjsreact
이들은 내가 사용하는 종속성입니다
{
"dependencies": {
//...
"@material-ui/core": "^4.12.4",
"@material-ui/icons": "^4.11.3",
"@mui/lab": "^5.0.0-alpha.91",
"@mui/material": "^5.9.1",
"@mui/styles": "^5.9.2",
"@mui/utils": "^5.9.1",
"material-table": "^1.69.3",
},
"devDependencies": {
// ...
"@types/react-beautiful-dnd": "^13.1.2",
}
}
예, 이것 때문에 Material Table 1.69.3을 사용하고 있습니다stackoverflow answer.
모든 의존성을 설치하자
npm i [email protected] @material-ui/core @material-ui/icons @mui/lab @mui/material @mui/styles @mui/utils @types/react-beautiful-dnd
이에 따라 issues , 추가해야 합니다
components/PatchedPagination.tsx
// https://github.com/mbrn/material-table/pull/2937#issuecomment-879017952
import MaterialTable, { MaterialTableProps } from 'material-table'
import { TablePagination, TablePaginationProps } from '@material-ui/core'
function PatchedPagination(props: TablePaginationProps) {
const {
ActionsComponent,
onChangePage,
onChangeRowsPerPage,
...tablePaginationProps
} = props
return (
<TablePagination
{...tablePaginationProps}
// @ts-expect-error onChangePage was renamed to onPageChange
onPageChange={onChangePage}
onRowsPerPageChange={onChangeRowsPerPage}
ActionsComponent={(subprops) => {
const { onPageChange, ...actionsComponentProps } = subprops
return (
// @ts-expect-error ActionsComponent is provided by material-table
<ActionsComponent
{...actionsComponentProps}
onChangePage={onPageChange}
/>
)
}}
/>
)
}
export default PatchedPagination
생성
pages/basic-table.tsx
import { forwardRef } from 'react'
import { resetServerContext } from 'react-beautiful-dnd'
import MaterialTable from 'material-table'
import PatchedPagination from '../components/PatchedPagination'
// icons
import AddBox from '@material-ui/icons/AddBox'
import ArrowDownward from '@material-ui/icons/ArrowDownward'
import Check from '@material-ui/icons/Check'
import ChevronLeft from '@material-ui/icons/ChevronLeft'
import ChevronRight from '@material-ui/icons/ChevronRight'
import Clear from '@material-ui/icons/Clear'
import DeleteOutline from '@material-ui/icons/DeleteOutline'
import Edit from '@material-ui/icons/Edit'
import FilterList from '@material-ui/icons/FilterList'
import FirstPage from '@material-ui/icons/FirstPage'
import LastPage from '@material-ui/icons/LastPage'
import Remove from '@material-ui/icons/Remove'
import SaveAlt from '@material-ui/icons/SaveAlt'
import Search from '@material-ui/icons/Search'
import ViewColumn from '@material-ui/icons/ViewColumn'
// types
import type { Icons } from 'material-table'
import type { GetServerSideProps } from 'next'
const tableIcons: Icons = {
Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />),
Check: forwardRef((props, ref) => <Check {...props} ref={ref} />),
Clear: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
Delete: forwardRef((props, ref) => <DeleteOutline {...props} ref={ref} />),
DetailPanel: forwardRef((props, ref) => (
<ChevronRight {...props} ref={ref} />
)),
Edit: forwardRef((props, ref) => <Edit {...props} ref={ref} />),
Export: forwardRef((props, ref) => <SaveAlt {...props} ref={ref} />),
Filter: forwardRef((props, ref) => <FilterList {...props} ref={ref} />),
FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
PreviousPage: forwardRef((props, ref) => (
<ChevronLeft {...props} ref={ref} />
)),
ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
SortArrow: forwardRef((props, ref) => <ArrowDownward {...props} ref={ref} />),
ThirdStateCheck: forwardRef((props, ref) => <Remove {...props} ref={ref} />),
ViewColumn: forwardRef((props, ref) => <ViewColumn {...props} ref={ref} />),
}
// more detail why using resetServerContext
// https://stackoverflow.com/a/64242579
export const getServerSideProps: GetServerSideProps = async () => {
resetServerContext()
return { props: {} }
}
function BasicTable() {
return (
<MaterialTable
icons={tableIcons}
title="Basic Filtering Preview"
columns={[
{ title: 'Name', field: 'name' },
{ title: 'Surname', field: 'surname' },
{ title: 'Birth Year', field: 'birthYear', type: 'numeric' },
{
title: 'Birth Place',
field: 'birthCity',
lookup: { 34: 'İstanbul', 63: 'Şanlıurfa' },
},
]}
data={[
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{
name: 'Zerya Betül',
surname: 'Baran',
birthYear: 2017,
birthCity: 34,
},
]}
options={{
filtering: true,
}}
components={{ Pagination: PatchedPagination }}
/>
)
}
export default BasicTable
완료!
Reference
이 문제에 관하여(NextJS 12 + TypeScript를 사용한 재료 테이블), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mhx/material-table-with-nextjs-12-typescript-1gmp텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)