add basic table to games page

This commit is contained in:
Alexander Navarro 2023-09-09 12:55:41 -03:00
parent 606d9a285b
commit 897148e47f
3 changed files with 93 additions and 10 deletions

View file

@ -60,3 +60,11 @@ ul {
li:not(:last-child) { li:not(:last-child) {
margin-bottom: var(--prj-spacing-1); margin-bottom: var(--prj-spacing-1);
} }
/* TODO: Move to where it belogns */
th,
td {
padding: 0.25rem 1rem;
border: 1px solid white;
text-align: center;
}

46
src/components/Table.tsx Normal file
View file

@ -0,0 +1,46 @@
import React from 'react';
type DataItem = Record<string, any>;
export interface Header {
key: string;
header: string;
formatter?: (data: DataItem) => JSX.ElementType;
}
interface Props {
data: DataItem[];
headers: Header[];
}
export default function Table({ data, headers }: Props): JSX.Element {
function getProperty(data: DataItem, header: Header): any {
if (header.key === 'index') {
return 'index';
}
return data[header.key];
}
return (
<table>
<thead>
<tr>
{headers.map((item, idx) => (
<th key={idx}>{item.header}</th>
))}
</tr>
</thead>
<tbody>
{data.map((item, idx) => (
<tr key={idx}>
{headers.map((header, hidx) => (
<td key={hidx}>{getProperty(item.data, header)}</td>
))}
</tr>
))}
</tbody>
</table>
);
}

View file

@ -1,13 +1,41 @@
--- ---
import Layout from '../../layouts/Layout.astro';
import { getCollection } from 'astro:content'; import { getCollection } from 'astro:content';
import Layout from '../../layouts/Layout.astro';
import Table, { Header } from '../../components/Table.jsx';
const games = await getCollection('games'); const games = await getCollection('games');
const headers: Header[] = [
{
key: 'index',
},
{
key: 'title',
header: 'Title',
},
{
key: 'status',
header: 'Status',
},
{
key: 'times_played',
header: 'Times Played',
},
{
key: 'registered_hours',
header: 'Registered Hours',
},
];
--- ---
<Layout title="aleidk"> <Layout title="aleidk">
<h1>Games</h1> <h1>Games</h1>
<section>
<Table data={games} headers={headers} />
</section>
<section>
<ul> <ul>
{ {
games.map((item) => ( games.map((item) => (
@ -17,4 +45,5 @@ const games = await getCollection('games');
)) ))
} }
</ul> </ul>
</section>
</Layout> </Layout>