Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/Table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, { createContext, useContext } from "react";
import classes from "../utils/classes";
import { CellProps, RowProps, TableProps } from "./types";
import themeDefault from "./style/Default.module.scss";
import Tooltip from "../Tooltip";


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

Expand Down Expand Up @@ -31,8 +33,21 @@ export default function Table({
// TABLE HEADINGS
// Hide column title if the item has an action (action button) or the title starts with underscore
const modelDatum = data?.[0];
const thead: any = modelDatum ? Object.keys(modelDatum).map((k) => (k.indexOf("_") === 0 ? "" : k)) : {};
const thead: any = modelDatum
? Object.keys(modelDatum).map((k) => {
const value = modelDatum[k];

if (k.indexOf("_") === 0) {
return "";
}

if (typeof value === 'object' && value?.captionInfo) {
return { key: k, captionInfo: value.captionInfo };
}

return k;
})
: {};
const context = {
style,
highlightColumns,
Expand Down Expand Up @@ -75,11 +90,10 @@ export default function Table({
);
}

const Row = ({ datum, onClick }: RowProps) => {
const Row = ({ datum, onClick, isHeading }: RowProps) => {
const { style, highlightColumns, hideColumns, columnWidths, columnAlignments } = useContext(TableContext);

const className = classes([style?.tr]);

return (
<div className={className} role="row" onClick={() => onClick?.(datum)}>
{Object.keys(datum)
Expand All @@ -89,9 +103,11 @@ const Row = ({ datum, onClick }: RowProps) => {
// datum[k] is the content for the cell
// If it is an object with the 'content' property, use that as content (can be JSX or a primitive)
// Another 'raw' property with a primitive value is used to sort and search
const content = (datum[k] as any)?.content || datum[k];
let content = (datum[k] as any)?.content || datum[k];
const tooltip = (datum[k] as any)?.tooltip;

const captionInfo = isHeading ? (datum[k] as any)?.captionInfo : null;
const headingKey = isHeading ? (datum[k] as any)?.key : null;
content = isHeading && captionInfo ? <Tooltip title={captionInfo}>{headingKey}</Tooltip> : content;
const wrappedContent = content && typeof content === "string" ? <span>{content}</span> : content;

const cellClass = classes([
Expand Down Expand Up @@ -121,4 +137,4 @@ const Cell = ({ children, cellClass, cellStyle, tooltip }: CellProps) => {
...(tooltip ? { title: tooltip } : {}),
};
return <div {...cellProps}>{children}</div>;
};
};
6 changes: 5 additions & 1 deletion src/Table/storyData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const storyData = [
{
id: 1,
Name: "John Doe",
Name: {
raw: "John Doe",
content: "John Doe",
captionInfo: "This is a caption example",
},
Age: 25,
Email: "john.doe@example.com",
},
Expand Down
1 change: 1 addition & 0 deletions src/Table/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type TableComposite = {
raw: Primitive;
content: Primitive | React.ReactElement;
tooltip?: string;
captionInfo?: string;
};

export type TableValue = Primitive | TableComposite;
Expand Down