react-garden
Version:
React + TypeScript + ThreeJS app using Material UI on NextJS, Apollo Client, GraphQL + WordPress REST APIs, for ThreeD web development.. a part of the threed.ai code family.
160 lines (147 loc) • 4.48 kB
JavaScript
// ** React Imports
import { useState } from 'react'
// ** MUI Imports
import Box from '@mui/material/Box'
import Card from '@mui/material/Card'
import Button from '@mui/material/Button'
import Typography from '@mui/material/Typography'
import CardHeader from '@mui/material/CardHeader'
import { DataGrid } from '@mui/x-data-grid'
// ** Custom Components
import CustomChip from '~/@core/components/mui/chip'
import CustomAvatar from '~/@core/components/mui/avatar'
// ** Utils Import
import { getInitials } from '~/@core/utils/get-initials'
// ** Data Import
import { rows } from '~/@fake-db/table/static-data'
// ** renders client column
const renderClient = params => {
const { row } = params
const stateNum = Math.floor(Math.random() * 6)
const states = ['success', 'error', 'warning', 'info', 'primary', 'secondary']
const color = states[stateNum]
if (row.avatar.length) {
return <CustomAvatar src={`/images/avatars/${row.avatar}`} sx={{ mr: 3, width: '1.875rem', height: '1.875rem' }} />
} else {
return (
<CustomAvatar skin='light' color={color} sx={{ mr: 3, fontSize: '.8rem', width: '1.875rem', height: '1.875rem' }}>
{getInitials(row.full_name ? row.full_name : 'Marty McGee')}
</CustomAvatar>
)
}
}
const statusObj = {
1: { title: 'current', color: 'primary' },
2: { title: 'professional', color: 'success' },
3: { title: 'rejected', color: 'error' },
4: { title: 'resigned', color: 'warning' },
5: { title: 'applied', color: 'info' }
}
const TableSort = () => {
// ** States
const [pageSize, setPageSize] = useState(7)
const [isNameSortable, setIsNameSortable] = useState(true)
const columns = [
{
flex: 0.275,
minWidth: 290,
field: 'full_name',
headerName: 'Name',
sortable: isNameSortable,
renderCell: params => {
const { row } = params
return (
<Box sx={{ display: 'flex', alignItems: 'center' }}>
{renderClient(params)}
<Box sx={{ display: 'flex', flexDirection: 'column' }}>
<Typography noWrap variant='body2' sx={{ color: 'text.primary', fontWeight: 600 }}>
{row.full_name}
</Typography>
<Typography noWrap variant='caption'>
{row.email}
</Typography>
</Box>
</Box>
)
}
},
{
flex: 0.2,
minWidth: 120,
headerName: 'Date',
field: 'start_date',
sortable: isNameSortable,
renderCell: params => (
<Typography variant='body2' sx={{ color: 'text.primary' }}>
{params.row.start_date}
</Typography>
)
},
{
flex: 0.2,
minWidth: 110,
field: 'salary',
headerName: 'Salary',
sortable: isNameSortable,
renderCell: params => (
<Typography variant='body2' sx={{ color: 'text.primary' }}>
{params.row.salary}
</Typography>
)
},
{
flex: 0.125,
field: 'age',
minWidth: 80,
headerName: 'Age',
sortable: isNameSortable,
renderCell: params => (
<Typography variant='body2' sx={{ color: 'text.primary' }}>
{params.row.age}
</Typography>
)
},
{
flex: 0.2,
minWidth: 140,
field: 'status',
headerName: 'Status',
sortable: isNameSortable,
renderCell: params => {
const status = statusObj[params.row.status]
return (
<CustomChip
size='small'
skin='light'
color={status.color}
label={status.title}
sx={{ '& .MuiChip-label': { textTransform: 'capitalize' } }}
/>
)
}
}
]
return (
<Card>
<CardHeader
title='Sorting'
action={
<Box>
<Button size='small' variant='contained' onClick={() => setIsNameSortable(!isNameSortable)}>
{`Disable Sorting: ${!isNameSortable}`}
</Button>
</Box>
}
/>
<DataGrid
autoHeight
rows={rows}
columns={columns}
pageSize={pageSize}
rowsPerPageOptions={[7, 10, 25, 50]}
onPageSizeChange={newPageSize => setPageSize(newPageSize)}
/>
</Card>
)
}
export default TableSort