Skip to content

Commit

Permalink
feat: Add tooltips for description in chart tables
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Sep 16, 2022
1 parent db83180 commit f287d08
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
22 changes: 15 additions & 7 deletions app/charts/table/table-content.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box, TableSortLabel, Theme } from "@mui/material";
import { Box, TableSortLabel, Theme, Tooltip } from "@mui/material";
import { makeStyles } from "@mui/styles";
import clsx from "clsx";
import * as React from "react";
import React, { useMemo, useContext } from "react";
import { HeaderGroup } from "react-table";

import Flex from "../../components/flex";
Expand All @@ -28,7 +28,7 @@ export const TableContentProvider = ({
totalColumnsWidth,
children,
}: TableContentProps & { children: React.ReactNode }) => {
const value = React.useMemo(() => {
const value = useMemo(() => {
return {
headerGroups,
tableColumnsMeta,
Expand Down Expand Up @@ -66,7 +66,7 @@ const useStyles = makeStyles((theme: Theme) => ({
}));

export const TableContent = ({ children }: { children: React.ReactNode }) => {
const ctx = React.useContext(TableContentContext);
const ctx = useContext(TableContentContext);
const classes = useStyles();

if (!ctx) {
Expand All @@ -85,8 +85,8 @@ export const TableContent = ({ children }: { children: React.ReactNode }) => {
// eslint-disable-next-line react/jsx-key
<Box {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map((column) => {
const { columnComponentType } = tableColumnsMeta[column.id];

const { columnComponentType, iri, description } =
tableColumnsMeta[column.id];
// We assume that the customSortCount items are at the beginning of the sorted array, so any item with a lower index must be a custom sorted one
const isCustomSorted = column.sortedIndex < customSortCount;

Expand All @@ -105,7 +105,15 @@ export const TableContent = ({ children }: { children: React.ReactNode }) => {
active={isCustomSorted}
direction={column.isSortedDesc ? "desc" : "asc"}
>
{column.render("Header")}
{description ? (
<Tooltip arrow title={description}>
<span style={{ textDecoration: "underline" }}>
{column.render("Header")}
</span>
</Tooltip>
) : (
column.render("Header")
)}
</TableSortLabel>
</Flex>
);
Expand Down
15 changes: 13 additions & 2 deletions app/charts/table/table-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { estimateTextWidth } from "@/utils/estimate-text-width";
export type MKColumnMeta<T> = {
iri: string;
slugifiedIri: string;
description?: string;
columnComponentType: ComponentType;
formatter: (cell: Cell<Observation, any>) => string;
textStyle?: string;
Expand Down Expand Up @@ -256,6 +257,9 @@ const useTableState = ({
* It is not used by react-table, only for custom styling.
*/
const tableColumnsMeta = useMemo<TableChartState["tableColumnsMeta"]>(() => {
const allColumnsByIri = Object.fromEntries(
[...dimensions, ...measures].map((x) => [x.iri, x])
);
return mapKeys(
mapValues(fields, (columnMeta, iri) => {
const slugifiedIri = getSlugifiedIri(iri);
Expand All @@ -264,11 +268,11 @@ const useTableState = ({
const columnComponentType = columnMeta.componentType;
const formatter = formatters[iri];
const cellFormatter = (x: Cell<Observation>) => formatter(x.value);

const common = {
iri,
slugifiedIri,
columnComponentType,
description: allColumnsByIri[iri]?.description || undefined,
formatter: cellFormatter,
...columnStyle,
};
Expand Down Expand Up @@ -357,7 +361,14 @@ const useTableState = ({
}),
(v) => v.slugifiedIri
);
}, [data, dimensions, fields, formatters, theme.palette.primary.main]);
}, [
data,
dimensions,
fields,
formatters,
measures,
theme.palette.primary.main,
]);

return {
chartType: "table",
Expand Down

0 comments on commit f287d08

Please sign in to comment.