Skip to content

Commit b345e3f

Browse files
committed
[FEATURE] Components: Read and render links in the Table component
Signed-off-by: Seyed Mahmoud SHAHROKNI <seyedmahmoud.shahrokni@amadeus.com> Signed-off-by: Mahmoud Shahrokni <seyedmahmoud.shahrokni@amadeus.com>
1 parent ee1b242 commit b345e3f

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

components/src/Table/TableCell.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
import { TableCell as MuiTableCell, styled, TableCellProps as MuiTableCellProps, Box, useTheme } from '@mui/material';
14+
import {
15+
TableCell as MuiTableCell,
16+
styled,
17+
TableCellProps as MuiTableCellProps,
18+
Box,
19+
useTheme,
20+
Link,
21+
} from '@mui/material';
1522
import { ReactElement, useEffect, useRef } from 'react';
16-
import { TableCellAlignment, TableDensity, getTableCellLayout } from './model/table-model';
23+
import { DataLink, TableCellAlignment, TableDensity, getTableCellLayout } from './model/table-model';
1724

1825
const StyledMuiTableCell = styled(MuiTableCell)(({ theme }) => ({
1926
padding: 0,
@@ -65,6 +72,7 @@ export interface TableCellProps extends Omit<MuiTableCellProps, 'tabIndex' | 'al
6572
onFocusTrigger?: (e: React.MouseEvent<HTMLTableCellElement> | React.KeyboardEvent<HTMLTableCellElement>) => void;
6673
color?: string;
6774
backgroundColor?: string;
75+
dataLink?: DataLink;
6876
}
6977

7078
export function TableCell({
@@ -81,10 +89,10 @@ export function TableCell({
8189
align,
8290
color,
8391
backgroundColor,
92+
dataLink,
8493
...otherProps
8594
}: TableCellProps): ReactElement {
8695
const theme = useTheme();
87-
8896
const elRef = useRef<HTMLTableCellElement>();
8997

9098
const isHeader = variant === 'head';
@@ -172,7 +180,17 @@ export function TableCell({
172180
aria-label={description}
173181
textAlign={align}
174182
>
175-
{children}
183+
{dataLink ? (
184+
<Link
185+
href={dataLink.url}
186+
target={dataLink.openNewTab ? '_blank' : '_self'}
187+
rel={dataLink.openNewTab ? 'noopener noreferrer' : undefined}
188+
>
189+
{children}
190+
</Link>
191+
) : (
192+
children
193+
)}
176194
</Box>
177195
</StyledMuiTableCell>
178196
);

components/src/Table/VirtualizedTable.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
import { Column, HeaderGroup, Row, flexRender } from '@tanstack/react-table';
14+
import { Column, ColumnMeta, HeaderGroup, Row, flexRender } from '@tanstack/react-table';
1515
import { Box, TablePagination, TableRow as MuiTableRow } from '@mui/material';
1616
import { TableVirtuoso, TableComponents, TableVirtuosoHandle, TableVirtuosoProps } from 'react-virtuoso';
1717
import { useRef, useMemo, ReactElement } from 'react';
@@ -22,7 +22,7 @@ import { TableHead } from './TableHead';
2222
import { TableHeaderCell } from './TableHeaderCell';
2323
import { TableCell, TableCellProps } from './TableCell';
2424
import { VirtualizedTableContainer } from './VirtualizedTableContainer';
25-
import { TableCellConfigs, TableProps, TableRowEventOpts } from './model/table-model';
25+
import { TableCellConfigs, TableColumnConfig, TableProps, TableRowEventOpts } from './model/table-model';
2626
import { useVirtualizedTableKeyboardNav } from './hooks/useVirtualizedTableKeyboardNav';
2727
import { TableFoot } from './TableFoot';
2828

@@ -72,6 +72,7 @@ export function VirtualizedTable<TableData>({
7272
startIndex: 0,
7373
endIndex: 0,
7474
});
75+
7576
const setVisibleRange: TableVirtuosoProps<TableData, unknown>['rangeChanged'] = (newVisibleRange) => {
7677
visibleRange.current = newVisibleRange;
7778
};
@@ -218,7 +219,6 @@ export function VirtualizedTable<TableData>({
218219
<>
219220
{row.getVisibleCells().map((cell, i, cells) => {
220221
const position: TableCellPosition = {
221-
// Add 1 to the row index because the header is row 0
222222
row: index + 1,
223223
column: i,
224224
};
@@ -229,6 +229,16 @@ export function VirtualizedTable<TableData>({
229229
const cellRenderFn = cell.column.columnDef.cell;
230230
const cellContent = typeof cellRenderFn === 'function' ? cellRenderFn(cellContext) : null;
231231

232+
/*
233+
IMPORTANT:
234+
If Variables exist in the link, they should have been translated by the plugin already. (Being developed at the moment)
235+
Components have no access to any context (Which is intentional and correct)
236+
We may want to add parameters to a link from neighboring cells in the future as well.
237+
If this is the case, the value of the neighboring cells should be read from here and be replaced. (Bing discussed at the moment, not decided yet)
238+
*/
239+
const { dataLink } = cell.column.columnDef.meta as ColumnMeta<TableData, unknown> &
240+
Pick<TableColumnConfig<TableData>, 'dataLink'>;
241+
232242
const cellDescriptionDef = cell.column.columnDef.meta?.cellDescription;
233243
let description: string | undefined = undefined;
234244
if (typeof cellDescriptionDef === 'function') {
@@ -258,6 +268,7 @@ export function VirtualizedTable<TableData>({
258268
description={description}
259269
color={cellConfig?.textColor ?? undefined}
260270
backgroundColor={cellConfig?.backgroundColor ?? undefined}
271+
dataLink={dataLink}
261272
>
262273
{cellConfig?.text || cellContent}
263274
</TableCell>

components/src/Table/model/table-model.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ declare module '@tanstack/table-core' {
265265
}
266266
}
267267

268+
// Column link settings
269+
// The URL could be set to a static link or could be constructed dynamically
270+
// The URL may include reference to the variables or neighboring cells in the row
271+
export interface DataLink {
272+
url: string;
273+
title?: string;
274+
openNewTab: boolean;
275+
}
276+
268277
// Only exposing a very simplified version of the very extensive column definitions
269278
// possible with tanstack table to make it easier for us to control rendering
270279
// and functionality.
@@ -324,11 +333,7 @@ export interface TableColumnConfig<TableData>
324333
* Dynamic link setting. If available the the cell content should turn into
325334
* a link with the value of the cell as the dynamic section
326335
*/
327-
dataLink?: {
328-
url: string;
329-
title?: string;
330-
openNewTab: boolean;
331-
};
336+
dataLink?: DataLink;
332337
}
333338

334339
/**
@@ -338,7 +343,7 @@ export function persesColumnsToTanstackColumns<TableData>(
338343
columns: Array<TableColumnConfig<TableData>>
339344
): Array<ColumnDef<TableData>> {
340345
const tableCols: Array<ColumnDef<TableData>> = columns.map(
341-
({ width, align, headerDescription, cellDescription, enableSorting, ...otherProps }) => {
346+
({ width, align, headerDescription, cellDescription, enableSorting, dataLink, ...otherProps }) => {
342347
// Tanstack Table does not support an "auto" value to naturally size to fit
343348
// the space in a table. We translate our custom "auto" setting to 0 size
344349
// for these columns, so it is easy to fall back to auto when rendering.
@@ -370,6 +375,7 @@ export function persesColumnsToTanstackColumns<TableData>(
370375
align,
371376
headerDescription,
372377
cellDescription,
378+
dataLink,
373379
},
374380
};
375381

0 commit comments

Comments
 (0)