import { useNavigation, useRouterContext } from '@pankod/refine-core'
import {
List,
Edit,
Create,
TableContainer,
Table,
Thead,
Tbody,
Tr,
Th,
Td,
Text,
EditButton,
FormControl,
FormErrorMessage,
FormLabel,
Input,
} from '@pankod/refine-chakra-ui'
import { useTable, ColumnDef, flexRender } from '@pankod/refine-react-table'
import { useForm } from '@pankod/refine-react-hook-form'
const PostList: React.FC = () => {
const columns = React.useMemo<ColumnDef<IPost>[]>(
() => [
{
id: 'id',
header: 'ID',
accessorKey: 'id',
},
{
id: 'title',
header: 'Title',
accessorKey: 'title',
},
{
id: 'actions',
header: 'Actions',
accessorKey: 'id',
cell: function render({ getValue }) {
return <EditButton hideText recordItemId={getValue() as number} />
},
},
],
[],
)
const { getHeaderGroups, getRowModel } = useTable({
columns,
refineCoreProps: {
initialPageSize: 5,
},
})
return (
<List>
<TableContainer whiteSpace="pre-line">
<Table variant="simple">
<Thead>
{getHeaderGroups().map((headerGroup) => (
<Tr key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<Th key={header.id}>
{!header.isPlaceholder && (
<Text>
{flexRender(
header.column.columnDef.header,
header.getContext(),
)}
</Text>
)}
</Th>
))}
</Tr>
))}
</Thead>
<Tbody>
{getRowModel().rows.map((row) => (
<Tr key={row.id}>
{row.getVisibleCells().map((cell) => (
<Td key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</Td>
))}
</Tr>
))}
</Tbody>
</Table>
</TableContainer>
</List>
)
}
const PostEdit: React.FC = () => {
const {
refineCore: { formLoading },
saveButtonProps,
register,
formState: { errors },
} = useForm<IPost>()
return (
<Edit isLoading={formLoading} saveButtonProps={saveButtonProps}>
<FormControl mb="3" isInvalid={!!errors?.title}>
<FormLabel>Title</FormLabel>
<Input {...register('title', { required: 'Title is required' })} />
<FormErrorMessage>{`${errors.title?.message}`}</FormErrorMessage>
</FormControl>
</Edit>
)
}
const PostCreate = () => {
const {
refineCore: { formLoading },
saveButtonProps,
register,
formState: { errors },
} = useForm<IPost>()
return (
<Create isLoading={formLoading} saveButtonProps={saveButtonProps}>
<FormControl mb="3" isInvalid={!!errors?.title}>
<FormLabel>Title</FormLabel>
<Input {...register('title', { required: 'Title is required' })} />
<FormErrorMessage>{`${errors.title?.message}`}</FormErrorMessage>
</FormControl>
</Create>
)
}
interface IPost {
id: number
title: string
}