Extract a common TableOfContents component
Some checks failed
Check usage of free licenses / build-static-assets (pull_request) Successful in 30s
Add copyright notice / copyright_notice (pull_request) Successful in 44s
Playwright Tests / test (pull_request) Failing after 3m33s

This commit is contained in:
Manuel Bustillo 2024-11-10 20:52:48 +01:00
parent f26e646a36
commit 1838889a0c
2 changed files with 45 additions and 38 deletions

View File

@ -5,6 +5,7 @@
import React, { useState } from "react" import React, { useState } from "react"
import { TableArrangement } from '@/app/lib/definitions'; import { TableArrangement } from '@/app/lib/definitions';
import { classNames } from "../components/button"; import { classNames } from "../components/button";
import TableOfContents from "../components/table-of-contents";
export default function ArrangementsTable ({onArrangementSelected}: {onArrangementSelected: (arrangementId: string) => void}) { export default function ArrangementsTable ({onArrangementSelected}: {onArrangementSelected: (arrangementId: string) => void}) {
const [arrangements, setArrangements] = useState<Array<TableArrangement>>([]); const [arrangements, setArrangements] = useState<Array<TableArrangement>>([]);
@ -32,43 +33,23 @@ export default function ArrangementsTable ({onArrangementSelected}: {onArrangeme
arrangements.length === 0 && loadArrangements(); arrangements.length === 0 && loadArrangements();
return( return(
<div className="w-full relative overflow-x-auto shadow-md sm:rounded-lg"> <TableOfContents
<table className="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400"> headers={['Name', 'Discomfort', 'Actions']}
<caption className="p-5 text-lg font-semibold text-left rtl:text-right text-gray-900 bg-white dark:text-white dark:bg-gray-800"> caption='Simulations'
Simulations elements={arrangements}
<p className="mt-1 text-sm font-normal text-gray-500 dark:text-gray-400"> rowRender={(arrangement) => (
There are {arrangements.length} simmulations in the list <tr key={arrangement.id} className="bg-white border-b odd:bg-white odd:dark:bg-gray-900 even:bg-gray-50 even:dark:bg-gray-800">
</p> <th scope="row" className="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
</caption> {arrangement.name}
<thead className="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400"> </th>
<tr> <td className="px-6 py-4">
<th scope="col" className="px-6 py-3"> {arrangement.discomfort}
Name </td>
</th> <td>
<th scope="col" className="px-6 py-3"> <button data-arrangement-id={arrangement.id} onClick={arrangementClicked} className={classNames('primary')}>Load</button>
Discomfort </td>
</th> </tr>
<th scope="col" className="px-6 py-3"> )}
Actions />
</th>
</tr>
</thead>
<tbody>
{arrangements.map((arrangement) => (
<tr key={arrangement.id} className="bg-white border-b odd:bg-white odd:dark:bg-gray-900 even:bg-gray-50 even:dark:bg-gray-800">
<th scope="row" className="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
{arrangement.name}
</th>
<td className="px-6 py-4">
{arrangement.discomfort}
</td>
<td>
<button data-arrangement-id={arrangement.id} onClick={arrangementClicked} className={classNames('primary')}>Load</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
); );
} }

View File

@ -0,0 +1,26 @@
export default function TableOfContents<Type>({ headers, caption, elements, rowRender }: { headers: string[], caption: string, elements: Type[], rowRender: (element: Type) => JSX.Element }) {
return (
<div className="w-full relative overflow-x-auto shadow-md sm:rounded-lg">
<table className="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
<caption className="p-5 text-lg font-semibold text-left rtl:text-right text-gray-900 bg-white dark:text-white dark:bg-gray-800">
{caption}
<p className="mt-1 text-sm font-normal text-gray-500 dark:text-gray-400">
There are {elements.length} elements in the list
</p>
</caption>
<thead className="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<tr>
{headers.map((header) => (
<th scope="col" className="px-6 py-3">
{header}
</th>
))}
</tr>
</thead>
<tbody>
{elements.map((element) => rowRender(element))}
</tbody>
</table>
</div>
)
}