Skip to content

Commit 68a6fde

Browse files
authored
Merge pull request #15 from tableflowhq/feature/table-heading-caption
Added caption info for show tooltip icon in heading row
2 parents 60d9bfc + 5bcfaea commit 68a6fde

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/Table/index.tsx

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React, { createContext, useContext } from "react";
22
import classes from "../utils/classes";
33
import { CellProps, RowProps, TableProps } from "./types";
44
import themeDefault from "./style/Default.module.scss";
5+
import Tooltip from "../Tooltip";
6+
57

68
const TableContext = createContext<any>({});
79

@@ -31,8 +33,21 @@ export default function Table({
3133
// TABLE HEADINGS
3234
// Hide column title if the item has an action (action button) or the title starts with underscore
3335
const modelDatum = data?.[0];
34-
const thead: any = modelDatum ? Object.keys(modelDatum).map((k) => (k.indexOf("_") === 0 ? "" : k)) : {};
36+
const thead: any = modelDatum
37+
? Object.keys(modelDatum).map((k) => {
38+
const value = modelDatum[k];
39+
40+
if (k.indexOf("_") === 0) {
41+
return "";
42+
}
43+
44+
if (typeof value === 'object' && value?.captionInfo) {
45+
return { key: k, captionInfo: value.captionInfo };
46+
}
3547

48+
return k;
49+
})
50+
: {};
3651
const context = {
3752
style,
3853
highlightColumns,
@@ -75,11 +90,10 @@ export default function Table({
7590
);
7691
}
7792

78-
const Row = ({ datum, onClick }: RowProps) => {
93+
const Row = ({ datum, onClick, isHeading }: RowProps) => {
7994
const { style, highlightColumns, hideColumns, columnWidths, columnAlignments } = useContext(TableContext);
8095

8196
const className = classes([style?.tr]);
82-
8397
return (
8498
<div className={className} role="row" onClick={() => onClick?.(datum)}>
8599
{Object.keys(datum)
@@ -89,9 +103,11 @@ const Row = ({ datum, onClick }: RowProps) => {
89103
// datum[k] is the content for the cell
90104
// If it is an object with the 'content' property, use that as content (can be JSX or a primitive)
91105
// Another 'raw' property with a primitive value is used to sort and search
92-
const content = (datum[k] as any)?.content || datum[k];
106+
let content = (datum[k] as any)?.content || datum[k];
93107
const tooltip = (datum[k] as any)?.tooltip;
94-
108+
const captionInfo = isHeading ? (datum[k] as any)?.captionInfo : null;
109+
const headingKey = isHeading ? (datum[k] as any)?.key : null;
110+
content = isHeading && captionInfo ? <Tooltip title={captionInfo}>{headingKey}</Tooltip> : content;
95111
const wrappedContent = content && typeof content === "string" ? <span>{content}</span> : content;
96112

97113
const cellClass = classes([
@@ -121,4 +137,4 @@ const Cell = ({ children, cellClass, cellStyle, tooltip }: CellProps) => {
121137
...(tooltip ? { title: tooltip } : {}),
122138
};
123139
return <div {...cellProps}>{children}</div>;
124-
};
140+
};

src/Table/storyData.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const storyData = [
22
{
33
id: 1,
4-
Name: "John Doe",
4+
Name: {
5+
raw: "John Doe",
6+
content: "John Doe",
7+
captionInfo: "This is a caption example",
8+
},
59
Age: 25,
610
711
},

src/Table/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type TableComposite = {
88
raw: Primitive;
99
content: Primitive | React.ReactElement;
1010
tooltip?: string;
11+
captionInfo?: string;
1112
};
1213

1314
export type TableValue = Primitive | TableComposite;

0 commit comments

Comments
 (0)