Skip to content

Commit

Permalink
feat: legend table is implemeted using tanstack table
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandru-HM authored and diehbria committed Jan 5, 2024
1 parent 403f2bf commit c92533a
Show file tree
Hide file tree
Showing 12 changed files with 521 additions and 10 deletions.
7 changes: 4 additions & 3 deletions packages/react-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@
"@types/dompurify": "3.0.2",
"@types/is-hotkey": "^0.1.7",
"@types/jest": "^29.4.4",
"@types/lodash.difference": "4.5.9",
"@types/lodash.maxby": "4.6.7",
"@types/lodash.merge": "4.6.7",
"@types/lodash.minby": "4.6.7",
"@types/lodash.omitby": "^4.6.7",
"@types/lodash.throttle": "4.1.7",
"@types/lodash.difference": "4.5.9",
"@types/react": "^18.2.12",
"@types/react-cytoscapejs": "^1.2.2",
"@types/react-dom": "^18.2.5",
Expand Down Expand Up @@ -106,6 +106,7 @@
"@iot-app-kit/core": "9.12.0",
"@iot-app-kit/core-util": "9.12.0",
"@iot-app-kit/source-iottwinmaker": "9.12.0",
"@tanstack/react-table": "^8.10.7",
"color": "^4.2.3",
"copy-to-clipboard": "^3.3.3",
"d3-array": "^3.2.3",
Expand All @@ -115,12 +116,12 @@
"echarts": "^5.4.3",
"is-hotkey": "^0.2.0",
"jest-canvas-mock": "^2.5.2",
"lodash.difference": "4.5.0",
"lodash.maxby": "4.6.0",
"lodash.merge": "4.6.2",
"lodash.minby": "4.6.0",
"lodash.omitby": "^4.6.0",
"lodash.throttle": "^4.1.1",
"lodash.difference": "4.5.0",
"parse-duration": "^1.0.3",
"react-cytoscapejs": "^2.0.0",
"react-error-boundary": "^4.0.10",
Expand All @@ -143,4 +144,4 @@
"url": "https://github.com/awslabs/iot-app-kit/issues"
},
"homepage": "https://github.com/awslabs/iot-app-kit#readme"
}
}
11 changes: 10 additions & 1 deletion packages/react-components/src/components/chart/baseChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { useViewport } from '../../hooks/useViewport';
import { getXAxis } from './chartOptions/axes/xAxis';
import { useHandleChartEvents } from './events/useHandleChartEvents';
import { TREND_CURSOR_KEY_MAP } from './trendCursor/constants';
import { useGetConfigValue } from '../../store';
import TanstackLegend from './legend/tanstackLegend';

/**
* Developer Notes:
Expand All @@ -53,6 +55,8 @@ const BaseChart = ({ viewport, queries, size = { width: 500, height: 500 }, ...o

const { group } = useViewport();

const showTanstackTable = useGetConfigValue('showTanstackTable');

// convert TimeSeriesDataQuery to TimeSeriesData
const {
isLoading,
Expand Down Expand Up @@ -238,7 +242,12 @@ const BaseChart = ({ viewport, queries, size = { width: 500, height: 500 }, ...o
width: rightLegendWidth,
}}
>
<Legend series={series} graphic={trendCursors} datastreams={dataStreams} width={rightLegendWidth} />
{/* TODO: Need to remove cloudscape table after new tanstack table approved and make tanstack table as default */}
{showTanstackTable ? (
<TanstackLegend series={series} graphic={trendCursors} datastreams={dataStreams} width={rightLegendWidth} />
) : (
<Legend series={series} graphic={trendCursors} datastreams={dataStreams} width={rightLegendWidth} />
)}
</div>
)}
</div>
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from 'react';
import { Table } from '@cloudscape-design/components';
import useChartsLegend from './useChartsLegend';
import { SeriesOption } from 'echarts';

import './legend.css';
import { useCollection } from '@cloudscape-design/collection-hooks';
import { DataStream } from '@iot-app-kit/core';

import { SeriesOption } from 'echarts';
import useChartsLegend from './useChartsLegend';

import { InternalGraphicComponentGroupOption } from '../trendCursor/types';
import './legend.css';

const Legend = (legendOptions: {
datastreams: DataStream[];
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { DataStream } from '@iot-app-kit/core';
import { ColumnDef } from '@tanstack/react-table';

import { SeriesOption } from 'echarts';
import useChartsLegendTanstack from './useChartsLegendTanstack';

import { InternalGraphicComponentGroupOption } from '../trendCursor/types';
import { TanstackTable } from './tanstackTable';

type legendOptions = {
datastreams: DataStream[];
series: SeriesOption[];
graphic: InternalGraphicComponentGroupOption[];
width: number;
};

const TanstackLegend = (legendOptions: legendOptions) => {
const { items: allItemsTanstack, columnDefinitions: columnDefinitionsTanstack } =
useChartsLegendTanstack(legendOptions);

return (
<TanstackTable
data={allItemsTanstack}
columnDefinitions={columnDefinitionsTanstack as ColumnDef<object, string>[]}
stickyColumns={{ first: 1 }}
stickyHeader
/>
);
};

export default TanstackLegend;
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
.tanstack-table-container {
height: 92%;
overflow: auto;
}

.tanstack-table {
width: 100%;
font-size: 14px;
font-weight: 400;
line-height: 16px;
}

.tanstack-table-sticky-header {
position: sticky;
z-index: 3;
top: 0;
cursor: pointer;
text-align: left;
}

.tanstack-table-sticky-column {
position: sticky;
left: 0;
cursor: default;
text-align: left;
z-index: 4;
white-space: nowrap;
}

.tanstack-table-header-divider:not(:last-child)::before {
content: "";
position: absolute;
right: 0;
bottom: 0;
top: 0;
min-height: 18px;
max-height: 30px;
margin: auto;
border-left: 2px solid #e9ebed;
box-sizing: border-box;
}

.tanstack-table-nonsticky-column-header {
display: flex;
align-items: center;
white-space: nowrap;
line-height: 1.2;
}

.tanstack-table-body {
cursor: default;
left: 0;
}

.tanstack-table-sticky-column-body {
left: 0;
position: sticky;
z-index: 1;
white-space: nowrap;
cursor: default;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import React, { useState } from 'react';

import {
spaceStaticXxs,
colorBorderButtonPrimaryDisabled,
colorBackgroundButtonNormalDefault,
spaceStaticM,
spaceStaticXxxs,
colorBackgroundLayoutToggleActive,
spaceStaticXs,
} from '@cloudscape-design/design-tokens';

import {
flexRender,
getCoreRowModel,
useReactTable,
getSortedRowModel,
SortingState,
ColumnDef,
} from '@tanstack/react-table';

import AscendingIcon from './ascendingIcon.svg';
import DescendingIcon from './descendingIcon.svg';
import SortingIcon from './sortingIcon.svg';
import './tanstackTable.css';

type tanstackTableOptions = {
data: object[];
columnDefinitions: ColumnDef<object, string>[];
stickyColumns?: { first: number };
stickyHeader?: boolean;
};

export const TanstackTable = ({ data, columnDefinitions, stickyColumns, stickyHeader }: tanstackTableOptions) => {
const columns: ColumnDef<object, string>[] = [...columnDefinitions];
const [sorting, setSorting] = useState<SortingState>([]);
const sortingIcons = {
padding: `0 ${spaceStaticXs}`,
height: spaceStaticM,
width: spaceStaticM,
strokeWidth: spaceStaticXxxs,
};

const legendData = useReactTable({
data,
columns,
state: {
sorting,
},
getCoreRowModel: getCoreRowModel(),
onSortingChange: setSorting,
getSortedRowModel: getSortedRowModel(),
});

return (
<div className='tanstack-table-container'>
<table className='tanstack-table' style={{ color: colorBackgroundLayoutToggleActive }}>
<thead>
{legendData.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header, i) => (
<th
className={`tanstack-table-header-divider ${stickyHeader && 'tanstack-table-sticky-header'} ${
stickyColumns?.first === i + 1 && 'tanstack-table-sticky-column'
}`}
key={header.id}
style={{
padding: `${spaceStaticM} ${spaceStaticXxs}`,
backgroundColor: `${colorBackgroundButtonNormalDefault}`,
borderBottom: `1px solid ${colorBorderButtonPrimaryDisabled}`,
}}
onClick={() => {
header.column.getCanSort() && header.column.toggleSorting();
}}
>
{!header.isPlaceholder ? (
<div
{...(stickyColumns?.first !== i + 1 && {
className: 'tanstack-table-nonsticky-column-header',
})}
style={{ ...(stickyColumns?.first !== i + 1 && { padding: `0px ${spaceStaticXs}` }) }}
>
{flexRender(header.column.columnDef.header, header.getContext())}
{{
asc: (
<span style={sortingIcons}>
<img alt='sorted asending' src={AscendingIcon}></img>
</span>
),
desc: (
<span style={sortingIcons}>
<img alt='sorted desending' src={DescendingIcon}></img>
</span>
),
}[header.column.getIsSorted() as string] ??
(stickyColumns?.first !== i + 1 && (
<span style={{ ...sortingIcons }}>
<img alt='desending not active' src={SortingIcon}></img>
</span>
))}
</div>
) : null}
</th>
))}
</tr>
))}
</thead>
<tbody>
{legendData.getRowModel().rows.map((row) => (
<tr key={row.id}>
{row.getVisibleCells().map((cell, i) => (
<td
key={cell.id}
className={
stickyColumns?.first === i + 1 ? 'tanstack-table-sticky-column-body' : 'tanstack-table-body'
}
style={{
borderBottom: `1px solid ${colorBorderButtonPrimaryDisabled}`,
padding: `${spaceStaticXxxs} ${spaceStaticXxs}`,
...(stickyColumns?.first === i + 1 && {
backgroundColor: `${colorBackgroundButtonNormalDefault}`,
}),
}}
>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
);
};

0 comments on commit c92533a

Please sign in to comment.