-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from Vizzuality/table-design
[SKY30-88] Implement a static version of the table
- Loading branch information
Showing
11 changed files
with
2,017 additions
and
1,397 deletions.
There are no files selected for viewing
37 changes: 24 additions & 13 deletions
37
frontend/src/containers/data-tool/content/details/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 0 additions & 67 deletions
67
frontend/src/containers/data-tool/content/details/mocked-data.ts
This file was deleted.
Oops, something went wrong.
128 changes: 128 additions & 0 deletions
128
frontend/src/containers/data-tool/content/details/table/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { useLayoutEffect, useRef, useState } from 'react'; | ||
|
||
import { | ||
flexRender, | ||
getCoreRowModel, | ||
getSortedRowModel, | ||
useReactTable, | ||
} from '@tanstack/react-table'; | ||
|
||
import { cn } from '@/lib/classnames'; | ||
|
||
// ! todo: type columns,data properly | ||
const DataToolTable = ({ columns, data }) => { | ||
const tableRef = useRef<HTMLTableElement>(); | ||
const firstColumnRef = useRef<HTMLTableCellElement>(null); | ||
|
||
const [tableDimensions, setTableDimensions] = useState<{ | ||
firstColumnWidth: HTMLTableCellElement['offsetWidth']; | ||
tableWidth: HTMLTableElement['offsetWidth']; | ||
availableBarWidth: number; | ||
}>({ | ||
firstColumnWidth: 0, | ||
tableWidth: 0, | ||
availableBarWidth: 0, | ||
}); | ||
|
||
const table = useReactTable<typeof data>({ | ||
data, | ||
columns, | ||
getCoreRowModel: getCoreRowModel(), | ||
getSortedRowModel: getSortedRowModel(), | ||
}); | ||
|
||
const hasData = table.getRowModel().rows?.length; | ||
|
||
const firstColumn = columns[0]; | ||
const lastColumn = columns[columns.length - 1]; | ||
|
||
useLayoutEffect(() => { | ||
const tableWidth = tableRef.current?.offsetWidth; | ||
const firstColumnWidth = firstColumnRef.current?.offsetWidth; | ||
const availableBarWidth = tableWidth - firstColumnWidth; | ||
|
||
setTableDimensions({ | ||
firstColumnWidth, | ||
tableWidth, | ||
availableBarWidth, | ||
}); | ||
}, [data]); | ||
|
||
return ( | ||
<table ref={tableRef} className="whitespace-nowrap font-mono text-xs"> | ||
<thead className="text-left"> | ||
{table.getHeaderGroups().map((headerGroup) => ( | ||
<tr key={headerGroup.id}> | ||
{headerGroup.headers.map((header) => { | ||
const { id, column } = header; | ||
const isFirstColumn = id === firstColumn.accessorKey; | ||
const isLastColumn = id === lastColumn.accessorKey; | ||
|
||
return ( | ||
<th | ||
key={id} | ||
ref={isFirstColumn ? firstColumnRef : null} | ||
className={cn({ | ||
'h-10 py-3 pl-6 pr-16': true, | ||
'border-r border-dashed border-black pl-0 pr-5': isFirstColumn, | ||
'pr-0': isLastColumn, | ||
})} | ||
> | ||
{flexRender(column.columnDef.header, header.getContext())} | ||
</th> | ||
); | ||
})} | ||
</tr> | ||
))} | ||
</thead> | ||
<tbody> | ||
{hasData && | ||
table.getRowModel().rows.map((row) => { | ||
const coverage = row.original?.coverage || null; // %; | ||
const offset = tableDimensions.firstColumnWidth; | ||
const barWidth = (coverage * tableDimensions.availableBarWidth) / 100; | ||
|
||
return ( | ||
<tr | ||
key={row.id} | ||
className="border-b border-t border-black" | ||
style={{ | ||
backgroundImage: `linear-gradient(to right, rgb(72, 121, 255) ${barWidth}px, transparent ${barWidth}px)`, | ||
backgroundPositionX: `${offset}px`, | ||
}} | ||
> | ||
{row.getVisibleCells().map((cell) => { | ||
const { column } = cell; | ||
const isFirstColumn = column.id === firstColumn.accessorKey; | ||
const isLastColumn = column.id === lastColumn.accessorKey; | ||
|
||
return ( | ||
<td | ||
key={cell.id} | ||
className={cn({ | ||
'h-16 py-3 pl-6 pr-16': true, | ||
'border-r border-dashed border-black pl-0 pr-5': isFirstColumn, | ||
'pr-0': isLastColumn, | ||
})} | ||
> | ||
{flexRender(column.columnDef.cell, cell.getContext())} | ||
</td> | ||
); | ||
})} | ||
</tr> | ||
); | ||
})} | ||
|
||
{!hasData && ( | ||
<tr> | ||
<td colSpan={columns.length} className="h-24 text-center"> | ||
No results. | ||
</td> | ||
</tr> | ||
)} | ||
</tbody> | ||
</table> | ||
); | ||
}; | ||
|
||
export default DataToolTable; |
23 changes: 23 additions & 0 deletions
23
frontend/src/containers/data-tool/content/details/tables-settings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import GlobalRegionalTable from '@/containers/data-tool/content/details/tables/global-regional'; | ||
import NationalHighSeasTable from '@/containers/data-tool/content/details/tables/national-highseas'; | ||
|
||
const tablesSettings = { | ||
worldwideRegion: { | ||
locationTypes: ['worldwide', 'region'], | ||
component: GlobalRegionalTable, | ||
title: { | ||
worldwide: 'Marine Conservation at National and Regional Levels', | ||
region: 'Marine Conservation for {location}', | ||
}, | ||
}, | ||
countryHighseas: { | ||
locationTypes: ['country', 'highseas'], | ||
component: NationalHighSeasTable, | ||
title: { | ||
country: 'Marine Conservation for {location}', | ||
highSeas: 'Marine Conservation for High Seas', | ||
}, | ||
}, | ||
}; | ||
|
||
export default tablesSettings; |
96 changes: 96 additions & 0 deletions
96
frontend/src/containers/data-tool/content/details/tables/global-regional/columns.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { format } from 'd3-format'; | ||
|
||
// ! type me | ||
const columns = [ | ||
{ | ||
accessorKey: 'location', | ||
header: 'Location', | ||
cell: ({ row }) => { | ||
const { location } = row.original; | ||
return <span className="underline">{location}</span>; | ||
}, | ||
}, | ||
{ | ||
accessorKey: 'coverage', | ||
header: 'Coverage', | ||
cell: ({ row }) => { | ||
const { coverage: value } = row.original; | ||
if (!value) return <>—</>; | ||
return ( | ||
<span className="text-4xl font-bold"> | ||
{value} | ||
<span className="text-xs">%</span> | ||
</span> | ||
); | ||
}, | ||
}, | ||
{ | ||
accessorKey: 'locationType', | ||
header: 'Location type', | ||
}, | ||
{ | ||
accessorKey: 'mpas', | ||
header: 'MPAs', | ||
}, | ||
{ | ||
accessorKey: 'oecms', | ||
header: 'OECMs', | ||
}, | ||
{ | ||
accessorKey: 'area', | ||
header: 'Area', | ||
cell: ({ row }) => { | ||
const { area: value } = row.original; | ||
const formattedValue = format(',.2r')(value); | ||
return ( | ||
<span> | ||
{formattedValue} km<sup>2</sup> | ||
</span> | ||
); | ||
}, | ||
}, | ||
{ | ||
accessorKey: 'fullyHighProtected', | ||
header: 'Fully/Highly Protected', | ||
cell: ({ row }) => { | ||
const { fullyHighProtected: value } = row.original; | ||
if (!value) return <>No data</>; | ||
return ( | ||
<> | ||
{value} | ||
<span className="text-xs">%</span> | ||
</> | ||
); | ||
}, | ||
}, | ||
{ | ||
accessorKey: 'highlyProtectedLFP', | ||
header: 'Highly Protected LFP', | ||
cell: ({ row }) => { | ||
const { highlyProtectedLFP: value } = row.original; | ||
if (!value) return <>No data</>; | ||
return ( | ||
<> | ||
{value} | ||
<span className="text-xs">%</span> | ||
</> | ||
); | ||
}, | ||
}, | ||
{ | ||
accessorKey: 'globalContribution', | ||
header: 'Global contribution', | ||
cell: ({ row }) => { | ||
const { globalContribution: value } = row.original; | ||
if (!value) return <>No data</>; | ||
return ( | ||
<> | ||
{value} | ||
<span className="text-xs">%</span> | ||
</> | ||
); | ||
}, | ||
}, | ||
]; | ||
|
||
export default columns; |
11 changes: 11 additions & 0 deletions
11
frontend/src/containers/data-tool/content/details/tables/global-regional/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Table from '@/containers/data-tool/content/details/table'; | ||
import columns from '@/containers/data-tool/content/details/tables/global-regional/columns'; | ||
import mockedData from '@/containers/data-tool/content/details/tables/global-regional/mocked-data'; | ||
|
||
const GlobalRegionalTable: React.FC = () => { | ||
const data = mockedData; | ||
|
||
return <Table columns={columns} data={data} />; | ||
}; | ||
|
||
export default GlobalRegionalTable; |
Oops, something went wrong.